For this code i am inputing one number and trying to see if it is a perfect number

#include<iostream>
#include<cmath>
#include<iomanip>
#include<conio.h>

using namespace std;

void perfect(int);
int isfactor(int, int);
int N, i;

int main()
{
    perfect(N);
	isfactor(N, i);
}

int isfactor(int number, int divisor)
{
    if(number%divisor==0)
        return 1;
    else
        return 0;
}


void perfect(int N)
{
	int sum=0;
    cout<<"Enter an integer to see whether it is a perfect number or not: "<<endl;
    cin>>N;

    if(N>0)
    {
        for(i=1; i<=N; i++);
		{
			if(isfactor(N, i))
			{
				sum+=i;
			}
		}
		
		if(sum==N)
			cout<<N<<" is a perfect number."<<endl;
		else
			cout<<N<<" is not  perfect number."<<endl;
    }

    getch();
}

the problem is that the sum keeps coming out to be 0

You have a global variable N which you give no value, then you pass this to perfect with no declared value, then inside perfect you ask the user for a value of N.
This can't be good.
Also why call isfactor from main when it's being called from perfect.
I'm not to sure what to #include in programs but 2 out of 4 look unnecessary (not sure if the conio.h is for getch(),probably).

Hello salty11,

I do not really get how your program is finding if a number is perfect or not by adding its factors. It does not work.

For ex. 25 is a perfect number however it's factors are, 1 + 3 + 5 + 25 = 34.

In your program, you are equating the sum (34) to N (25) and if they are equal, then it is a perfect number. It will be false while 25 is a perfect number.


Now about your program...

The reason the sum always equals 0 is because of this line in your perfect() function:

for(i=1; i<=N; i++);

When you put a semicolon at the end of the for loop, it does not run. Take it off.

Now regarding the sum of the factors, I do not get the logic behind it so I wrote this program:

#include <iostream>
#include <cmath>

using namespace std;

void perfect(int);
int isfactor(int, int);
int N, i;

int main()
{
    perfect(N);
}

int isfactor(int number, int divisor)
{
    //if(number%divisor==0)
	if ((divisor * divisor) == number)
        return 1;
    else
        return 0;
}


void perfect(int N)
{
    int sum=0;
    cout<<"Enter an integer to see whether it is a perfect number or not: ";
    cin>>N;

    if(N>0)
    {
        for(i=1; i<=N; i++)
		{
			if(isfactor(N, i))
			{
				cout << N << " is a perfect number." << endl;
				break;
			}
			if (i == N) cout<<N<<" is not  perfect number."<<endl;
		}
		
		/*if(sum==N)
			cout<<N<<" is a perfect number."<<endl;
		else
			cout<<N<<" is not  perfect number."<<endl;*/
    }
}
commented: 25 IS NOT a perfect number +0

wrong.
see this.
How is 3 a factor of 25.
@the OP ignore this nonsense.

Hello salty11,
There is a semicolon after for loop. You have terminated the loop.

for(i=1; i<=N; i++);

You gave i<=N in the for loop. If we take 6 then 1,2,3,6 are all added with sum rather than 1,2,3. That's why you get the wrong output.

THANK YOU!!
i made that stupid little mistake of having the ; after my for statment, i got rid of that and also got rid of the extra isfactor in the int main
also i changed the i<=N to i<N, because ya it shouldnt count the N as part of it
AND IT WORKS!!
i guess it was just those little things that messed up

To pathik raval, "a perfect number is a positive integer that is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself."
-Wikipedia
i was confused about what a perfect number was in the beginning too lol

//purpose: to find out if input is a perfect number
#include<iostream>
#include<cmath>
#include<iomanip>
#include<conio.h>
using namespace std;
//functions
void perfect(int);//to output if perfect
int isfactor(int, int);//to calculate factors
void print_factors(int);//display factors
int N, i;//variables

int main()//main function
{
	perfect(N);
}

int isfactor(int number, int divisor)
{
	if(number%divisor==0)
		return 1;//if factor of N
	else
		return 0;//if not factor of N
}

void perfect(int N)
{
	int sum=0;//initialize to zero

	for(N=1; N<100; N++)
	{
		for(i=1; i<N; i++)//divisor
		{
			if(isfactor(N, i))
			{
				sum+=i;//sum of factors
			}
		}
		if(sum==N)//if perfect number
		{
			print_factors(N);//display factors if N is perfect number
			cout<<"="<<N<<endl;
			cout<<N<<" is a perfect number."<<endl;
		}
	}

	getch();//output to screen
}//end program

void print_factors(int N)
{
	int divisor=1;
	int count=2;
	while(divisor!=N)
	{
		cout<<count-1<<"+";
		divisor=divisor+count;
		++count;
	}
	cout<<count-1;
}

well i made these changes and it before the for statment it worked when only one input was put in, but now with the

for(N=1; N<100; N++)

it doesnt seem to work, i believe its inputting the correct values but its just not being entered into the rest of the program the right way?
i cant seem to spot the problem though???

woops actually i tried it a new way , cause the for stament needed to be in int main and use a different variable, but not it works so no worries :)

Sorry, I was mixing a perfect number with a perfect square;

That is why the program I wrote checks if a number is a perfect square.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.