Write C program to find Armstrong numbers between 1 to n

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

** For Example: ** 407 = 4^{3} + 0^{3} + 7^{3} = 64 + 0 + 343 = 407

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <stdio.h> #include <math.h> //defines common mathematical functions int main() { int lower, higher, i, temp1, temp2, remainder, n = 0, result = 0; printf("Enter two numbers: "); //Reading numbers from user scanf("%d %d", &lower, &higher); printf("Armstrong numbers between %d an %d are: ", lower, higher); for(i = lower + 1; i < higher; ++i) { temp2 = i; temp1 = i; // number of digits calculation while (temp1 != 0) { temp1 /= 10; ++n; } // result contains sum of nth power of its digits while (temp2 != 0) { remainder = temp2 % 10; result += pow(remainder, n); temp2 /= 10; } // checking if number i is equal to the sum of nth power of its digits if (result == i) { printf("%d ", i); } // resetting the values to check Armstrong number for next iteration n = 0; result = 0; } return 0; } |

06 July 2019 3592
Written By: **Rohit**

© 2020 Tech Study. All rights reserved | Developed by Tech Study| Privacy Policy | Sitemap