Write a function that check if a number is an Armstrong number.

Determine how many digits are in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer is the original number then it is an Armstrong number.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example, 371 is an Armstrong number since \(3^3 + 7^3 + 1^3 = 371\).


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#include<math.h>

int isArmstrong(int nb)
{
	int lastDigit, sum, originalNum, digits;
	sum = 0;

	originalNum = nb;

	// total digits in nb 
	digits = (int)log10(nb) + 1;


	while (nb > 0)
	{
		// Extract the last digit
		lastDigit = nb % 10;

		// Compute sum of power of last digit
		sum = sum + round(pow(lastDigit, digits));

		// Remove the last digit
		nb = nb / 10;
	}

	return (originalNum == sum);
}


void main()
{
	int a;
	printf("Enter an integer: ");
	scanf("%d", &a);

 
	if (isArmstrong(a))
		printf("%d is Armstrong.\n",a);
	else
		printf("%d is not Armstrong.\n", a);
 
	getch();
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Complexity of sum of square root