C program to perform arithmetic operations using switch case

C program to perform arithmetic operations using switch case

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int ch;
float a,b,res;
clrscr();
printf(“Enter two numbers:”);
scanf(“%f%f”,&a,&b);
printf(“nMenun1.Additionn2.Subtractionn3.Multiplicationn4.Division”);
printf(“nEnter your choice:”);
scanf(“%d”,&ch);

switch(ch)
{
case 1: res=a+b;
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;
default: printf(“Wrong choice!!nPress any key…”);
getch();
exit(0);
}

printf(“nResult=%f”,res);
getch();
}

12 thoughts on “C program to perform arithmetic operations using switch case

  1. Rizan Rasheed

    to do division like this
    we have to tell that b!=0
    as following:
    case 4:if (b!=0)
    res=a/b;
    else
    printf("Division not possible");
    break;

    Reply
  2. Mariani

    Thanks. Why do we add a getch function at the end of the function. We can also freeze output on screen using scanf function. I observed this coding pattern everywhere.

    Reply
    1. Admin Post author

      Because in case of getch() the output screen is stopped until we press any key. But in case of scanf() we have to give some input and then press enter to disappear the screen. So getch() is better way to stop output screen.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *