Home Tools

Find the Armstrong number using C language

If any number is equal to the sum of its own digits raised to the power of the number of digits is called Armstrong Number.
armstrong.c
1#include <stdio.h>
2int main() {
3	//Entry Point
4	int number, temp, count = 0, total = 0;
5	printf("Enter the integer number: ");
6	scanf("%d", &number);
7	
8	//Find the number of digits
9	temp = number;
10	while(temp > 0) {
11		count = count + 1;
12		temp = temp/10;
13	}
14	
15	temp = number;
16	while(temp > 0) {
17		total = total + power(temp%10,count);
18        temp = temp/10;
19	}
20		
21	if(number == total) {
22		printf("%d is an Armstrong number.\n", number);
23	}
24	else
25	{
26		printf("%d is not an Armstrong number.\n", number);
27	}
28	return 0;
29}
30
31//User Defined Pow Function
32int power(int base,int exponent) {
33	int sum = 1;
34	while(exponent > 0) {
35		sum = sum * base;
36		exponent = exponent - 1;
37	}
38	return sum;
39}

Output

Enter the integer number:153
153 is an Armstrong number.