0% found this document useful (0 votes)
133 views

C++ Programming Problems

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

C++ Programming Problems

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

You might also like