 # BEST C PROGRAM TO MAKE A CALCULATOR – 2020

BEST C PROGRAM TO MAKE A CALCULATOR – 2020

Hello buddies, are you in search how to create a calculator in C specially with four main operations i.e. addition, subtraction, multiplication, division?

If you are not aware that how a C program works (that we are giving some set of instructions and they how are operated.)
This full stuff is explained in this best video –

Do watch it for sure before starting.

If you are in difficulty that from where to learn C language then I as a 13-year-old kid will suggest you to start with a book called “LET US C “by Yashwant kanetkar.
SO, let us now begin to make a program: –

THE CODE IS: –

Guys, just copy and paste this on your code editor and comment me whether it worked or not.
EXPLANATION: –

So, let us understand this in 4 basic steps: –

1st STEP

#include<stdio.h>

• This step is basically including a library in your code. This library basically makes your “PRINTF” statement, a valid statement;
• This library enables you to print your statement on your screen or command line.

2ND STEP

int main () {

Our second step is to write a “INT MAIN ()” FUNCTION

This function is basically a container in which some of your statements are operated and then a result as an output is returned back to this container, which gets printed on our screen.

3rd STEP

“  int sum = 0, product = 1, differnce, quotient, m, n, i;

char chose, divi;”

(Initializing and declaring variables)

1. In this statement sum is declared as 0 (the reason you will read further.
2. Product, difference, quotient all are having their function as according to their name in this code (variables can vary also in terms of name)
3. Below these we have declared two variables chose, divi (just an extra variable)
4. “Chose” will be used to ask user that which task they want to perform.

4TH STEP

switch (chose) {

“ case ‘A’:

printf(“how many numbers do you want to add”);

scanf(“%d”, &m);

for (i = 1; i <= m; i++) {

printf(“enter the number”);

scanf(“%d”, &n);

sum = sum + n;

}

printf(“%d”, sum);

break;

case ‘M’:

printf(“how many numbers you want to multiply”);

scanf(“%d”, &m);

for (i = 1; i <= m; i++) {

printf(“enter the number”);

scanf(“%d”, &n);

product = product * n;

}

printf(“the product is %d”, product);

break;

case ‘S’:

printf(“enter the two numbers between which you want to find the difference”);

scanf(“%d %d”, &m, &n);

differnce = m – n;

printf(“the difference is %d”, differnce);

break;

case ‘D’:

printf(“enter the dividend and then divisor”);

scanf(“%d %d”, &m, &n);

quotient = m / n;

printf (“the quotient is %d”, quotient);

}

} ”

### (WRITING THE SWITCH STATEMENTS AFTER ASKING THE USER THAT WHICH TASK HE/SHE WANT TO PERFORM)

So after scanf the value, the chose get that value(in simple terms) and according to that the suitable CASE (the statements under that are executed) and receive an output.

So if a user enters A , addition task  gets operated where are first asking the person how many numbers he want to add and after letting that value , we are running a loop to let the person enter different value till he want and all the values are getting added to the variable assigned called “SUM=0

Actually it also took me 5 days to understand that how this sum=sum+n(where sum is 0 in first step)

So this statement work in this way that at first time the value of su is 0 then when first integer entered by user it get added to sum now this becomes sum+n so if sum is 0 and n is 3 so at first step the whole sum will become sum+n that is 3 , now when another n will be added it will become sum+n+n which whole will become a sum for the upcoming integer.

Same is now the case with multiplication where we are using product as 1 not 0 because any number multiply 0 becomes 0 that we already know , so in this way the whole cycle of multiplication also gets operated.

## WHY USING BREAK AFTER EVERYCASE?

This is because we have already read that if a case is operated first then if there are some ore below it, all gets operated and hence we don’t get a suitable result.

But in this code if a case gets operated successfully then the flow breaks down due to the break statement and it do not get operated further. Guys if you are facing any problem in this code or in understanding the code then comment me or contact me directly.

### 1 thought on “BEST C PROGRAM TO MAKE A CALCULATOR – 2020”

1. https://waterfallmagazine.com
Heya i’m for the primary time here. I came across
this board and I find It truly helpful & it helped me
out a lot. I am hoping to present something back and aid others such as
you helped me.