How to write Armstrong number in C program by inventorpradhap 0 comments Armstrong number in C program #include<stdio.h> #include<math.h> int main() { int n,sum =0,digit = 0,copy; printf("\nEnter the value of n : "); /* if entered n is 153*/ scanf("%d", &n); copy = n; while(copy>0) { copy = copy/10; digit++; } copy = n; while(copy>0) { sum = sum+pow(copy%10,digit); copy = copy/10; } if(sum==n) { printf("Entered number is an Armstrong Number"); } else { printf("Entered number is not an Armstrong Number"); } return 0; } /* First while loop for count digits 1). copy = n ; copy = 153; check 53>0 yes; copy = 153/10; = 15; digit++ = 1; 2). copy = 15; check 15>0 yes; copy = 15/10; 1; digit++ = 2; 3). copy = 1; check 1>0 yes; copy = 1/10; = 0; digit++ = 3; Second while loop for sum and power 1). copy = n; copy = 153; check 153>0 yes; sum = 0+ pow(153%10,3) => 0+ 3^3 = 27; copy = 153/10; = 15; 2). copy = 15; check 15>0 yes; sum = 27 + pow(15%10,3) => 27+ 5^3 = 152; 3). copy = 1; check 1>0 yes; sum = 152 + pow(1%10,3) => 152 + 1^3 = 153; */ inventorpradhap
No comments:
Post a Comment