perfect numbers in range of given numbers

Apr 15, 2019 at 10:27pm
Write your question here.
So I have to write a program that asks the user to enter a range of 2 numbers and print out if there are perfect numbers in that range. if there are no perfect numbers in the given range print out "there are no perfect numbers found." using 2 loops and a function to test if true or false.

I cannot get it to print out if there are no perfect numbers correctly. The different expressions of IF statements i have tried either print out "there are no perfect numbers" about 100 times or it does not print anything out at all. Do the loops need to be inside the function so they are not run through multiple times?

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

using namespace std;

bool isAfactor(int, int);

int main()
{
	int startVal, endVal, num = 0, newNum, sum;

	cout << "Enter starting integer: ";
	cin >> startVal;
	cout << endl;

	cout << "Enter ending integer: ";
	cin >> endVal;
	cout << endl;

	for (num = startVal; num <= endVal; num++)
	{
		sum = 0;
		for (newNum = 1; newNum < num; newNum++) 
		{
			if (isAfactor(num, newNum))
				sum = sum + newNum;
		}
		if (sum == num)
		{
			cout << num << " is a perfect number." << endl;
		}
                if (sum != num)
                {
                        cout << "There are no perfect numbers between " << 
                        startVal << " and " << endVal << endl;
                }
	}


	return 0;
}

bool isAfactor(int num1, int num2)
{

	if (num1 % num2 == 0)
		return true;
	else
		return false;
}
Apr 15, 2019 at 11:33pm
use a flag
1
2
3
4
5
6
7
8
bool found = false;
for (num = startVal; num <= endVal; num++)
	if (is_perfect(num)){
		cout << num << " is a perfect number." << endl;
		found = true;
	}
if (not found)
	cout << "There are no perfect numbers between " << startVal << " and " << endVal << endl;
Last edited on Apr 15, 2019 at 11:33pm
Apr 16, 2019 at 6:49am
print out if there are perfect numbers in that range.

The task does not say: "print the perfect numbers".
There either are perfect numbers in range or not. Yes or no.
Therefore, you can stop the search when you find the first perfect number:
1
2
3
4
5
6
7
8
9
10
11
12
bool found = false;
for ( num = startVal; num <= endVal; ++num ) {
  if ( is_perfect(num) ) {
    found = true;
    break;
  }
}
if (found) {
  cout << "There are\n";
} else {
  cout << "None\n";
}
Apr 16, 2019 at 9:45pm
Apologies for the misinformation in the task,
I do need the program to print out the perfect numbers in the given range, not just say "There are perfect numbers"

I was able to get the program to stop printing "there are no perfect numbers" multiple times but it seems that it is still running through the loop and printing "there are no perfect numbers" AND the perfect numbers in the range. Do i need to change the loops from for to another form? (I have to use 2 loops as a requirement)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	bool found = false;
	for (num = startVal; num <= endVal; num++)
	{
		sum = 0;
		for (newNum = 1; newNum < num; newNum++)
		{
			if (isAfactor(num, newNum)) {
				sum = sum + newNum;
				found = true;
			}
		}
		if (sum == num)
		{
			cout << num << " is a perfect number." << endl;
		}
		if (not found)
			cout << "There are no perfect numebrs between " << startVal << " and " << endVal << endl;
	}
Last edited on Apr 16, 2019 at 10:22pm
Apr 16, 2019 at 10:24pm
If you had this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool is_perfect( int X )
{
  int sum = 0;
  for (int newNum = 1; newNum < X; newNum++)
  {
    if ( isAfactor(X, newNum) )
    {
      sum = sum + newNum;
    }
  }

  return (sum == X);
}

How would you use it in your code?
Apr 16, 2019 at 11:26pm
I figured it out, that helped a lot. thank you :D
Topic archived. No new replies allowed.