You are on page 1of 23

To select the code please click the hyperlink embedded in code snippets .Thank you!

1. Write for, do-while, and while statements to compute the following sums and Products.

a. 1+2+3+…+100

Using For loop 1

#include <iostream>

using namespace std;

int main()

int total{};

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

total+=i;

cout<<total<<endl;

return 0;

Using Do while loop1

#include <iostream>

using namespace std;

int main(){
int i{},total{};

do{

i++;

total+=i;

}while(i<100);

cout<<total<<endl;

return 0;

Using While loop2

#include <iostream>

using namespace std;

int main(){

int i{},total{};

while(i < 100){

i++;

total+=i;

cout<<total<<endl;

return 0;

The output of total should be 5050.


b, 5+10+15+…+50 3

3Using For loop3

#include <iostream>

using namespace std;

int main()

int total{};

for(int i{5}; i<= 50; i++){

if(i % 5 == 0){

total+=i;

cout<<total<<endl;

return 0;

Using Do while loop

#include <iostream>

Using namespace std;

Int main()

Int i{4}, total{};


do{

i++;

if(i % 5 == 0){

Total +=i;

}While(i<50);

Cout<<total<<endl;

return 0;

Using while loop4

#include <iostream>

using namespace std;

int main()

int i{5}, total{};

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

5Using For loop

#include <iostream>

using namespace std;

int main()

double total{};

for(double i{2}; i <= 15; i++){

total += 1/i;

cout<<total<<endl;

return 0;

Using Do while loop5

#include <iostream>

using namespace std;

int main(){

double i{2},total{};
do{

total+=1/i;

i++;

}while(i<=15);

cout<<total<<endl;

return 0;

Using while loop6

#include <iostream>

using namespace std;

int main()

double i{2},total{};

while(i<=15){

total+=1/i;

i++;

cout<<total<<endl;

return 0;

D, 1*2*3*…*20

Using For loop6


#include <iostream>

Using namespace std;

int main()

unsigned long long int total{1};

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

total*=i;

Cout<<total<<endl;

return 0;

Using Do while loop7

#include <iostream>

using namespace std;

int main()

unsigned long long int total{1};

int i{1};

do{

total*=i;

i++;

}while(i<=20);

cout<<total<<endl;
return 0;

Using While loop8

#include <iostream>

using namespace std;

int main()

int i{1};

unsigned long long int total{1};

while(i <= 20){

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>

using namespace std;

int main()

int inc{};

for(int i{10}; i<= 49; i++){

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

numbers between 1 and 100.9


10#include <iostream>

using namespace std;

int main()

int limit{100};

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

for(int number = 2; number <=limit; ++number){

bool isPrimeFlag = true;

for(int i = 2; i <= (number/2); ++i){

if(number%i == 0 ){

isPrimeFlag = false; break; }

if (isPrimeFlag == true) cout << number << " " ;

return 0;

4. Write a C++ program that counts the number of digits in an integer number. For example;

23,498 has five digits10

#include <iostream>
using namespace std;

int main()

int num{23498};

int amount{};

for(int i{1}; num > 0; i++){

if(num % 10 !=0){

amount++;

num = num / 10;

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()

int mid_mark{}, final_mark{}, total{};

string mid_msg {"\nEnter your mid exam result:"};

string final_msg{"\nEnter your final exam result:"};

while(true){

cout<<mid_msg;

cin>>mid_mark;

if(mid_mark > 0 && mid_mark <= 40){

while(true){

cout<<final_msg;

cin>>final_mark;

if(final_mark > 0 && final_mark <=60){

total = mid_mark + final_mark;

cout<<"\nYour grade is: ";

if(total <= 100 && total > 95)

cout<<"A"<<endl;

else if(total < 95 && total > 85)

cout<<"B"<<endl;

else if(total < 85 && total > 75)

cout<<"C"<<endl;
else if(total < 75 && total > 65)

cout<<"D"<<endl;

else

cout<<"F"<<endl;

break;

else

cout<<"Invalid final mark"<<endl;

else

cout<<"Invalid mid exam"<<endl;

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>

using namespace std;

int main()

{
int num{5}, fact{1};

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

fact*=i;

cout<<"The factorial of "<<num<<" is "<<fact<<endl;

return 0;

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 the user.14

#include <iostream>

using namespace std;

int find_fact(int num){

int fact{1};

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

fact*=i;

return fact;

int main()

int num{5}, sum{};


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

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

be 65, the value of ASCII “A”).

15

AB

ABC

ABCD

ABCDE

#include <iostream>

using namespace std;

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>

using namespace std;

int main()

char a{'a'};

for(int i{1}; i<=6; i++){


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

cout<<a;

a++;

cout<<endl;

return 0;

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

A.

* * *

** *** ***

*** ***** *****

**** ******* ***

***** ********* *

A,17

#include <iostream>

using namespace std;

int main()

{
for(int i{1}; i<=5; i++){

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

cout<<"*";

cout<<endl;

return 0;

B,

#include <iostream>

using namespace std;

int main() {

int space{}, rows{5};

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

for(space = 1; space <= rows-i; ++space) {

cout <<" ";

while(k != 2*i-1) {

cout << "* ";

++k;

cout << endl;

}
return 0;

C,

#include<iostream>

using namespace std;

int main()

int i, j, rowNum{5}, space;

space = rowNum-1;

for(i=1; i<=rowNum; i++)

for(j=1; j<=space; j++)

cout<<" ";

space--;

for(j=1; j<=(2*i-1); j++)

cout<<"*";

cout<<endl;

space = 1;

for(i=1; i<=(rowNum-1); i++)

for(j=1; j<=space; j++)

cout<<" ";

space++;

for(j=1; j<=(2*(rowNum-i)-1); j++)


cout<<"*";

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>

using namespace std;

int main()

int students{5},point{},total{},average;

vector <int> marks{};

for(int i{1}; i<=students; i++){

cout<<"Enter the mark for student "<<i<<" :";

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>

using namespace std;

int main() {

char c;

bool isLowercaseVowel, isUppercaseVowel;

cout << "Enter an alphabet: ";

cin >> c;

isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (!isalpha(c))

printf("Error! Non-alphabetic character.");

else if (isLowercaseVowel || isUppercaseVowel)

cout << c << " is a vowel.";


else

cout << c << " is a consonant.";

return 0;

13. Write a C++ code to display only even numbers found between 0 and 2022

#include <iostream>

Using namespace std;

int main()

for( int i{1}; i<20; i++){

if ( i%2==0){

Cout<<i<<endl;

return 0;

You might also like