In: Computer Science
A. Write a C++ with a menu (using switch) that asks the user to select one of the following choices to the user:
1. Options ‘S’ or ‘s’
2. Option ‘T’ or ‘t
3. Options ‘P’ or ‘p’
4. print in the Matrix format
B. When 1 is selected, prompt the user to enter the starting value st (int value). Use a single FOR loop to count numbers from 1 to st. When the loop is finished, find the average of those numbers.
C. When 2 is selected, prompt the user for a number num (int). Use a single WHILE loop to calculate the sum of the odd numbers between 1 and num.
D. When 3 is selected. Use a single DO-WHILE loop to promt the user to enter random (int) values and stop whenver the first negative value is entered. Then tell the user how many positive value has been entered.
E. When 4 is selected. Use NESTED FOR loops to output all the even numbers from 1 to 80 in 5 rows and 8 columns.
NB. If the user does not select one of these 4 options print " invalid selection".
#include<bits/stdc++.h>
using namespace std;
//option function
void option()
{
//prompt message for the user
cout<<"================================="<<endl;
cout<<"1. Options 'S' or 's'
"<<endl;
cout<<"2. Options 'T' or 't'
"<<endl;
cout<<"3. Options 'P' or 'p'
"<<endl;
cout<<"4. Print in the Matrix
format"<<endl;
cout<<"================================="<<endl;
}
//main function
int main()
{
//cjoice variable
char choice;
//call the option function
option();
//take input from the user
cin>>choice;
//switch statement
switch(choice)
{
//if the case is 'S'. 's' or
1
case 'S':
case 's':
case '1':
{
//initialize the
st varibale and count varibale
int
st,count=0;
//prompt to
user
cout<<"Enter the starting value : ";
//take input
from the user
cin>>st;
//for loop till
st from 1
for(int
i=1;i<=st;i++)
//increment 1 in count
count++;
//average of the
first n natural number
float average =
float(st+1)/2;
//output for the
user
cout<<"Average = "<<average<<endl;
//then
break
break;
}
//if the case is 'T' ,'t' or
2
case 'T':
case 't':
case '2':
{
//initialse the
varibal num and sum_odd
int num,sum_odd
= 0;
//message for
the user
cout<<"Enter a number : ";
//take input
from the user
cin>>num;
// while loop
till num is not 0
while(num>0)
{
//if number is odd
if(num%2==1)
//then add to sum_odd varaible
sum_odd = sum_odd+num;
//decrement in num
num--;
}
//output for the
user
cout<<"Sum
of odd "<<sum_odd<<endl;
break;
}
//if the case is 'P' ,'p' or
3
case 'P':
case 'p':
case '3':
{ //initialize the
variable
int
random;
//count as
0
int count = 0;
//do while
loop
do
{
//prompt for the user
cout<<"Enter random number : ";
//take input from the user
cin>>random;
//if number is positive then increment 1 in
count
if(random>0)
count++;
}
//terminating
condition
while(random>0);
//output for the
user
cout<<"Total positive number
"<<count<<endl;
break;
}
//for case 4
case '4':
{
//initial
k
int k = 2;
//for all
row
for(int
i=1;i<=5;i++)
{
//for all column
for(int j=1;j<=8;j++)
{
//output for user
cout<<setw(3)<<k;
//move to next even
k = k+2;
}
cout<<endl;
}
break;
}
//default case
default:
//message for
the user
cout<<"Invalid selection";
}
}
If you have any problem then let me know in comment box and if like then please rate or give a big thumbs up thank you.