Question

In: Computer Science

C++ Write a program using switch-case-break that will take an input number as for example 2...

C++

Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.

Solutions

Expert Solution

#include<iostream>
using namespace std;
int main()
{
   int n=0;
   //takes input number and provides various powers of 2
   cout<<"Enter: 1:for 2^1\n2:for 2^2\n3:for 2^3\n4:for 2^4\n5:for 2^5:\nEnter your choice:";
   cin>>n;
   // switch-case-break
   switch(n){
       case 1:
           cout<<2<<endl;
           break;
       case 2:
           cout<<4<<endl;
           break;
       case 3:
           cout<<8<<endl;
           break;
       case 4:
           cout<<16<<endl;
           break;
       case 5:
           cout<<32<<endl;
           break;
       default:
           cout<<"Invalid choice\n";
      
   }
   return 0;
}

output:

Enter: 1:for 2^1
2:for 2^2
3:for 2^3
4:for 2^4
5:for 2^5:
Enter your choice:3
8


Process exited normally.
Press any key to continue . . .


Related Solutions

Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
3. Write a program using switch-case statements that accepts an integer number between 0 and 100...
3. Write a program using switch-case statements that accepts an integer number between 0 and 100 and, based on its value, reports its equivalent letter grade (A, B, C, D, or F).
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...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
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...
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
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 ( (+, -, * , /) .
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT