0

I'm supposed to write a small code that generates a random number between 2 and 100 and then checks if its a perfect number. I don't get any bugs from the compiler but the output doesn't show my cout lines like " x is a perfect number". Can someone help me with finding the reason?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

bool PerfectCheck(int x) {

    int t; // divisor

    int sum; //  sum of divisors

    for (t = 0; t <= x; t++ ) {
        if (x%t == 0) {
            sum = sum + t;
        }
    }
    if (sum == x)
        return true;
    else
        return false;
}

int main() {

    srand(time(NULL));

    int x = rand()%(100-2+1)+2;

    if (PerfectCheck(x) == true )
        cout << x << "is a perfect number.";
    else
        cout << x << "is not a perfect number.";
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

1

There are three problems in your PerfectCheck function. First, you don't initialize the sum variable, so it can start off with any value whatsoever; you should set it to zero explicitly in the declaration.

Second, your for loop starts with t as zero, and you check x % t. The modulo operator implicitly involves a division, so using a left-hand value of zero has the same issue as trying to divide by zero, which will cause a crash.

Third, when checking for a perfect number, don't use that number itself in the sum; so, stop your loop with t < x instead of t <= x.

Also, as mentioned in the comments, you can simplify the return statement. The code below works, in the tests I have performed:

bool PerfectCheck(int x)
{
    int sum = 0; //  sum of divisors - MUST BE INITIALIZED (to zero)
    for (int t = 1; t < x; t++) { // DO NOT INCLUDE X ITSELF!!
        if (x % t == 0) {
            sum += t;
        }
    }
    return (sum == x); // This will already give a "true" or "false" bool value.
}

You could actually make the code a tiny bit more 'efficient', by initializing sum to 1 and then starting the loop from t = 2 (all numbers divide by 1, after all).

Feel free to ask for further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Even after doing that, no numbers in 100 iterations (used for loop to execute the code 100 times) shows 'perfect number'. I was coding what exactly you've coded. But the program just shows *Not a perfect number*. – Rohan Bari May 29 '20 at 07:20
  • My mistake - sorry! See edit: Don't include the number itself in the test/summation. – Adrian Mole May 29 '20 at 07:23
  • Thank you so much. the division by 0 was indeed the problem. – Niklas Klein May 29 '20 at 07:27
1

if (x%t == 0) calculates the remainder after dividing x by t and compares the result with 0. That statement is correct, but look at the previous code for (t = 0; t <= x; t++ ) {. The first value of t is 0, so you have a division by zero. More than likely that is crashing your program which is why you see no output. Change for (t = 0; t <= x; t++ ) { to for (t = 1; t <= x; t++ ) {.

Also I believe the definition of a perfect number does not include division by the number itself. So the for loop should actually be for (t = 1; t < x; t++ ) {.

Finally you are using the sum variable to add up the divisors but you do not give it an initial value. Your code should say int sum = 0;.

Three errors in a ten line program (plus various style issues). Imagine how many errors here might be in a 5000 line program. Programming is tricky, you have to be very focused and get your code exactly right, nearly right is not good enough.

john
  • 85,011
  • 4
  • 57
  • 81