0% found this document useful (0 votes)
12 views2 pages

C Programs Using Switch Statement

The document provides examples of C programs using switch statements for different functionalities. The first program performs basic arithmetic operations based on user input, the second checks if a character is a vowel or consonant, and the third calculates the area of a shape based on user-defined dimensions and shape code. Each program includes user input handling and appropriate output display.

Uploaded by

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

C Programs Using Switch Statement

The document provides examples of C programs using switch statements for different functionalities. The first program performs basic arithmetic operations based on user input, the second checks if a character is a vowel or consonant, and the third calculates the area of a shape based on user-defined dimensions and shape code. Each program includes user input handling and appropriate output display.

Uploaded by

smirithireddy3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

switch statement Examples:

Write a program to accept two values and operator (+ - * / %). Calculate and print the result using
values as per the operator given.
#include <stdio.h>
#include <conio.h>
int main()
{
int n1, n2, result;
char opr;
printf("Enter first value : ");
scanf("%d", &n1);
printf("Enter second value : ");
scanf("%d", &n2);

printf("Enter operator (+ - * / %% ) : ");


opr = getche();

switch(opr)
{
case '+': result = n1 + n2;
break;

case '-': result = n1 - n2;


break;

case '*': result = n1 * n2;


break;

case '/': result = n1 / n2;


break;

case '%': result = n1 % n2;


break;
default: printf("\nInvalid opeator");
}
printf("\nResult : %d", result);
return 0;
}

*-*-*-*-*-*

Write a program to accept a character and print if it is vowel or consonant.


#include <stdio.h>
#include <conio.h>
int main()
{
char alpha;
printf("Enter an alphabet : ");
alpha = getche();
switch(alpha)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' : printf("\nThe character is vowel");
break;

default : printf("\nThe character is consonant");


}
return 0;
}

*-*-*-*-*
Write a program to accept two dimensions and shape code (r-rectangle / t-triangle). Calculate and
print the area as per the shape code given.
#include <stdio.h>
#include <conio.h>

int main()
{
float d1, d2, area;
char shape;
printf("Enter dimension 1 : ");
scanf("%f", &d1);
printf("Enter dimension 2 : ");
scanf("%f", &d2);
printf("Enter shape (r-rectangle / t-triangle) : ");
shape = getche();
switch(shape)
{
case 'r':
case 'R': area = d1 * d2;
break;

case 't':
case 'T': area = 1/2.0 * (d1 * d2);
break;

default : printf("\nInvalid shape code");


return 0;
}
printf("\nArea : %f", area);
return 0;
}

You might also like