help plzzz :"( !!!!


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
  # include <iostream>
# include <cmath>

using namespace std;

bool isNarcissistic (int num);

int main()
{
    int num;

    cout << "Enter a positive number";
    cin >> num;

    if (isNarcissistic(num))
        cout << num << "is Naracissistic";
    else
        cout << num << "is no Naracissistic";

    return 0;
}
bool isNarcissistic(int num)
{
   int num1;
   int x1;
   int x2;
   int digit;
   int sum = 0;
   int i = 0;

   x1 = num1;

   while (x1 != 0)
   {
       digit = x1 % 10;
       x1 = x1 / 100;
       i++;
   }
   x2 = num1;

   while (x2 != 0)
   {
       digit = x2 % 10;
       x2 = x2 /100;
       sum = sum + pow(digit,i);
   }
   if (sum == num1)
    return true;
   else
    return false;
}


plz help asap :"(!!!
One thing that's apparent is that you don't use the argument which is passed into your isNarcissistic() function at all. Instead of declaring a num1 variable, just use num.
Edit: Also, you're dividing by 100 all the time, which is moving the decimal place two places to the left. You should probably be dividing by 10 instead.

I messed around with your code, and my "correct" implementation ended up being very similar with a few more tweaks besides the ones I already mentioned, so you should be able to figure it out from here.
Last edited on
i did what u said but there is an error when i use num instead of num1 and i also divided by ten its the same :/ its says 153 is no narcissistic :/

# include <iostream>
# include <cmath>

using namespace std;

bool isNarcissistic (int num);

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 num)
{
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;
}
Your isNarcissistic() function never uses the value which is passed to it. Your function declaration has int num in it, but you never use it or assign it to anything. You just have this uninitialized variable called num1 all over the place. Try this first:

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
bool isNarcissistic(int num)
{
    int x1;
    int x2;
    int digit;
    int sum = 0;
    int i = 0;

    x1 = num;

    while (x1 != 0)
    {
        digit = x1 % 10;
        x1 = x1 / 10;
        i++;
    }
    x2 = num;

    while (x2 != 0)
    {
        digit = x2 % 10;
        x2 = x2 /10;
        sum = sum + pow(digit,i);
    }
    
    if (sum == num)
        return true;
    else
        return false;
}


Any other problems you might have are problems with your logic in the function.
Edit: I tested this. It works.
Last edited on
Topic archived. No new replies allowed.