Question

In: Computer Science

A. Write a C++ with a menu (using switch) that asks the user to select one...

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".

Solutions

Expert Solution

#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.


Related Solutions

Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
Question #11. Write a script that uses the menu function to prompt the user to select...
Question #11. Write a script that uses the menu function to prompt the user to select the current day of the week. Then use the menu function to prompt the user to select their favorite day of the week. Print a message telling the user how many days it will be until their favorite day. The output must include the current day, the favorite day, and the number of days until the favorite day. Debug your programming by running it...
Write a C++ code to print to the user a simple menu of a fast food...
Write a C++ code to print to the user a simple menu of a fast food restaurant. You should allow the user to select his/her preferred burgers and/or drinks and you should display the final bill to the user for payment. The user should be able to select more than one item. You should use the switch statement.
In C write a program that asks the user for the dimensions of 3 sides of...
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle 1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions:...
Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT