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();
}

19 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;

  2. 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.

    1. 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.

    1. When you want to print or read float value or value with decimal then use %f. For integer value use %d.

Leave a Comment

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