How Does a Simple Calculator Works:
First of all you have to know that how a simple calculator works. Simple calculator means that it can perform basic mathematical operations like summation , subtraction , multiplication and division.Now there are four operations and all that you have to know is the common thing between these mathematical operations.In all these operations , calculator take three values from the user and shows the result according to operation you defined like summation etc.Three values means two numbers and mathematical operation.
simple calculator
Explanation of Program Code:
Now you know that how a simple calculator works.While writing the program code, you should know that you have to take care of every thing involve in the process.First thing you have to do is to include libraries for input and output streams used in C language like stdio.h and so on.Then you to go for the basic function used in this language which is the main function of type integer.
The Second step is to think you are required to define in case any values.If you think over the calculator how it works then you definitely know there must be variables to store the values.For two numbers , mathematical operation and output values, variables should be defined.For numbers , integer type variables you need in case of integer values.For mathematical operation as it is character so type must be character type.You can see this in the variable initialization section of program code.
Now other things are easy , printf statement is used to display some statements and scanf is to get or take the values and stored in the variables you selected for them.Now how my program knows,for which mathematical operator , it use or apply which formula.You can done this in either using if statement or using switch statement. I used switch statement which performs the operation and apply formula according to mathematical operation , in order to learn the concept of switch statement read this article , this article is written for c++ language but the basics is same.
Recommended Article:
Program Code:#include<conio.h>
#include<stdio.h>
int main()
{
//variables initialization
int num1, num2, result;
char sign , redo;
//mathematical operation from user
printf("Enter Mathematical Operation\n ");
scanf("%c",&sign);
//first number from user
printf("Enter First Number\n ");
scanf("%d",&num1);
//second number from user
printf("Enter Second Number \n ");
scanf("%d",&num2);
//program code
// operation checking place
switch(sign)
{
// for addition in case of operation +
case '+':
result = num1+num2 ;
printf("Summation of two numbers: %d ", result);
break;
// for subtraction in case of operation -
case '-':
// for postive result from subtraction
if(num1>num2)
{
result = num1-num2 ;
printf("Subtraction of two numbers is: %d",result);
}
else
{
result = num2-num1 ;
printf("Subtraction of two numbers is: %d",result);
}
break;
// for multiplication in case of operation *
case '*':
result = num1*num2;
printf("Multiplication of two numbers: %d ",result);
break;
// for division in case of operation /
case '/':
result = num1/num2;
printf("Division of two numbers is: %d",result);
break;
default:
printf("invalid operation \n ");
}
getch();
}
Output: After compile and run , the output will be ....
Hope that you will find this helpful.
Nice sharing for the beginners.
ReplyDeletePerhaps you can design another calculator which is more generalized in the sense that user can enter a complete equation in one go and have the result.
For example
User Input = 5 + 6 / 2 x 3 - 4
Output = 10
(using DMAS rule)
This comment has been removed by the author.
Deleteyou can give example
DeleteCalculator program in C
ReplyDeleteIn C language we can design a program to add, subtract, multiply, divide any number, these all operation you can perform by using switch case.
A C program to make calculator using switch case statement is very nice program to understand the decision making statements and arithmetic operators.
ReplyDelete