So I need help on merging a program that finds perfect numbers and one that displays deficient numbers in a given range. Can anybody help me?
This is the source code for the first program.
# include <iostream>
# include <string>
using namespace std;
int main()
{
int i=1, u=1, sum=0,ctr=0;
cout << "\n\n Find the Perfect numbers and number of Perfect numbers between 1 to 1000:\n";
cout << "------------------------------------------------------------------------------\n";
cout << "The Perfect numbers are : \n";
while(i<=1000)
{
while(u<=1000)
{
if(u<i)
{
if(i%u==0 )
sum=sum+u;
}
u++;
}
if(sum==i)
{
ctr++;
cout<<i<<" is a Perfect number."<<"\n";
}
i++;
u=1; sum=0;
}
cout<<"Number of Perfect numbers between 1 to 1000 is: "<<ctr<<"\n";
}
And this is for the second program.
#include <bits/stdc++.h>
using namespace std;
int getSum(int n)
{
int sum = 0;
for (int i=1; i<=sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
sum = sum - n;
return sum;
}
bool checkDeficient(int n)
{
return (getSum(n) < n);
}
int main()
{
int n,ctr=0;
cout << "\n\n The Deficient numbers between 1 to 100 are: \n";
cout << " ------------------------------------------------\n";
for(int j=1;j<=100;j++)
{
n=j;
checkDeficient(n)? cout << n<<" ": cout << "";
if(checkDeficient(n))
{ctr++;}
}
cout << endl<<"The Total number of Deficient numbers are: "<<ctr << endl;
}
#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
void perf()
{
int i = 1, u = 1, sum = 0, ctr = 0;
cout << "\n\n Find the Perfect numbers and number of Perfect numbers between 1 to 1000:\n";
cout << "------------------------------------------------------------------------------\n";
cout << "The Perfect numbers are : \n";
while (i <= 1000)
{
while (u <= 1000)
{
if (u < i)
{
if (i % u == 0)
sum = sum + u;
}
u++;
}
if (sum == i)
{
ctr++;
cout << i << " is a Perfect number." << "\n";
}
i++;
u = 1;
sum = 0;
}
cout << "Number of Perfect numbers between 1 to 1000 is: " << ctr << "\n";
}
int getSum(int n)
{
int sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
sum = sum + i;
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
sum = sum - n;
return sum;
}
bool checkDeficient(int n)
{
return (getSum(n) < n);
}
void def()
{
int n, ctr = 0;
cout << "\n\n The Deficient numbers between 1 to 100 are: \n";
cout << " ------------------------------------------------\n";
for (int j = 1; j <= 100; j++)
{
n = j;
checkDeficient(n) ? cout << n << " " : cout << "";
if (checkDeficient(n))
{
ctr++;
}
}
cout << endl << "The Total number of Deficient numbers are: " << ctr << endl;
}
int main()
{
perf();
def();
}