0% found this document useful (0 votes)
84 views8 pages

C++ Programming Exercises and Solutions

This document contains a group assignment for a Fundamentals of Programming with C++ course. It lists the names and student IDs of 6 students and includes 10 programming problems to solve using C++. The problems cover topics like loops, prime numbers, factorials, and printing shapes using ASCII values. The assignment is to be submitted to the instructor Mr. Misganu T. by July 30, 2021 in Nekemte, Ethiopia.

Uploaded by

Duol Koang Thong
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)
84 views8 pages

C++ Programming Exercises and Solutions

This document contains a group assignment for a Fundamentals of Programming with C++ course. It lists the names and student IDs of 6 students and includes 10 programming problems to solve using C++. The problems cover topics like loops, prime numbers, factorials, and printing shapes using ASCII values. The assignment is to be submitted to the instructor Mr. Misganu T. by July 30, 2021 in Nekemte, Ethiopia.

Uploaded by

Duol Koang Thong
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

WOLLEGA UNIVERSITY

(Icon of Quality and Relevance)

College of Engineering and Technology


Department of Computer Science
Course Title: Fundamental of Programming with C++
Course code: CoSc2015

Group Assignment
S/no Full Name ID Number
1 Duol Koang 1201489
2 Bol Michael 1205702
3 Degaga Girma 1201302
4 Meti Girma 1203956
5 Solomon Aga 1204993
6 Adisu Terfu 1208761

Submitted to: Mr. Misganu T. July 30, 2021


Nekemte, Ethiopia
1. Write for, do-while, and while statements to compute the following sums
and products.
a. 1+2+3+…+100

//For statement //Do...while statement //While statement


#include<iostream.h> #include<iostream.h> #include<iostream.h>
void main() void main() void main()
{ { {
int sum=0; int i=1,sum=0; int i=1,sum=0;
for(int i=0;i<=100;i++) do { while(i<=100)
sum=sum+i; sum=sum+i; {
{ i++; sum=sum+i;
cout<<"The result is: while(i<=100); i++;
"<<sum; }
} cout<<sum; cout<<sum;
} }

2. Write an application to print out the numbers 10 through 49 in the following


manner
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

#include<iostream.h>
void main()
{
for(int i=10;i<=49;i++)
{
cout<<i<<" ";
if(i%10==9)
{
cout<<endl;
}
}
}

1|B y D uol K. Tho ng


3. 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.h>
void main()
{
int i,j,prime;
for(i=2; i<100; i++)
{
prime=0;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
prime=1;
break;
}
}
if(prime==0)
cout<<i<<", ";
}
}

4. Write a C++ program that counts the number of digits in an integer number.
For example; 23,498 has five digits.
#include<iostream.h>
void main()
{
int num,x;
int count=0;
cout<<"Enter any number:";
cin>>num;
x=num;
while(x!=0)
{
count++;
x/=10;
}
cout<<num<<" has "<<count<<" digits.";
}
2|B y D uol K. Tho ng
5. Write a C++ application 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.
#include<iostream,h>
void main()
{
int mid, final;
cout<<"Enter your mark from mid:";
cin>>mid;
while(mid>40 || mid<0)
{
cout<<"Enter the mark in specific range!\n";
return(0);
}
cout<<"Enter your mark from final:";
cin>>final;
while(final>60 || final<0)
{
cout<<"Enter the mark in specific range!\n";
return(0);
}
int sum=mid+final;
if(sum>=90 && sum<=100)
{
cout<<"Your grade is: A";
}
else if(sum>=80 && sum<90)
{
cout<<"Your grade is: B";
}
else if(sum>=70 && sum<80)
{
cout<<"Your grade is: C";
}
else if(sum>=60 && sum<70)
{
cout<<"Your grade is: D";
}
3|B y D uol K. Tho ng
else
{
cout<<"Your grade is: F";
}
}

6. Write a C++ program that accepts a positive number from the user and
displays the factorial of that number. Use for loops to find the factorial of the
number.
#include<iostream.h>
void main()
{
int number,fact=1;

cout <<"Enter the number:";


cin>>number;

for(int i=number; i>=1; i--)


{
fact=fact*i;
}
cout<<"The factorial of "<<number<<" is:"<<fact;
}
}

7. Write a C++ code that computes the sum of the following series. Sum = 1! +
2! + 3! + 4! + …n!. The program should accept the number from user.
#include<iostream.h>
void main()
{
int n,fact=1,sum=0;
cout<<"Enter any number:";
cin>>n;
for(int i=1;i<=n;i++)
{
fact=fact*i;
sum=sum+fact;
}
cout<<"The sum is:"<<sum;
}
4|B y D uol K. Tho ng
8. Using the ASCII table numbers, write a program to print the following
output, using a nested for loop. (Hint: the outer loop should loop from 1 to 5,
and the inner loop’s start variable should be 65, the value of ASCII “A”).

A
AB
ABC
ABCD
ABCDE

#include<iostream.h>
void main()
{
int i,j;
for(i='A';i<='E';i++)
{
for(j='A';j<=i;j++)
{
cout<<char(j);
}
cout<<endl;
}
}

9. Write a C++ program that displays the following output using their ASCII
values.

a
bc
def
gehi
jklmn
opqrst

#include<iostream.h>
void main()
{
char alphabet='a';
5|B y D uol K. Tho ng
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
{
cout<<alphabet++;
}
cout<<endl;
}
}

10. Write a C++ program that will print the following shapes.

A. * B. ***** C. *
** **** ***
*** *** *****
**** ** *******
***** * *********

//A //B //C


#include<iostream.h> #include<iostream.h> #include<iostream.h>
void main() void main() void main()
{ { {
int i,j; int i,j; int n=5;
for(i=1;i<=5;i++) for(i=1;i<=5;i++) int px=n;
{ { int py=n;
for(j=1;j<=i;j++) for(j=5;j>=i;j--) int i,j;
{ { for(i=1;i<=n;i++)
cout<<"*"; cout<<"*"; {
} } for(j=1;j<n*2;j++)
cout<<endl; cout<<endl; {
} } if(j>=px && j<=py)
} } {
//B cout<<"*";
#include<iostream.h> }
void main() else
{ {
int i,j; cout<<" ";
for(i=1;i<=5;i++) }
{ }
6|B y D uol K. Tho ng
for(j=5;j>=i;j--) px--;
{ py++;
cout<<"*"; cout<<endl;
} }
cout<<endl; }
}
}

7|B y D uol K. Tho ng

Common questions

Powered by AI

The program uses a series of if-else if statements to determine and assign letter grades based on the calculated total of mid and final exam scores. The conditions check if the total falls within specific ranges (90-100 for 'A', 80-89 for 'B', etc.). This structure ensures clear, organized, and easily readable code where each range corresponds to a specific letter grade, minimizing error risk in the assignment .

The C++ program demonstrates sum computation using three loop constructs: for, while, and do-while. In the for loop, it initializes a counter and performs an iteration directly inside the loop definition. The while loop initializes the counter before the loop starts and checks the condition at the start of each iteration. The do-while loop also initializes before the loop, but unlike the while loop, checks the condition after each iteration, ensuring it runs at least once. These differences highlight the flexibility of each loop type in different contexts .

In the given C++ program, two nested loops are used: the outer loop controls the number of rows while the inner loop handles the printing of characters. The character 'alphabet' starts at 'a'. For each iteration of the outer loop from 1 to 6, the inner loop runs from 1 to the current outer-loop index. During each iteration of the inner loop, the current character in 'alphabet' is printed and incremented. This setup ensures an incremental, stacked pattern of characters based on ASCII values is printed across multiple lines .

The program uses a for loop to iterate from 1 to n, where n is the user-provided input. Within each iteration, it calculates the factorial of the current number 'i' using a cumulative product stored in 'fact'. It adds this 'fact' value to a running total 'sum'. The program outputs the total sum of factorial values after the loop concludes .

The program checks the validity of the input scores by using while loops that continue prompting the user to enter a correct range until it meets the condition. For the mid-term score, it ensures the user enters a value between 0 and 40, and for the final score, between 0 and 60. If the entered score is out of these ranges, the program displays a message requesting the user to enter a score within the specified range and exits with return(0) if an invalid input is detected .

The program initializes 'fact' to 1 and uses a for loop that iterates from the input number down to 1. Within the loop, it multiplies 'fact' by the iterator value 'i'. This effectively computes the factorial by multiplying all the integers from the input down to 1, storing the result in 'fact', which is then printed as the factorial result .

The program employs nested for loops where the outer loop iterates over values from 'A' to 'E', corresponding to ASCII values 65 to 69. The inner loop runs from 'A' up to the current value of the outer loop, printing characters. This approach effectively builds rows where each row contains an incrementally larger subset of the alphabet starting from 'A', creating the desired pyramid pattern of increasing character sequences .

The program iterates over numbers starting from 2 up to just below 100. For each number 'i', it initializes a flag 'prime' to 0, implying it starts by assuming 'i' is prime. A nested for loop checks divisors from 2 up to 'i/2'. If 'i' is divisible by any of these numbers (i.e., remainder is 0), the flag 'prime' is set to 1, indicating 'i' is not a prime, and the loop breaks. If 'prime' remains 0 after exiting the loop, 'i' is considered prime and is printed .

You might also like