I have to turn in a project in my beginer C++ class in two days time and I am totally stuck. The project is to promt a user for an integer (between 1>=num<1000); Tell if that number is a prime number ( if not find a way to show the divisors of the number); and then if the number is prime,decide if it is a perfect number or not (once again showing the divisors that prove the number is perfect.
My teacher treats us like we rogram already and left me in a fog... I have very limited info on for-loops which is how we are to solve this program.
so far i have to find if a number is prime or not...
#include <iostream>
#include <string>
using namespace std;
int main()
{
double num;
bool prime;
cout<<"This is my program check if a number is a Prime number, also a Perfect Number!"<<endl;
cout<<"Enter a number between 1 and 1000 "<<endl;
cin>>num;
if ((num<=0)||(num>1000))//SETTING UP THE FIRST ERROR MESSAGE
{
cout<<"Error, Please re-enter a valid number between 1 and 1000!"<<endl;
}
//NOW I AM GOING TO START THE MATH!
for (int i = 3; i <= num; i++){
prime=true;
for (int num = 2; num <= i - 1; num++){
if (i%num==0){
prime=false;
}
}
if (prime){
cout<<num<<" is a Prime number!"<<endl;
}
}
return 0;
}//End of Program
This just list out the prime numbers though... I cannot figure out how to make it show the divisors for the number....
an for perfect numbers is it
for (int i=1; i <= ( n / 2 ): i++ )
if (num%i==0)
product += i;
prime=true;
I am trying but I am afraid I am out to sea with no paddle...Teacher wont provide insiht, So I turn to you for some direction and guidance.
Thank You
D