Help with C++ perfect number program

  • Comp Sci
  • Thread starter gbsboard
  • Start date
  • Tags
    C++ Program
In summary: Summary: In summary, the program requires 4 functions: 1 main, 1 svr, and 2 void functions. A user can enter any number of positive integers in the main function using a loop (preferably a for loop). The program also includes a function called sum_of_divisors, which accepts an integer and returns the sum of its perfect divisors. The program then prints out the input value and a message indicating whether the number is perfect or not. The code also includes some errors that need to be fixed, such as a global variable and missing semi-colons. The main issue the user is facing is prompting for a number and not being able to run the rest of the program.
  • #1
gbsboard
4
0
Ok I've been having a lot of trouble with this program, I am also very new to C++. The program requires to have 4 functions 1 main, 1 svr, 2 void functions
any kind of loop (for loop preferred) in the main function so that a user can enter any number of positive integers in the main function. One function is called sum_of_divisors which accepts an integer and returns the sum of its perfect divisors.

The program also prints out the input value as well as a message indicating whether the number is perfect or not

Example

Enter Number you want to test: 12
Sum of Divisor = 16
12 is not a perfect number


Enter Number you want to test: 6
Sum of Divisor = 6
6 is a perfect number

So far I have as my code

Code:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//File Name: Perfect Numbers
//Author: ********
//Email Address: ***********
//Homework Number: 3
//Description: Tests input number to see whether or not they
//are perfect numbers.
//Last Changed: feb 2, 2011

#include <iostream>
using namespace std;

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: get_data
//* purpose: reads the value used 
//*
//* parameters: Num1 - the first number
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void get_data (int& num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Sum of Divisors
//* purpose:  Figures out whether what the divisors are
//* parameters: Divisor - gets the number
//*             
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *



void sum_of_divisors(int num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Results
//* purpose:  Shows the results of the function
//* parameters: Divisor - shows the perfect
//*             
//* calls:   get_data
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void show_results (int num1);

int num1;

int main()
{
	int num1;

	get_data(num1);
	while (num<=0);
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1)
	get_data(num1)
	}
	return(0);
}

void get_data(int& num1)
{
	using namespace std;
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}

void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;

void show_results(int num1)
{
	sum_of_divisors();
	cout<<" \n\nThe perfect divisors for the input number \n\n"<<
	cout<<" Number being tested:   " << num1 <<" Divisors:   " << sum_of_divisors <<endl<<endl;	
}
 
Physics news on Phys.org
  • #2
I'll just start out by pointing out that it's spelled "Function". :)

Here's some of the problems I see, without getting into the logic.
gbsboard said:
Code:
int num1;

int main()
{
	int num1;
You have a global 'num1' there, but also one in your main. You probably shouldn't have the global variable there.
gbsboard said:
Code:
	get_data(num1);
	while (num<=0);
That while loop will go on forever if num is <= 0, and will do absolutely nothing. However, you didn't declare a variable called 'num'. I'm guessing you meant for that get_data() to be inside the while loop, and to use num1.
gbsboard said:
Code:
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1)
	get_data(num1)
	}
	return(0);
}
You're missing semi-colons on both function calls in that for loop. Also, return isn't a function, so you don't need the parentheses. Doesn't hurt, but really serves no purpose here.
gbsboard said:
Code:
void get_data(int& num1)
{
	using namespace std;
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}
You don't need that using namespace std in there. It's already at the top of the program. Wouldn't think that would even compile, but maybe. Never tried putting it in the middle of a function.
gbsboard said:
Code:
void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;
You're missing a curly brace after the return statement.
gbsboard said:
Code:
void show_results(int num1)
{
	sum_of_divisors();
	cout<<" \n\nThe perfect divisors for the input number \n\n"<<
	cout<<" Number being tested:   " << num1 <<" Divisors:   " << sum_of_divisors <<endl<<endl;	
}
Those two cout lines are pretty off. You either need to replace that last << on the first cout line with a semi-colon or make it into this (assuming I didn't make a syntax mistake or something):
Code:
	cout<<" \n\nThe perfect divisors for the input number \n\n"
	      <<" Number being tested:   " << num1 <<" Divisors:   "
              << sum_of_divisors <<endl<<endl;
Anyways, that's a quick pass through to find the errors that jump out at me. Hopefully enough to get you rolling.
 
  • #3
If you want help, help us out by telling us what problems you are having with your code.

What's an svr function?
 
  • #4
ok the problem i keep getting now is that it will prompt me to get a number so i'll type in 6 and then it stops and won't run any more of the program but will let me keep typing in numbers/letters and I am still figuring out where cout so that it will say "if the sum of the divisor = num1" such as 6 because 1+2+3=6 then cout<<"6 is a perfect number" but if the sum of the divisor doesn't = num1 then 6 is not a perfect number. I have updated the source code with the corrections suggested but i still am not sure what i am supposed to do with

get_data(num1);
while (num1>=1);

I know that the highest number I could need for the program would be 8128.

Updated code
Code:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//File Name: Perfect Numbers
//Author: xxxxxx
//Email Address: xxxxxxxx
//Homework Number: 3
//Description: Tests input number to see whether or not they
//are perfect numbers.
//Last Changed: Feb 2, 2011

#include <iostream>
using namespace std;

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: get_data
//* purpose: reads the value used 
//*
//* parameters: Num1 - the first number
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void get_data (int& num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Sum of Divisors
//* purpose:  Figures out whether what the divisors are
//* parameters: Divisor - gets the number
//*             
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *
void sum_of_divisors(int num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Results
//* purpose:  Shows the results of the function
//* parameters: Divisor - shows the perfect
//*             
//* calls:   get_data
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void show_results (int num1);

int main()
{
	int num1;

	get_data(num1);
	while (num1>=1);
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1);
	get_data(num1);
	}

	return(0);
}

void get_data(int& num1)
{
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}

void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;
}

void show_results(int num1)
{
		
	cout<<" \n\nThe perfect divisors for the input number \n\n"
	      <<" Number being tested:   " << num1 <<" Divisors:   "
              << sum_of_divisors <<endl<<endl;	
	

}

My professor never really told us what a SVR function was 90% of the class figured it ment the void sum_of_divisor

and thank you for all the help I have been trying to figure out this program for a little over 8 hours and it's just making me a lot less interested in computer programming than I was.
 
Last edited:
  • #5
That semicolon at the end of your while loop makes it an infinite loop.

while(num1 >= 1);

If you have entered a value for num1 that is 1 or larger, your loop runs forever, and never goes to the statements following the loop.

You need to think more about what your main function should do, and specifically, what should be going on in main versus what should be going on in show_results() and sum_of_divisors().

One possible organization would be this:
Code:
// in main()
int num = 1;

  while (num >= 1)
  {
     show_results(num1);
     get_data(num1);
  }
  return 0;
The code for show_results calls the sum_of_divisors function, but you are not calling this function correctly - you need to have sum_of_divisors(num1), which you don't.

Also, you really should change "Funtion" to "Function" as grep suggested.
 
  • #6
In addition to what Mark said, you need to return a value from this function:
Code:
int sum_of_divisors(int num1)
{
    int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)
        if(num1 % divisor == 0)
            sum += divisor;

    return sum;
}
Right now, that sum is stuck in the function and ceases to exist once it exits. Also, note that I fixed the indentation. It makes it clear that the if statement is inside the for loop, which is very important. I would just use curly braces for clarity, myself, but that's up to you. At minimum, make sure to indent it correctly.

This also means you need to pass that sum value into show_results, which should have a signature such as:

void show_results (int num1, int sum);
 
Last edited:
  • #7
Alright i was able to get the program to function 100% perfectly need to thank you for all the help it is very much appreciated.
 
  • #8
Code:
int sum(int n){
        int s=1,d;
        for(d=2; d*d<n; d++)
                if (n%d==0)
                        s+= d + n/d;
        if (d*d==n)
                s+=d;
        return s;
}

but...
http://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers"
Code:
#include <stdio.h>
unsigned long long int s,n1;
unsigned int p,i,n;
 
int main(){
        p =2; 
        n =3;  // n== 2^p - 1
        n1=2;  // n1*n perfect number if n is prime
        do{
                s=4;                     // 1  Lucas-Lehmer test
                for(i=3; i<=p; i++)      // 2
                        s=(s*s-2) % n;   // 3
                if (s==0 || p==2)        // 4
                        printf("Mersenne prime=2^%2d-1=%10d, perfect number %20llu\n", p, n, n1*n);
                p++;
                n1=n+1;
                n=2*n+1;
        }while (p<32);
        return(0);
}
Output
Code:
Mersenne prime=2^ 2-1=         3, perfect number                    6
Mersenne prime=2^ 3-1=         7, perfect number                   28
Mersenne prime=2^ 5-1=        31, perfect number                  496
Mersenne prime=2^ 7-1=       127, perfect number                 8128
Mersenne prime=2^13-1=      8191, perfect number             33550336
Mersenne prime=2^17-1=    131071, perfect number           8589869056
Mersenne prime=2^19-1=    524287, perfect number         137438691328
Mersenne prime=2^31-1=2147483647, perfect number  2305843008139952128
 
Last edited by a moderator:

1. What is a perfect number in C++?

A perfect number in C++ is a positive integer that is equal to the sum of its proper divisors (factors excluding the number itself). For example, 6 is a perfect number because 1+2+3=6.

2. How can I check if a number is a perfect number in C++?

To check if a number is a perfect number in C++, you can use a for loop to find the sum of the proper divisors of the number and compare it to the number itself. If they are equal, then the number is a perfect number.

3. What is the largest perfect number in C++?

The largest known perfect number in C++ is 2^82,589,933-1, which has 24,862,048 digits. However, there are infinitely many perfect numbers, and it is unknown if there is a largest perfect number.

4. Can I use the C++ standard library to find perfect numbers?

No, the C++ standard library does not have a built-in function for finding perfect numbers. You will need to write your own logic or use a third-party library.

5. Is there a faster way to find perfect numbers in C++?

Yes, there are various algorithms and optimizations that can be used to find perfect numbers more efficiently in C++. One example is the Euclid-Euler theorem, which states that if 2^p-1 is a Mersenne prime, then (2^p-1)*2^(p-1) is a perfect number. This allows for a faster way to generate perfect numbers.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
755
  • Engineering and Comp Sci Homework Help
Replies
8
Views
842
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
945
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
Back
Top