0

Armstrong no. sum error

#include<iostream> #include<cmath> using namespace std ; int countNum (int) ; int isArmstrongNum (int) ; int countNum(int n){ int digit=0; while (n>0){ n=n/10 ; digit++ ; } return digit ; } int isArmstrong (int n){ int temp,power, sum=0; temp = n; power = countNum(n); while(n>0){ sum = sum + pow (n%10, power) ; n=n/10 ; } return sum==temp; } int main(){ int lower, upper, sum=0 ; cout<< "Enter the lower and upper limit"<<endl; cin>>lower; cin>>upper; while (lower>=upper){ if (isArmstrong(lower)){ sum+=lower ; lower++ ; } } cout<< sum ; return 0; } The isArmstrong function is working but i'm am not getting the sum for lower and upper limit. Can Someone Please point out the error.

7th Jun 2019, 5:43 PM
Aditya Kumar
Aditya Kumar - avatar
3 odpowiedzi
+ 2
please use the code playground for easy debugging
8th Jun 2019, 2:02 AM
Ôť│AsterisKÔť│
Ôť│AsterisKÔť│ - avatar
0
Can you give an example? I couldn't find any bugs. Maybe it is better for larger numbers to use long instead of int.
7th Jun 2019, 10:14 PM
Denise Ro├čberg
Denise Ro├čberg - avatar
0
int main (){ int i, sum=0; cout << "Enter a Number"<<endl ; for (i=1 ; i <= 50000 ; i++){ if (isArmstrong(i)){ cout << i <<endl; } } return 0 ; } if i replace last para of code with this then its show all the armstrong no between 1 to 50000
8th Jun 2019, 5:28 AM
Aditya Kumar
Aditya Kumar - avatar