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>
2 int main() {
3     int number, temp, count = 0, total = 0;
4     printf("Enter the integer number: ");
5     scanf("%d", &number);
6     
7     temp = number;
8     while(temp > 0) {
9         count = count + 1;
10         temp = temp/10;
11     }
12     
13     temp = number;
14     while(temp > 0) {
15         total = total + power(temp%10,count);
16         temp = temp/10;
17     }
18         
19     if(number == total) {
20         printf("%d is an Armstrong number.\n", number);
21     }
22     else
23     {
24         printf("%d is not an Armstrong number.\n", number);
25     }
26     return 0;
27 }
28 
29 //User Defined Pow Function
30 int power(int base,int exponent) {
31     int sum = 1;
32     while(exponent > 0) {
33         sum = sum * base;
34         exponent = exponent - 1;
35     }
36     return sum;
37 }

No comments:

Post a Comment

Algoberry is a programming blog, focusing on PHP, C++, C, Java, Creating Software Tools and many more.

Search This Blog

Powered by Blogger.

Blog Archive