0

Code for Armstrong number

I have wrote the code for armstrong number where each digits are raised to thhe power of total digits in the number Please help it because I'm not getting the correct code #include <iostream> using namespace std; float power(int base, int exp) { float n = 1.0; for (int i = 0; i < exp; i++) { n = n * base; } return n; } void arm(int a) { int count = 0, rem = 0; int num=0; while (a > 0) { a /= 10; count++; } cout << count << endl; while (a > 0) { rem = a % 10; num =num+ power(rem, count); a /= 10; //cout << num << endl; } cout << num; } int main() { cout << "Enter the number "; int n; cin >> n; arm(n); return 0; }

20th Apr 2021, 9:10 AM
Anjali Shaw
Anjali Shaw - avatar
2 odpowiedzi
+ 2
I am telling you what's happening : in this line, while (a > 0) { a /= 10; count++; } a becomes 0. So when it goes to this line, while (a > 0) { rem = a % 10; num =num+ power(rem, count); a /= 10; //cout << num << endl; } it doesn't go inside this loop. As a result , num=0 declared in the very beginning of that function ,still remains the same and prints 0.
20th Apr 2021, 9:22 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
The future is now thanks to science thanks Ôś║´ŞĆ ! Actually I forgot the fact that value of a gets changed In first while loop thanks ! Buddy !
20th Apr 2021, 9:50 AM
Anjali Shaw
Anjali Shaw - avatar