Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Switch case selection
Concept Learning Code Sheets

Finding the day by days of week number – switch case #1963

User gives the day number and the program outputs the day name for five days and holiday for sat. and sun.

#include <iostream>
using namespace std;
int main ( )
{
int d;
cout<<"Enter the day number of week(1-7) : ";
cin>>d;
switch (d)
{
case 1: cout <<"Monday"; break;
case 2: cout <<"Tuesday";break;
case 3: cout <<"Wednesday";break;
case 4: cout <<"Thursday";break;
case 5: cout <<"Friday" ;break;
case 6:
case 7: cout <<"HOLIDAY" ;break;
default: cout <<"Error in input";
}
return 0 ;
}

Finding the day by days of week number – if else #1968

User gives the day number and the program outputs the day name for five days and holiday for sat. and sun. using the if else logic instead of switch case

#include <iostream>
using namespace std;
int main ( )
{
int d;
cout<<"Enter the day number of week(1-7) : ";
cin>>d;
if(d==1) cout<<"Monday";
else if(d==2) cout <<"Tuesday";
else if(d==3) cout <<"Wednesday";
else if(d==4) cout <<"Thursday";
else if(d==5) cout <<"Friday";
else if(d==6 || d==7) cout <<"HOLIDAY";
else cout<<"Error in input";
return 0 ;
}

Checking if character entered is a vowel – switch-case #1973

Entering a character from console and then checking if entered character is a vowel or something else.

#include <iostream>
using namespace std;
int main ( )
{
char c;
cout<<"Enter an alphabet(a-z/A-Z) : ";
cin>>c;
switch (c)
{
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u': cout <<"It is a vowel";
break;
default: cout <<"It is not a vowel";
}
return 0 ;
}

Finding quarter of the year based on month – switch case #1975

User inputs the month code from 1 to 12 and the output indicates the quarter of the month which it falls in.

#include <iostream>
using namespace std;
int main()
{
int m;
cout<<"Enter the month number(1-12) : ";
cin>>m;
switch (m)
{
case 1: case 2: case 3:
cout <<"First quarter of year"<<endl;
cout <<"Months: Jan-Feb-Mar";
break;
case 4: case 5: case 6:
cout <<"Second quarter of year"<<endl;
cout <<"Months: Apr-May-Jun";
break;
case 7: case 8: case 9:
cout <<"Third quarter of year"<<endl;
cout <<"Months: Jul-Aug-Sep";
break;
case 10: case 11: case 12:
cout <<"Fourth quarter of year"<<endl;
cout <<"Months: Oct-Nov-Dec";
break;
default: cout <<"Invalid input";
}
return 0 ;
}

Menu creation using switch-case #1978

Creating a menu using switch case and run appropriate code piece based on menu selection.

#include<iostream>
#include<cmath>
using namespace std;
int main ( )
{
int c=0; float s;
cout<<"Enter side of square =";
cin>>s;
cout << "1. Find Area " <<endl;
cout << "2. Find Perimeter " <<endl;
cout << "3. Find Diagonal " <<endl;
cout << "4. Exit " << endl;
cout << "Enter your choice : " ;
cin >> c ;
switch (c)
{
case 1: cout << "Area = " << s*s ;
break ;
case 2: cout << "Perimeter = " << 4*s ;
break ;
case 3: cout << "Diagonal = " << s*sqrt(2);
break ;
case 4:exit(0);
default: cout << "Wrong Menu Choice" ;
break ;
}
return 0 ;
}

Finding odd even number using a switch case #2056

Finding if the given number is odd or even using a switch-case construct.

#include <iostream>
using namespace std;
int main ( )
{
int n;
cout<<"Enter an integer : ";
cin>>n;
switch(n%2)
{
case 0: cout<<"Even";break;
case 1: cout<<"Odd";break;
}
return 0 ;
}

Calculator using the switch case #2059

A program that works like a calculator for two input numbers and works for most arithmetic operations like addition, subtraction, multiplication, division etc.

#include <iostream>
using namespace std;
int main ( )
{
double a,b;
char op;
cout<<"Enter two numbers : ";
cin>>a>>b;
cout<<"Enter operator (+,-,*,/) : ";
cin>>op;
switch (op)
{
case '+': cout <<a<<"+"<<b<<"="<<a+b; break;
case '-': cout <<a<<"-"<<b<<"="<<a-b; break;
case '*': cout <<a<<"*"<<b<<"="<<a*b; break;
case '/': cout <<a<<"/"<<b<<"="<<a/b; break;
default: cout <<"Error in input";
}
return 0 ;
}
Solved Problems

Switch case with fall through example. #2371

Write the output of the following code based on the switch-case selection whe

Error checking switch-case (char-int check) #2994

Following program may compile and link well but still it may not serve the de


Code Sheets:7  Solved Problems:2 
Back