HOW TO BUILD A CALCULATOR USING C LANGUAGE???
How To Build a Simple Calculator Using C Language
Creating a calculator in C is one of the best ways to sharpen your programming skills. In this blog, you will be getting complete instructions about how to develop a calculator in C that can perform addition, subtraction, division as well as multiplication (You can also add other methods if you like to do so...).
Starting With the project
Open your code editor for e.g.- we have used here Visual Studio Code (VSC). You can open up your preferred editor and create a new C file with the name 'calculator.c'. (You can give any name)
Code
#include <stdio.h>
int main()
{
int a , b, c;
printf("-------CALCULATOR----------");
printf("\n 1.) Addition");
printf("\n 2.) Subtraction");
printf("\n 3.) Multiplication");
printf("\n 4.) Division");
printf("\n Please Enter 1 / 2 / 3 / 4");
printf("\n Please Enter Your Choice: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter Your First Number: ");
scanf("%d",&b);
printf("Enter Your Second Number: ");
scanf("%d",&c);
printf("The Addition of %d and %d is %d",b,c,b+c);
break;
case 2:
printf("Enter Your First Number: ");
scanf("%d",&b);
printf("Enter Your Second Number: ");
scanf("%d",&c);
printf("The Subtraction of %d and %d is %d",b,c,b-c);
break;
case 3:
printf("Enter Your First Number: ");
scanf("%d",&b);
printf("Enter Your Second Number: ");
scanf("%d",&c);
printf("The Multiplication of %d and %d is %d",b,c,b*c);
break;
case 4:
printf("Enter Your First Number: ");
scanf("%d",&b);
printf("Enter Your Second Number: ");
scanf("%d",&c);
printf("The Division of %d and %d is %d",b,c,b/c);
break;
default:
printf("Invalid");
break;
}
printf("\n THANK YOU");
return 0;
}
Compiling The Code
To compile your code, open up your terminal and run:
gcc calculator.c -o calculator that is, gcc (project name with extension) -o (project name)
Running the Code
./calculator that is, ./(project name)
Example
AKS: FUN LEARN |
YouTube Video Link
Conclusion
Congratulations!!! You have successfully created a calculator in C. You can even add up more ideas in the same calculator. Experiment with it and see how you can enhance the calculator.
~ By AKS: FUN LEARN
Comments
Post a Comment