help me quick plzzz
Nov 30, 2013 at 9:37pm UTC
i have to write a program that says whether the number i enter is Naracissistic or not.
i wrote the codes and everything but every time i write 153 or 24678051 it says its not a Naracissistic number :( plz tell me whats wrong with the codes i wrote :(
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 41 42 43 44 45 46 47 48 49 50 51 52 53
# include <iostream>
# include <cmath>
using namespace std;
bool isNarcissistic (int num1);
int main()
{
int num1;
cout << "Enter a positive number" ;
cin >> num1;
if (isNarcissistic(num1))
cout << num1 << "is Narcissistic" ;
else
cout << num1 << "is no Narcissistic" ;
return 0;
}
bool isNarcissistic(int num1)
{
int x1;
int x2;
int digit;
int sum = 0;
int i = 0;
x1 = num1;
while (x1 != 0)
{
digit = x1 % 10;
x1 = x1 /10;
i++;
}
x2 = num1;
while (x2 != 0)
{
digit = x2 % 10;
x2 = x2/10;
sum = sum + pow(digit,i);
}
if (sum == num1)
return true ;
else
return false ;
}
Nov 30, 2013 at 10:07pm UTC
Nov 30, 2013 at 10:23pm UTC
oh thnks!
i just wanted to ask why did u multiply the digit by 1.0 ?
Nov 30, 2013 at 10:29pm UTC
I just wanted to make it double. pow function is defined as double pow(double base, double exponent);
and codepad site does not like warnings
Nov 30, 2013 at 10:54pm UTC
oh ok, thnk u :)
Topic archived. No new replies allowed.