Professional Documents
Culture Documents
1. Write for, do-while, and while statements to compute the following sums and Products.
a. 1+2+3+…+100
#include <iostream>
int main()
int total{};
total+=i;
cout<<total<<endl;
return 0;
#include <iostream>
int main(){
int i{},total{};
do{
i++;
total+=i;
}while(i<100);
cout<<total<<endl;
return 0;
#include <iostream>
int main(){
int i{},total{};
i++;
total+=i;
cout<<total<<endl;
return 0;
#include <iostream>
int main()
int total{};
if(i % 5 == 0){
total+=i;
cout<<total<<endl;
return 0;
#include <iostream>
Int main()
i++;
if(i % 5 == 0){
Total +=i;
}While(i<50);
Cout<<total<<endl;
return 0;
#include <iostream>
int main()
while(i<=50){
if(i % 5 == 0){
total += i;
i++;
}
cout<<total<<endl;
return 0;
C, 1+1/2+1/3+1/4+…1/15
#include <iostream>
int main()
double total{};
total += 1/i;
cout<<total<<endl;
return 0;
#include <iostream>
int main(){
double i{2},total{};
do{
total+=1/i;
i++;
}while(i<=15);
cout<<total<<endl;
return 0;
#include <iostream>
int main()
double i{2},total{};
while(i<=15){
total+=1/i;
i++;
cout<<total<<endl;
return 0;
D, 1*2*3*…*20
int main()
total*=i;
Cout<<total<<endl;
return 0;
#include <iostream>
int main()
int i{1};
do{
total*=i;
i++;
}while(i<=20);
cout<<total<<endl;
return 0;
#include <iostream>
int main()
int i{1};
total *= i;
i++;
cout<<total<<endl;
return 0;
82. 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>
int main()
int inc{};
inc++;
cout<<i<<" ";
if(inc % 10 == 0){
cout<<endl;
return 0;
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
int main()
int limit{100};
cout << "The Prime numbers between 1 and 100 are : \n";
if(number%i == 0 ){
return 0;
4. Write a C++ program that counts the number of digits in an integer number. For example;
#include <iostream>
using namespace std;
int main()
int num{23498};
int amount{};
if(num % 10 !=0){
amount++;
cout<<amount<<endl;
return 0;
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 continue11
#include <iostream>
using namespace std;
int main()
while(true){
cout<<mid_msg;
cin>>mid_mark;
while(true){
cout<<final_msg;
cin>>final_mark;
cout<<"A"<<endl;
cout<<"B"<<endl;
cout<<"C"<<endl;
else if(total < 75 && total > 65)
cout<<"D"<<endl;
else
cout<<"F"<<endl;
break;
else
else
return 0;
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.13
#include <iostream>
int main()
{
int num{5}, fact{1};
fact*=i;
return 0;
7. Write a C++ code that computes the sum of the following series. Sum = 1! + 2! + 3! + 4! + …n!
#include <iostream>
int fact{1};
fact*=i;
return fact;
int main()
sum+=find_fact(i);
cout<<sum<<endl;
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
15
AB
ABC
ABCD
ABCDE
#include <iostream>
int main()
for(int i{1};i<=5;i++)
for(int j{1};j<=i;j++)
{
cout<<((char)(j+64));
cout<<endl;
return 0;
9. Write a C++ program that displays the following output using their ASCII values.16
bc
def
gehi
jklmn
opqrst
#include <iostream>
int main()
char a{'a'};
cout<<a;
a++;
cout<<endl;
return 0;
10. Write a C++ program that will print the following shapes.17
A.
* * *
** *** ***
***** ********* *
A,17
#include <iostream>
int main()
{
for(int i{1}; i<=5; i++){
cout<<"*";
cout<<endl;
return 0;
B,
#include <iostream>
int main() {
while(k != 2*i-1) {
++k;
}
return 0;
C,
#include<iostream>
int main()
space = rowNum-1;
cout<<" ";
space--;
cout<<"*";
cout<<endl;
space = 1;
cout<<" ";
space++;
cout<<endl;
cout<<endl;
return 0;
11. Write a C++ program that accepts marks of five students and then displays their average. The
program should not accept mark which is less than 0 and mark greater than 100.20
#include <iostream>
#include <vector>
int main()
int students{5},point{},total{},average;
cin>>point;
marks.push_back(point);
for(auto v: marks){
total+=v;
average=total/5;
cout<<"\nThe average is:"<<average<<endl;
return 0;
12. Use either a switch or an if-else statement and display whether a vowel or a consonant character
is entered by the user. The program should work for both lower case and upper case letters.
21
#include <iostream>
int main() {
char c;
cin >> c;
if (!isalpha(c))
return 0;
13. Write a C++ code to display only even numbers found between 0 and 2022
#include <iostream>
int main()
if ( i%2==0){
Cout<<i<<endl;
return 0;