Finding perfect numbers in an asked range

My class assignment is to create a program that finds perfect numbers in an asked range and im stuck on how to do that. The instructions say to use a nested loop and to write a function. I'll post the instructions below the code.

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
int startval, endval;

cout << "Enter starting interger: ";
cin >> startval;
cout << "\n\n";
cout << "Enter ending interger: ";
cin >> endval;
cout << "\n\n";

for (int x = startval; x <= endval; x++)
{
for (int y = 1; y <= (x / 2); y++)
{


}
}



system("PAUSE");
return 0;

}




Write a program that asks the user for input for two integer values: startval, and endval. The program will then find all Perfect Numbers that fall between startval and endval.

A perfect number is a number equal to the sum of all its proper divisors (divisors smaller than the number) including 1. The number 6 is the smallest perfect number; because it is the sum of its divisors 1, 2, and 3 (1+2+3 = 6). The next perfect number is 28 (28 = 1+2+4+7+14).

The search for the Perfect Number will require two loops (nested). The outer loop will step thru the range of numbers between startval and endval. The inner loop will step thru the values from 1 to (outerval/2) to determine the factors of the number.

Write and use a function called isAFactor that accepts two int args, and determines if the second number is a factor of the first. It should return a bool value.


When a number is found, print out a message:



Xxx is a Perfect Number.



If no Perfect Numbers can be found in the given range, print the message:


No Perfect Numbers found between xxxx and yyyy.

Last edited on
I'm not sure how efficient this would be, but below is my take on how it would be done.

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
vector<int> nums;
vector<int> perfect;

for (int x = startval; x <= endval; x++)
{

for (int y = 1; y <= (x / 2); y++)
{
if(x%y==0){ //is there a remainder when the number is divided by 1,2..ect
nums.push(y); //if the remainer is 0 (meaning, it's a divisor)
                                            //then push it into the vector
}

}
//here is where you need to add all the numbers in the vector together
//and then compare it to x. If it equals x, then it is a perfect number.
//could then push the number onto "perfect" vector"

x++; //goes to the next number between startval and endval

nums.clear(); //clear the vector to start fresh with the next iteration
}

//print out perfect vector to view all perfect numbers between the 2 values

system("PAUSE");
return 0;

}
Last edited on
ector<double> nums;
Why are you using doubles unsensible?
What double? I don't know what you're talkin about :P but yeah I had another window open with a guy who was working with vector doubles and I guess my mind didn't separate the two. Anyway, it's been edited. Good call.
Last edited on
Thank you for the help! i really appreciate it :). Unfortunately, if i use this method the professor might think that i cheated as we didn't learn about using vectors and i don't even know if we will learn about vectors. I definitely appreciate your help though and wish i can repay you back
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
#include<iostream>

int main(){
int start,end;
std::cout<<"enter the starting integer"<<std::endl;
std::cin>>start;

std::cout<<"enter the last integer"<<std::endl;
std::cin>>end;

for(int i=start;i<=end;i++){
int last_divisor=i/2;

//divisors of any number cannot be greater than half of  a  number but 1 is the only exception
if(i==1){
last_divisor=1;// otherwise last_divisor variable would have become 0 (1/2)
} 

int sum=0;

  for(int  j=1; j<=last_divisor ; j++ ){
      if(i%j==0){
      sum+=j;
      }
  }//inner for loop


  if(sum==i){
  std::cout<<i<<" ";
  }
 

}//outer for loop
std::cout<<" are perfect numbers "<<std::endl;
return 0;
}
Last edited on
Topic archived. No new replies allowed.