Injibara University
College of Engineering and Technology
Department of Software Engineering
Computer Programming (CoSc1012) Group Assignment
Group Four
Group Members Id
1, Hanna Yirsaw INU1601306
2, Tigist Belayneh INU1601664
3, Ermias Bishaw INU1601137
4, Abebaw Yenesew INU1600658
5, Haymanot Ebabu INU1500893
6, Tesfahun Abebe INU1601639
1) A perfect number is a positive integer that is equal to the sum of its proper divisors. A proper
divisor is a positive integer other than the number itself that divides the number evenly (i.e., no
remainder).For example, 6 is a perfect number because the sum of its proper divisors 1, 2, and 3 is
equal to 6.Eight is not a perfect number because 1 + 2 + 4 ≠ 8.Write a C++ program that accepts a
positive integer and determines whether the number is perfect. Also, display all proper divisors of the
number.Try a number between 20 and 30 and another number between 490 and 500.[4 points]
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
int sum = 0;
cout << "Proper divisors of " << num << " are: ";
for(int i = 1; i < num; i++) {
if(num % i == 0) {
sum += i;
cout << i << " ";
if(sum == num) {
cout << "\n" << num << " is a perfect number." << endl;
} else {
cout << "\n" << num << " is not a perfect number." << endl;
return 0;
}
Exampke 1:-
Enter a positive integer: 28
Proper divisors of 28 are: 1 2 4 7 14
28 is a perfect number.
Example 2:-
Enter a positive integer: 495
Proper divisors of 495 are: 1 3 5 9 11 15 33 45 55 99 165
495 is not a perfect number.
2) Write a C++ program that exchanges the values of two variables when the user enters them from
the keyboard and displays the values.[3 points]
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Before swapping a= " << a << ", b= " << b << endl;
// swap the values of a and b
temp = a;
a = b;
b = temp;
cout << "After swapping a= " << a << ", b= " << b << endl;
return 0;}
Example :-
Enter the value of a: 6
Enter the value of b: 10
Before swapping a= 6, b= 10
After swapping a= 10, b= 6
3) Write a program that accepts a positive integer from the user and displays the factorial of the given
number. You should use a recursive function called factorial() to calculate the factorial of the number.
#include <iostream>
using namespace std;
double factorial(double n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
int main() {
double num;
cout << "Enter a positive integer: ";
cin >> num;
if (num < 0) {
cout << "Error: Please enter a positive integer." << endl;
} else {
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
}
return 0;
Example:-
Enter a positive integer: 7
Factorial of 7 is: 5040
4) Write a program that displays the following outputs:[3 points]
A)
#include <iostream>
using namespace std;
int main() {
// First row
cout<< " * \n";
// Second row
cout << " ** \n";
// Third row
cout << "*********\n";
// Fourth row
cout<< " ** \n";
// Fifth row
cout<< " * \n";
return 0;
}
B)
#include <iostream>
using namespace std;
int main(){
int rows = 5;
int i, j, space = 1;
space = rows - 1;
for (j = 1; j <= rows; j++) {
for (i = 1; i <= space; i++)
cout << " ";
space--;
for (i = 1; i <= 2 * j - 1; i++) {
if (i == 1 || i == 2 * j - 1)
cout << "*";
else
cout << " ";
cout << endl;
space = 1;
for (j = 1; j <= rows - 1; j++) {
for (i = 1; i <= space; i++)
cout << " ";
space++;
for (i = 1; i <= 2 * (rows - j) - 1; i++) {
if (i == 1 || i == 2 * (rows - j) - 1)
cout << "*";
else
cout << " ";
cout << endl;
return 0;
5) Write a program in C++ to display a pattern for n number of rows using numbers.Odd numbers will
appear in each row.The first and last number of each row will be 1, and the middle column will be the
row number.[2 points]
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << "1 ";
if (i > 1) {
for (int j = 1; j <= i-2; j++) {
cout << i << " ";
}
cout << "1" << endl;
return 0;
Exampke:-
Enter the number of rows: 5
11
11
131
1441
15551