Sample program on switch ... case using character constants. using case without break.

#include <stdio.h>

int main(void)
{
char choice;
int n1, n2;

printf("\n\'A\'ddition\n\'S\'ubstraction\n\'M\'ultiplication\n\'D\'ivision\nChoice :");
scanf("%c", &choice);

switch(choice)
{
case 'a':
case 'A':
printf("Enter two numbers for addition :");
scanf("%d%d", &n1, &n2);
printf("\nAddition of %d and %d is %d",n1, n2, n1 + n2);
break;
case 's':
case 'S':
printf("Enter two numbers for substraction :");
scanf("%d%d", &n1, &n2);
printf("\nSubstraction of %d and %d is %d",n1, n2, n1 - n2);
break;
case 'm':
case 'M':
printf("Enter two numbers for multiplication :");
scanf("%d%d", &n1, &n2);
printf("\nMultiplication of %d and %d is %d",n1, n2, n1 * n2);
break;
case 'd':
case 'D':
printf("Enter two numbers for division :");
scanf("%d%d", &n1, &n2);
printf("\nDivision of %d and %d is %f",n1, n2, (float) n1 / n2);
break;
default :
printf("\nInvalid choice");
}

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin