C++ Assignment (Group 4)
C++ Assignment (Group 4)
Group Four
Group Members Id
#include <iostream>
int main() {
int num;
int sum = 0;
cout << "Proper divisors of " << num << " are: ";
if(num % i == 0) {
sum += 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:-
28 is a perfect number.
Example 2:-
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>
int main() {
int a, b, temp;
cin >> a;
cin >> b;
cout << "Before swapping a= " << a << ", b= " << b << endl;
temp = a;
a = b;
b = temp;
cout << "After swapping a= " << a << ", b= " << b << endl;
return 0;}
Example :-
Before swapping a= 6, b= 10
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>
double factorial(double n) {
if (n == 0) {
return 1;
} else {
int main() {
double num;
if (num < 0) {
} else {
cout << "Factorial of " << num << " is: " << factorial(num) << endl;
}
return 0;
Example:-
A)
#include <iostream>
int main() {
// First row
// Second row
// Third row
// Fourth row
// Fifth row
return 0;
}
B)
#include <iostream>
int main(){
int rows = 5;
int i, j, space = 1;
space = rows - 1;
space--;
if (i == 1 || i == 2 * j - 1)
else
space = 1;
space++;
else
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>
int main() {
int n;
cin >> n;
if (i > 1) {
}
cout << "1" << endl;
return 0;
Exampke:-
11
11
131
1441
15551