0% found this document useful (0 votes)
25 views20 pages

Switch Cases

The document contains examples of using switch case statements to implement simple calculator, voting eligibility, days in months, roman numerals, greater of three values, profit/loss calculation, temperature conversion, basic polygon identification, login validation, and area and perimeter calculation for basic shapes like circle, rectangle, square and triangle.

Uploaded by

ragulmech362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Switch Cases

The document contains examples of using switch case statements to implement simple calculator, voting eligibility, days in months, roman numerals, greater of three values, profit/loss calculation, temperature conversion, basic polygon identification, login validation, and area and perimeter calculation for basic shapes like circle, rectangle, square and triangle.

Uploaded by

ragulmech362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

SWITCH CASE STATEMENTS:

SIMPLE CALCULATOR:
#include <stdio.h>

int main() {

float a,b,c;

char op;

printf("Enter first value: ");

scanf("%f",&a);

printf("Enter second value: ");

scanf("%f",&b);

printf("Enter operation: ");

scanf(" %c",&op);

switch (op)

case '+':

printf("The sum values is %0.1f",a+b);

break;

case '-':

printf("The subtraction value is %0.1f",a-b);

break;

case '*':

c=a*b;

printf("The product value is %0.1f",c);

break;

case '/':

c=a/b;

printf("The division value is %0.2f",c);

break;

default:

printf("Invalid");
}

return 0;

OUTPUT:

Enter first value: 10

Enter second value: 20

Enter operation: /

The division value is 0.50


VOTING EXAMPLE:
#include <stdio.h>

int main() {

int a;

printf("Enter the age: ");

scanf("%d",&a);

switch(a>=18)

case 1:

switch(a>=18 && a<=22)

case 1:

printf("The person is eligible for Voting\n");

printf("The Person's Age is %d",a);

break;

case 0:

printf("The Person is eligible for voting and his/her Age is greater than 22");

break;

break;

case 0:

printf("The person age is %d\n",a);

printf("The person is under 18 and he/she is not eligible for voting\n");

break;

return 0;

}
DAYS IN MONTHS:
#include <stdio.h>

int main() {

int a;

printf("Enter the Month: ");

scanf("%d",&a);

switch(a>=1 && a<=12)

case 1:

switch (a==1||a==3||a==5||a==7||a==8||a==10||a==12)

case 1:

printf("The Month has 31 Days");

break;

case 0:

switch (a==2)

case 1:

printf("The month has 28 or 29 days");

break;

case 0:

printf("The month has 30 days");

break;

break;

case 0:

printf("Invalid Month");

break;

return 0;
}

DAYS IN MONTHS WITH LEAP YEAR:


#include <stdio.h>

int main() {

int a,b;

printf("Enter the Month: ");

scanf("%d",&a);

printf("Enter the year: ");

scanf("%d",&b);

switch(a>=1 && a<=12)

case 1:

switch (a==1||a==3||a==5||a==7||a==8||a==10||a==12)

case 1:

printf("The Month has 31 Days");

break;

case 0:

switch (a==2)

case 1:

if((b%4==0 || b%400==0)&&(b%100!=0))

printf("The month has 29 days");

else

printf("The month has 28 days");

break;

case 0:
printf("The month has 30 days");

break;

break;

case 0:

printf("Invalid Month");

break;

return 0;

OUTPUT:

Enter the Month: 02

Enter the year: 2024

The month has 29 days


BASIC SIDES :
#include <stdio.h>

int main() {

int a;

printf("Enter the sides: ");

scanf("%d",&a);

switch(a)

case 1:

printf("It is a Monogon");

break;

case 2:

printf("It is a Diagon");

break;

case 3:

printf("It is a triangle");

break;

case 4:

printf("It is a Tetragon");

break;

case 5:

printf("It is a Pentagon");

break;

case 6:

printf("It is a Hexagon");

break;

case 7:

printf("It is a Heptagon");

break;

case 8:

printf("It is a Octagon");
break;

case 9:

printf("It is a Nanogon");

break;

case 10:

printf("It is a Decagon");

break;

default:

printf("Invalid Input");

break;

return 0;

}
USER ID and PASSWORD:
#include <stdio.h>

int main() {

int a,b,c,d;

printf("User ID: ");

scanf("%d",&a);

c=1500,d=2000;

switch (a==c)

case 1:

printf("Password: ");

scanf("%d",&b);

switch (b==d)

case 1:

printf("Login Successfull");

break;

case 0:

printf("Incorrect Password");

break;

break;

case 0:

printf("Invalid User ID");

break;

return 0;

}
GREATER OF THREE VALUES:
#include <stdio.h>

int main() {

int a,b,c;

printf("Enter the first value: ");

scanf("%d",&a);

printf("Enter the second value: ");

scanf("%d",&b);

printf("Enter the third value: ");

scanf("%d",&c);

switch(a>b && a>c)

case 1:

printf("A is greater");

break;

case 0:

switch(b>c)

case 1:

printf("B is greater");

break;

case 0:

printf("C is greater");

break;

break;

return 0;

}
PROFIT OR LOSS:
#include <stdio.h>

int main() {

int a,b;

printf("Selling Price: ");

scanf("%d",&a);

printf("Cost Price: ");

scanf("%d",&b);

switch (a>b)

case 0:

switch (a==b)

case 1:

printf("No Profit or No Loss");

break;

case 0:

printf("It is Loss\n");

printf("Loss is %d",b-a);

break;

break;

case 1:

printf("It is Profit\n");

printf("Profit is %d",a-b);

break;

return 0;

}
TEMPERATURE (DEGREE – FAHREN & FAHREN – DEGREE):
#include <stdio.h>

int main() {

int a;

char d;

printf("Temperature Format: ");

scanf(" %c",&d);

switch(d)

case 'f':

printf("Enter the Temperature in dg-c: ");

scanf("%d",&a);

float b=((a*(1.8))+32);

printf("Temperature in fahrenheit is %0.1f f",b);

break;

case 'd':

printf("Enter the Temperature in F: ");

scanf("%d",&a);

float c=((a-32)*(0.56));

printf("Temperature in degree celcius is %0.1f dg-C",c);

break;

default:

printf("Invalid Temperature Format");

break;

return 0;

}
ROMAN LETTERS:
#include <stdio.h>

int main() {

int a;

printf("Enter the Number: ");

scanf("%d",&a);

switch(a)

case 1:

printf("The Roman Letter of 1 is I");

break;

case 2:

printf("The Roman Letter of 2 is II");

break;

case 3:

printf("The Roman Letter of 3 is III");

break;

case 4:

printf("The Roman Letter of 4 is IV");

break;

case 5:

printf("The Roman Letter of 5 is V");

break;

case 6:

printf("The Roman Letter of 6 is VI");

break;

case 7:

printf("The Roman Letter of 7 is VII");

break;

case 8:
printf("The Roman Letter of 8 is VIII");

break;

case 9:

printf("The Roman Letter of 9 is IX");

break;

case 10:

printf("The Roman Letter of 10 is X");

break;

case 11:

printf("The Roman Letter of 11 is XI");

break;

case 12:

printf("The Roman Letter of 12 is XII");

break;

case 13:

printf("The Roman Letter of 13 is XIII");

break;

case 14:

printf("The Roman Letter of 14 is XIV");

break;

case 15:

printf("The Roman Letter of 15 is XV");

break;

default:

printf("Invalid Input");

break;

return 0;

}
AREA and PERIMETER OF SHAPES:
#include <stdio.h>

int main() {

char shape;

printf("Enter the shape: ");

scanf("%c",&shape);

switch(shape)

case 'c':

float radius,perimeter,area;

printf("Enter the Radius of the circle: ");

scanf("%f",&radius);

perimeter=2*3.14*radius;

area=3.14*radius*radius;

printf("Perimeter of the circle is %0.1f\n",perimeter);

printf("Area of the circle is %0.1f\n",area);

break;

case 'r':

float length,breadth,Pe,Ar;

printf("Enter the Length of Rectangle: ");

scanf("%f",&length);

printf("Enter the breadth of rectangle: ");

scanf("%f",&breadth);

Pe=2*(length+breadth);

Ar=length*breadth;

printf("Perimeter of the Rectangle is %0.1f\n",Pe);

printf("Area of the Rectangle is %0.1f\n",Ar);

break;
case 's':

float side,peri,Area;

printf("Enter the side of the square: ");

scanf("%f",&side);

peri=4*side;

Area=side*side;

printf("Perimeter of the Square is %0.1f\n",peri);

printf("Area of the Square is %0.1f\n",Area);

break;

case 't':

float S,B,H,PERI,AREA;

char op;

printf("Enter the requirement(A or P): ");

scanf(" %c",&op);

switch(op)

case 'A':

printf("Enter the breadth of triangle: ");

scanf("%f",&B);

printf("Enter the Height of triangle: ");

scanf("%f",&H);

AREA=0.5*B*H;

printf("Area of the Triangle is %0.1f\n",AREA);

break;

case 'P':

printf("Enter the Side of triangle: ");

scanf("%f",&S);

PERI=3*S;

printf("Perimeter of the Square is %0.1f\n",PERI);


break;

break;

return 0;

}
AREA and PERIMETER OF SHAPES:
#include <stdio.h>

int main() {

char shape;

printf("Enter the shape: ");

scanf("%c",&shape);

switch(shape)

case 'c':

float radius,perimeter,area;

printf("Enter the Radius of the circle: ");

scanf("%f",&radius);

perimeter=2*3.14*radius;

area=3.14*radius*radius;

printf("Perimeter of the circle is %0.1f\n",perimeter);

printf("Area of the circle is %0.1f\n",area);

break;

case 'r':

float length,breadth,Pe,Ar;

printf("Enter the Length of Rectangle: ");

scanf("%f",&length);

printf("Enter the breadth of rectangle: ");

scanf("%f",&breadth);

Pe=2*(length+breadth);

Ar=length*breadth;

printf("Perimeter of the Rectangle is %0.1f\n",Pe);

printf("Area of the Rectangle is %0.1f\n",Ar);

break;
case 's':

float side,peri,Area;

printf("Enter the side of the square: ");

scanf("%f",&side);

peri=4*side;

Area=side*side;

printf("Perimeter of the Square is %0.1f\n",peri);

printf("Area of the Square is %0.1f\n",Area);

break;

case 't':

float S,B,H,PERI,AREA;

char op;

printf("Enter the requirement(A or P): ");

scanf(" %c",&op);

switch(op)

case 'A':

printf("Enter the breadth of triangle: ");

scanf("%f",&B);

printf("Enter the Height of triangle: ");

scanf("%f",&H);

AREA=0.5*B*H;

printf("Area of the Triangle is %0.1f\n",AREA);

break;

case 'P':

printf("Enter the Side of triangle: ");

scanf("%f",&S);

PERI=3*S;

printf("Perimeter of the Square is %0.1f\n",PERI);


break;

break;

return 0;

You might also like