How to Create a Calculator in C program?
C

How to Create a Calculator in C Program?


Calculator in C Program We will create a program of addition, subtraction, multiplication and division using the switch case. The example is shown below.

To Understand this Example, you should have Knowledge of the following C Programming topics:

How to Get Started Learning C Programming for Beginners

Create a Program of Addition, Subtraction, Multiplication and Division using a Switch Case

In this Blog we will create a program of addition, subtraction, multiplication and division using switch case in which float a, b, total, add, sub, mul, div and INT C will be taken. Then we will start SWITCH in which start case 1 will be done, last default will come then we will run the Program.

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

void main()
{
float a,b,total,add,sub,mul,div;
int c;
printf("enter your first number :-");
scanf("%f",&a);
printf("enter your second number :-");
scanf("%f",&b);

printf("1. add\n");
printf("2. sub\n");
printf("3. mul\n");
printf("4. div\n");
printf("5. all\n");

printf("please select number :-");
scanf("%d",&c);

switch (c)
{
case 1:
    total=a+b;
    printf("\ntotal of\n  a=%.f \n+ b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    break;
case 2:
    total=a-b;
    printf("\nsub of\n  a=%.f \n- b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    break;
case 3:
    total=a*b;
    printf("\nprodect of\n  a=%.f \n* b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    break;
case 4:
    total=a/b;
    printf("\ndiv of\n  a=%.f \n/ b=%.f  \n---------------\n  :-%.1f\n---------------",a,b,total);
    break;
case 5:
    total=a+b;
    printf("\ntotal of\n  a=%.f \n+ b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    total=a-b;
    printf("\nsub of\n  a=%.f \n- b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    total=a*b;
    printf("\nprodect of\n  a=%.f \n* b=%.f  \n---------------\n  :-%.f\n---------------",a,b,total);
    total=a/b;
    printf("\ndiv of\n  a=%.f \n/ b=%.f  \n---------------\n  :-%.1f\n---------------",a,b,total);
    break;
default:
    printf("please enter correct number");
    break;
}

}

Output:-


Related Posts

Leave a Reply

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