# Answer to Question #62741 in C for Naveena

Question #62741

write a c program to find out number is armstrom or not??

Expert's answer

#include <stdio.h>

int power(int a, int n) {

int res = 1, i;

for (i = 0; i < n; i++) {

res *= a;

}

return res;

}

int isArmstrongNumber(int n) {

int res = 0, m, numOfDigits = 0;

m = n;

do {

numOfDigits++;

m /= 10;

} while (m);

m = n;

do {

int d = m % 10;

m /= 10;

res += power(d, numOfDigits);

} while (m);

return (res == n);

}

int main() {

int n;

scanf("%d", &n);

printf(isArmstrongNumber(n) ? "YES\n" : "NO\n");

return 0;

}

int power(int a, int n) {

int res = 1, i;

for (i = 0; i < n; i++) {

res *= a;

}

return res;

}

int isArmstrongNumber(int n) {

int res = 0, m, numOfDigits = 0;

m = n;

do {

numOfDigits++;

m /= 10;

} while (m);

m = n;

do {

int d = m % 10;

m /= 10;

res += power(d, numOfDigits);

} while (m);

return (res == n);

}

int main() {

int n;

scanf("%d", &n);

printf(isArmstrongNumber(n) ? "YES\n" : "NO\n");

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment