Monday, 13 July 2015

Write C Program to Find Armstrong Number

1:  #include <stdio.h>  
2:  int power(int, int);  
3:  int main()  
4:  {  
5:    int n, sum = 0, temp, remainder, digits = 0;  
6:    printf("Input an integer\n");  
7:    scanf("%d", &n);  
8:    temp = n;  
9:    // Count number of digits  
10:    while (temp != 0) {  
11:     digits++;  
12:     temp = temp/10;  
13:    }  
14:    temp = n;  
15:    while (temp != 0) {  
16:     remainder = temp%10;  
17:     sum = sum + power(remainder, digits);  
18:     temp = temp/10;  
19:    }  
20:    if (n == sum)  
21:     printf("%d is an Armstrong number.\n", n);  
22:    else  
23:     printf("%d is not an Armstrong number.\n", n);  
24:    return 0;  
25:  }  
26:  int power(int n, int r) {  
27:    int c, p = 1;  
28:    for (c = 1; c <= r; c++)   
29:     p = p*n;  
30:    return p;    

No comments:

Post a Comment