Need help on combining 2 programs can you please combine these

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;
}
Last edited on
Lets say you have:
1
2
3
int main() {
  int answer = 7;
}

and
1
2
3
int main() {
  int answer = 42;
}

One approach is to do in one program all the operations. It may require some adjustments:
1
2
3
4
int main() {
  int answer = 7;
  answer = 42;
}

The other is to convert each main() into regular function and then call them:
1
2
3
4
5
6
7
8
9
10
11
12
void one() {
  int answer = 7;
}

void two() {
  int answer = 42;
}

int main() {
  one();
  two();
}

I am sadly new at c++ and still can't understand some parts of it. :((
Thank you for the advice and time.
I just need them in the same sample output
Last edited on
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <cmath>
using namespace 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();
}

Topic archived. No new replies allowed.