Write a C++ program to determine which numbers between 5 and 500 are perfect num
ID: 3621656 • Letter: W
Question
Write a C++ program to determine which numbers between 5 and 500 are perfect numbers. Also, determine which numbers between 10 and 300 are prime numbers. Send your output to a file Inam_nums.out. Design the output as follows:
Perfect Numbers between 5 and 500
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
6
28
etc
Prime Numbers between 10 and 300
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
11
13
etc
Program Requirements:
Use five functions for this program:
// Function prototypes.
void PrintHeading1 ();
void PrintHeading2 ();
bool IsPerfect (int);
bool IsPrime (int);
int main ()
{ .
.
.
return 0;
}
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
// Function prototypes.
void PrintHeading1 ();
void PrintHeading2 ();
bool IsPerfect (int);
bool IsPrime (int);
ofstream out;
int main()
{out.open("Inam_nums.out");
PrintHeading1 ();
for(int i=5;i<=500;i++)
if(IsPerfect (i))
out<<" "<<i<<endl;
PrintHeading2 ();
for(int i=10;i<=300;i++)
if(IsPrime (i))
out<<" "<<i<<endl;
out.close();
return 0;
}
void PrintHeading1 ()
{out<<"Perfect Numbers between 5 and 500 ";
out<<"_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ";
}
void PrintHeading2 ()
{out<<"Prime Numbers between 10 and 300 ";
out<<"_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ";
}
bool IsPerfect (int n)
{int i,sum=1;
for(i=2;i<n;i++)
if(n%i==0)
sum+=i;
if(sum==n)
return true;
else
return false;
}
bool IsPrime (int n)
{int i;
for(i=2;i<n;i++)
if(n%i==0) //if you find a factor the number isn't prime
return false;
return true; // get here only if no factors
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.