Help with functions
May 2, 2013 at 8:05pm UTC
this is a code to see if the number entered is an armstrong or not
for example 371 is armstrong because
3^3+7^3+1^3=371
(the cubic summation==the number)
my code give wrong result!!!
please help me
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
#include<iostream>
using namespace std;
bool arm(int num);
int main()
{
int y;
cout<<"enter anumber" <<endl;
cin>>y;
bool s;
s=arm(y);
if (s)
cout<<"its armstrong" <<endl;
else
cout<<"its NOt" <<endl;
return 0;
}
bool arm(int num)
{
int x; int sum=0;
while (num!=0)
{
x=num%10;
sum=sum+(x*x*x);
num=num/10;
}
if (num==sum)
return true ;
else
return false ;
}
May 2, 2013 at 8:12pm UTC
You modified your num variable so it no longer carries the original value. The while loop only exits when num == 0; hence your condition becomes:
I suggest to keep a copy of the original value in another variable.
Last edited on May 2, 2013 at 8:13pm UTC
May 2, 2013 at 8:20pm UTC
thankss man.. !!!
Topic archived. No new replies allowed.