0% found this document useful (0 votes)
35 views21 pages

C++ Assig. Section R

This document is a group assignment from Wollo University's Department of Engineering Science and Technology for a Computer Programming course focused on C++ programming exercises. It includes various programming tasks such as calculating powers, grading, summing factorials, identifying prime numbers, and creating patterns, with detailed code examples for each task. The assignment is submitted by a group of students and is due on May 20, 2025.

Uploaded by

seid23054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views21 pages

C++ Assig. Section R

This document is a group assignment from Wollo University's Department of Engineering Science and Technology for a Computer Programming course focused on C++ programming exercises. It includes various programming tasks such as calculating powers, grading, summing factorials, identifying prime numbers, and creating patterns, with detailed code examples for each task. The assignment is submitted by a group of students and is due on May 20, 2025.

Uploaded by

seid23054
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Wollo University

Department of Engineering Science and Technology

Group Assignment
Course : Computer Programming
Assignment : C++ Programming Exercise
Section : R

Submitted To:

Mr. Tefera Alagaw Woldegiworgies

(Lecturer)

1
Submitted By:

1. Mogessie Fentaw Abebe – WOUR/0445/17

2. Eyob Ayalew Bahiru – WOUR/0246/17

3. Gedefaw Ashagrie Kassie – WOUR/0281/17

4. Eyob Tekalign Bete – WOUR/0249/17

5. Befkadu Mengesha Belachew – WOUR/0362/16

6. Bandarg Fetene Workneh – WOUR/4153/16

7. Seid Ahmed Seid – WOUR/0570/17

8. Desalegn Jenber Yalew – WOUR/1174/17

9. Tewodros Dagne Worku – WOUR/0637/17

Submission Date:

May 20, 2025

2
Table of Contents Page no.

1. Program 1: Calculate 2 the power of n.................. 4

2. Program 2: Grade Calculator ................................. 5

3. Program 3: Sum of Factorial Series ....................... 7

4. Program 4: Prime Numbers (1-100) .................... 8

5. Program 5: Factors of a Number .......................... 9

6. Program 6:

- A: Sum of 1-100 (Loops) ....................................... 10

- B: Sum of Multiples of 5 (Loops) ........................ 11

- C: Sum of Even Numbers (Loops) ...................... 13

- D: Product of Odd Numbers (Loops) ................. 14

7. Program 7: Patterns

- A: Right-Angled Triangle ....................................... 17

- B: Descending Numbers ......................................... 17

- C: Pyramid of Stars ................................................. 18

- D: Diamond Pattern ..................................................19

8. Program 8: Simple Calculator ................................. 20

3
1. Write a C++ program that compute 2n where, n is input from the user/keyboard.

#include <iostream>

#include <cmath> // For pow function

using namespace std;

int main() {

int n;

cout << "Enter a number (n): ";

cin >> n;

int result = pow(2, n);

cout << "2 raised to the power of " << n << " is: " << result << endl;

return 0;

4
2. Write a C++ program that can compute the letter grade of a student
after accepting the student’s mid and final mark. The program should only
accept mid result [0-40] and final [0- 60]. If the data entered violates this
rule, the program should display that the user should enter the mark in the
specified range. The program is also expected to run until the user refuses
to continue. Use the following scale.
100 > X > 90 ----------------- > A
90 > X > 70 ------------------- > B
70 > X > 50 ------------------- > C
50 > X > 40 ------------------- > D
40 > X > 0 --------------------- > F
> 100 and < 0 -------------- > Invalid Input

#include <iostream>

using namespace std;

char getGrade(int totalMark) {

if (totalMark > 90 && totalMark <= 100) return 'A';

else if (totalMark > 70 && totalMark <= 90) return 'B';

else if (totalMark > 50 && totalMark <= 70) return 'C';

else if (totalMark > 40 && totalMark <= 50) return 'D';

else if (totalMark > 0 && totalMark <= 40) return 'F';

else return 'I'; // Invalid

int main() {

int mid, finalExam;

5
char choice;

do {

cout << "Enter mid mark (0 - 40): ";

cin >> mid;

cout << "Enter final mark (0 - 60): ";

cin >> finalExam;

if (mid < 0 || mid > 40 || finalExam < 0 || finalExam > 60) {

cout << "Invalid input! Please enter marks within the specified
range.\n";

} else {

int total = mid + finalExam;

char grade = getGrade(total);

if (grade == 'I')

cout << "Invalid total mark!\n";

else

cout << "Total Mark: " << total << " --> Grade: " << grade << endl;

cout << "Do you want to continue? (Y/N): ";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

cout << "Program ended." << endl;

return 0;

6
3. Write a C++ code that computes the sum of the following series.
Sum = 1! + 2! + 3! + 4! + …n!

Note: The program should accept the number from the user.

#include <iostream>
using namespace std;
// Function to compute factorial of a number
long long factorial(int num) {
long long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
int main() {
int n;
long long sum = 0;
cout << "Enter a positive integer (n): ";
cin >> n;
if (n < 1) {
cout << "Please enter a positive integer greater than 0." << endl;
return 1;
}
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
cout << "The sum of the series 1! + 2! + ... + " << n << "! is: " << sum
<< endl;
return 0;
}

7
4. A prime number is an integer greater than one and divisible only by itself and one. The first
seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the prime
numbers between 1 and 100.

#include <iostream>

using namespace std;

// Function to check if a number is prime

bool isPrime(int num) {

if (num <= 1)

return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0)

return false;

return true;

int main() {

cout << "Prime numbers between 1 and 100 are:\n";

for (int i = 1; i <= 100; i++) {

if (isPrime(i)) {

cout << i << " ";

cout << endl;

return 0;

8
5. Write a C++ program that accept a number and display the factor of that number.

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter a positive integer: ";

cin >> num;

if (num <= 0) {

cout << "Please enter a positive number greater than 0." << endl;

return 1;

cout << "Factors of " << num << " are: ";

for (int i = 1; i <= num; i++) {

if (num % i == 0) {

cout << i << " ";

cout << endl;

return 0;

9
6. Write C++ program using for, do-while, and while statements to compute the following sums
and products.

A. 1+2+3+…+100

B. 5+10+15+…+n

C. 2+4+6+…+n

D. 1*3*5*7…*20

A.
#include <iostream>

using namespace std;

int main() {

int sum;

// Using for loop

sum = 0;

for (int i = 1; i <= 100; i++) {

sum += i;

cout << "Sum using for loop: " << sum << endl;

// Using while loop

sum = 0;

int j = 1;

while (j <= 100) {

sum += j;

j++;

10
cout << "Sum using while loop: " << sum << endl;

// Using do-while loop

sum = 0;

int k = 1;

do {

sum += k;

k++;

} while (k <= 100);

cout << "Sum using do-while loop: " << sum << endl;

return 0;

B.
#include <iostream>

using namespace std;

int main() {

int n, sum;

cout << "Enter the value of n (must be a multiple of 5): ";

cin >> n;

if (n % 5 != 0 || n <= 0) {

cout << "Invalid input! n must be a positive multiple of 5." << endl;

return 1;

// Using for loop

sum = 0;

11
for (int i = 5; i <= n; i += 5) {

sum += i;

cout << "Sum using for loop: " << sum << endl;

// Using while loop

sum = 0;

int i = 5;

while (i <= n) {

sum += i;

i += 5;

cout << "Sum using while loop: " << sum << endl;

// Using do-while loop

sum = 0;

i = 5;

do {

sum += i;

i += 5;

} while (i <= n);

cout << "Sum using do-while loop: " << sum << endl;

return 0;

12
C.
#include <iostream>

using namespace std;

int main() {

int n, sum;

cout << "Enter a positive even number (n): ";

cin >> n;

if (n <= 0 || n % 2 != 0) {

cout << "Invalid input! Please enter a positive even number." << endl;

return 1;

// Using for loop

sum = 0;

for (int i = 2; i <= n; i += 2) {

sum += i;

cout << "Sum using for loop: " << sum << endl;

// Using while loop

sum = 0;

int i = 2;

while (i <= n) {

sum += i;

i += 2;

cout << "Sum using while loop: " << sum << endl;

// Using do-while loop

13
sum = 0;

i = 2;

do {

sum += i;

i += 2;

} while (i <= n);

cout << "Sum using do-while loop: " << sum << endl;

return 0;

D.
#include <iostream>

using namespace std;

int main() {

long long product;

// Using for loop

product = 1;

for (int i = 1; i < 20; i += 2) {

product *= i;

cout << "Product using for loop: " << product << endl;

// Using while loop

product = 1;

int i = 1;

while (i < 20) {

14
product *= i;

i += 2;

cout << "Product using while loop: " << product << endl;

// Using do-while loop

product = 1;

i = 1;

do {

product *= i;

i += 2;

} while (i < 20);

cout << "Product using do-while loop: " << product << endl;

return 0;

15
7. Write a C++ program that will print the following shapes.
A.
*
**
***
****
*****

B.
54321
4321
321
21
1

C.
*
***
*****
*******
*********

D.
*
***
*****
***
*

16
A.
#include <iostream>

using namespace std;

int main() {

int rows = 5; // Number of rows in the pattern

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

cout << "*";

cout << endl;

return 0;

B.
#include <iostream>

using namespace std;

int main() {

int rows = 5; // Number of rows in the pattern

for (int i = rows; i >= 1; i--) {

for (int j = i; j >= 1; j--) {

cout << j << " ";

cout << endl;

17
return 0;

C.
#include <iostream>

using namespace std;

int main() {

int rows = 5; // Number of rows in the pattern

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= 2 * i - 1; j++) { // Print odd numbers of stars

cout << "*";

cout << endl;

return 0;

18
D.
#include <iostream>

using namespace std;

int main() {

int rows = 5; // Number of rows for the maximum width

// Upper half (including the middle row)

for (int i = 1; i <= rows; i += 2) {

for (int j = 1; j <= i; j++) {

cout << "*";

cout << endl;

// Lower half

for (int i = rows - 2; i >= 1; i -= 2) {

for (int j = 1; j <= i; j++) {

cout << "*";

cout << endl;

return 0;

19
8. Develop a calculator program that computes and displays the result of a single requested
operation.

E.g. if the input is

15 * 20, then the program should display 15 * 20 equals 300

If the operator is not legal, as in the following example

24~25 then the program displays ~ is unrecognized operator

As a final example, if the denominator for a division is 0, as in the following

Input: 23/0 then the program should display the following:

23 / 0 can’t be computed: denominator is 0

#include <iostream>

#include <string>

using namespace std;

int main() {

double num1, num2;

char op;

cout << "Enter an expression (e.g., 15 * 20): ";

cin >> num1 >> op >> num2;

switch(op) {

case '+':

cout << num1 << " + " << num2 << " equals " << num1 + num2 << endl;

break;

case '-':

cout << num1 << " - " << num2 << " equals " << num1 - num2 << endl;

break;

20
case '*':

cout << num1 << " * " << num2 << " equals " << num1 * num2 << endl;

break;

case '/':

if (num2 == 0) {

cout << num1 << " / " << num2 << " can't be computed: denominator is 0"
<< endl;

} else {

cout << num1 << " / " << num2 << " equals " << num1 / num2 << endl;

break;

default:

cout << op << " is an unrecognized operator" << endl;

return 0;

21

You might also like