0

So, I am trying to do this code, which lets the user know whether a given nr. is a perfect one or not. But I am facing some difficulties in a part of my code. I also tried to solve it with a debugger, and I always see the initial value of variable "i", which I use in my for-loop, take a huge random value for no reason at all. While for the nr. 6, you get the correct answer, for nr. 28 you don't.

And maybe this is the reason why my code doesn't work as it should

#include <iostream>

using namespace std;

int main()
{
    int a,a_half,sum_divisor=0;
    float multi=0.0;
    
    cout <<"Give number: ";
    cin >>a;
    a_half = a/2;
    
    for(int i = 1;i<=a_half;i++)
    {
        multi=a/i;
        if(int(multi)==multi)
        {
            sum_divisor+=i;
        }
    }
    
    if(sum_divisor==a)
    {
        cout << a<<" is a perfect one!"<<endl;
    }else
    {
        cout <<a<<" is not a perfect number";
    }

    return 0;
}
imbAF
  • 169
  • 5
  • 1
    You might be surprised to learn that `multi` will always be an integer, because `a/i` does not do what you think it does, so `if (int(multi) == multi)` will always be true, so this doesn't really do anything useful. As far as the initial garbage value of `i` that might be normal, if the debugger attempts to show it after it's declared but before it is formally initialized with its initial value. This happens sometimes, and it does not necesarily mean that in of itself there's a problem. The only apparent problem here is the broken logic. – Sam Varshavchik Apr 12 '22 at 22:50
  • why doesn't? Do I need to add a dot ? – imbAF Apr 12 '22 at 22:52
  • I mean, I am dividing a number until half of it's value. If the result of the division is an integer, I sum the corresponding "i" value. Then in the end I check whether the sum is equal to the value of the nr. given. Why is the logic broken? All i need to do is, filter out the decimal numbers. – imbAF Apr 12 '22 at 22:55
  • @SamVarshavchik I simply declared the given variable as a float, and it works now! Thanks – imbAF Apr 12 '22 at 22:57
  • 1
    If I wasn't clear: the result of the shown division will always be an integer. If `a` is 7 and `i` is `2`, the result of the division is `3`. That's how integer division works in C++. You say you used a debugger, it should've made this pretty clear. If you looked at the values of `a`, `i`, and `multi`, simultaneously, didn't you see this? – Sam Varshavchik Apr 12 '22 at 22:58
  • The debugger just gives me the final result of the loop, not each iteration. Anyway I fixed the whole problem, by making "a" a float type – imbAF Apr 12 '22 at 23:02
  • 1
    Debugger lets you go through the code step by step, with as much details as you wish for. "Step over" will likely skip the loop, but "step into" should get you inside the loop. You can also place a breakpoint inside the loop and "continue" running to hit the breakpoint over and over. – Yksisarvinen Apr 12 '22 at 23:07
  • 1
    Also, the solution is to not use floating point numbers here, (because [they may not behave as you think](https://stackoverflow.com/q/588004/7976805)), but a modulo operator to get division remainder . When `26 / 4` gives you `6`, `26 % 4` gives you `2` (because `6 * 4 + 2 == 26`). If a number `i` is a divisor of number `a`, `a % i == 0` (no remaider). – Yksisarvinen Apr 12 '22 at 23:11

0 Answers0