C Language: Writing your own Basic Calculator in C 2021

How to make Calculator With C Language

For making a Calculator we have to look at a few things. What functionality we are going to add on that program . In this tutorial we are going to add basic addition , basic subtraction , basic multiplication , basic division , basic percentage calculator and a temperature calculator(celsius to fahrenheit). That’s it for this.

 

In our 2nd part we will make this calculator to do more advanced tasks. Like currency calculating , height , weight calculating and so on. So stay tuned until the 2nd part comes .

 

So now we are going to start the tutorial from here. 

 

Requirements: 

First of all let’s discuss the requirements for making the program . So first you do have a computer to compile the program, second your pc or laptop has to be capable of installing the DevC++ software. You need to install the devC++ software in your PC or Laptop. Then the last requirement is you should have basic knowledge of C programming .

 

Installation & Setup: 

The Installation and setup is described in another tutorial so if you want to install C Compiler then you can see the post . There I have Described how you can install the DevC++ or MinGW – Visual studio Extension .

link: 

 

Formule:

Addition: a+b

Subtraction: a-b

Multiplication: a*b

Division: a/b

Modulus Division: a%b

Percentage: a*b/100

Temperature: (t*9/5)+32

 

Start Coding:

So now let’s first declare all variables we need to make this project.

 

1
// Variable Declearation
        int input,add1,add2,sub1,sub2,mul1,mul2;
        float div1,div2,moddiv1,moddiv2,per1,per2,temp1,temp2;

So we have declared all the variables here. Now Let’s check which variable is for which work.

input is for taking input from the user to conduct where he wants to go for.

add1 and add2 are for Addition . sub1 and sub2 are for subtraction. mul1 and mul2 are for Multiplication.

div1 and div2 are for division. moddiv1 and moddiv2 are for the Modulus Division. per1 and per2 are for Percentage Calculation. temp1 and temp2 are for the Temperature Calculation.

Now Let’s begin in Next code:

2
// Input 
 	printf("\n1. Addition\t\t 2. Subtraction\n");
 	printf("3. Multiplication\t 4. Division\n");
 	printf("5. Modulus Division\t 6.  Percentage\t 7. Temperature\n\n");
 	printf("Enter What You want:");
 	scanf("%d",&input);

This code Will show options and take inputs from the user for the calculation he wants to do.

Now we have to make methods for each work for better code readability.

3
int Addition(a,b){
	// do the addition
}

int Subtraction(a,b){
	// do the Subtraction
}

int Multiplication(a,b){
	// do the multiplication
}

int Division(a,b){
	// do the Division
}

int ModDivision(a,b){
	// do the Mod Division
}

int Percentage(a,b){
	// do the percentage
}

float Temperature(a){
	// do the calculation
}

Now let’s add the if-else Statements for each number: we gave:

Note: This Will be under the main method.


 	// functioning
 	if(input == 1){
 		// Do some Work
	 }
	if(input == 2){
 		// Do some Work
	}
	if(input == 3){
 		// Do some Work
	}
	if(input == 4){
 		// Do some Work
	}
	if(input == 5){
 		// Do some Work
	}
	if(input == 6){
 		// Do some Work
	}
	if(input == 7){
 		// Do some Work
	}
	else{
		// Do some Work
	}

Now we will write the codes under if statements:

4
// functioning
 	if(input == 1){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&add1);
 		printf("Enter Second Number: ");
 		scanf("%d",&add2);
 		Addition(add1,add2);
	 }
	if(input == 2){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&sub1);
 		printf("Enter Second Number: ");
 		scanf("%d",&sub2);
 		Subtraction(sub1,sub2);
	}
	if(input == 3){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&mul1);
 		printf("Enter Second Number: ");
 		scanf("%d",&mul2);
 		Multiplication(mul1,mul2);
	}
	if(input == 4){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%f",&div1);
 		printf("Enter Second Number: ");
 		scanf("%f",&div2);
 		Division(div1,div2);
	}
	if(input == 5){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%f",&moddiv1);
 		printf("Enter Second Number: ");
 		scanf("%f",&moddiv2);
 		ModDivision(moddiv1,moddiv2);
	}
	if(input == 6){
 		// Do some Work
 		printf("Enter Your Total Value: ");
 		scanf("%f",&per1);
 		printf("Enter Your Percentaged Value: ");
 		scanf("%f",&per2);
 		Percentage(per1,per2);
	}
	if(input == 7){
 		// Do some Work
 		printf("Enter Celsius Temperature: ");
 		scanf("%f",&temp);
 		Temperature(temp);
	}

This is the code for separating each work or calculation factors to multiple functions.

Now we will do the final work which is calculation and show the result

// Calculation
int Addition(a,b){
	int result;
	result = a+b;
	printf("%d + %d = %d",a,b,result);
	return result;
}

int Subtraction(a,b){
	int result;
	result = a-b;
	printf("%d - %d = %d",a,b,result);
	return result;
}

int Multiplication(a,b){
	int result;
	result = a*b;
	printf("%d * %d = %d",a,b,result);
	return result;
}

int Division(a,b){
	float result;
	result = a/b;
	printf("%f / %f = %f",a,b,result);
	return 0;
}

int ModDivision(a,b){
	float result;
	result = a%b;
	printf("%f / %f = %f",a,b,result);
	return 0;
}

int Percentage(a,b){
	float result;
	result = a*(b/100);
	printf("Percentage of %f is = %f",a,result);
	return 0;
}

int Temperature(a){
	float result;
	result = (a*9/5)+32;
	printf("%f @celsus = %f @ferenheit",a,result);
	return 0;
}

You can also add a loop for making this process working multiple times. so the calculator will go to work more efficiently. You can make this loop when we are calling the external methods for calculation.

Now I am going to provide you the full source code for this tutorial here.

The source code is:

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

main(){
	// Variable Declearation
	int input,add1,add2,sub1,sub2,mul1,mul2;
    float div1,div2,moddiv1,moddiv2,per1,per2,temp;
 	// Input 
 	printf("\n1. Addition\t\t 2. Subtraction\n");
 	printf("3. Multiplication\t 4. Division\n");
 	printf("5. Modulus Division\t 6.  Percentage\t 7. Temperature\n\n");
 	printf("Enter What You want:");
 	scanf("%d",&input);
 	// functioning
 	if(input == 1){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&add1);
 		printf("Enter Second Number: ");
 		scanf("%d",&add2);
 		// You can use the print statement here also
 		Addition(add1,add2);
	 }
	if(input == 2){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&sub1);
 		printf("Enter Second Number: ");
 		scanf("%d",&sub2);
 		// You can use the print statement here also
 		Subtraction(sub1,sub2);
	}
	if(input == 3){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%d",&mul1);
 		printf("Enter Second Number: ");
 		scanf("%d",&mul2);
 		// You can use the print statement here also
 		Multiplication(mul1,mul2);
	}
	if(input == 4){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%f",&div1);
 		printf("Enter Second Number: ");
 		scanf("%f",&div2);
 		// You can use the print statement here also
 		Division(div1,div2);
	}
	if(input == 5){
 		// Do some Work
 		printf("Enter First Number: ");
 		scanf("%f",&moddiv1);
 		printf("Enter Second Number: ");
 		scanf("%f",&moddiv2);
 		// You can use the print statement here also
 		ModDivision(moddiv1,moddiv2);
	}
	if(input == 6){
 		// Do some Work
 		printf("Enter Your Total Value: ");
 		scanf("%f",&per1);
 		printf("Enter Your Percentaged Value: ");
 		scanf("%f",&per2);
 		// You can use the print statement here also
 		Percentage(per1,per2);
	}
	if(input == 7){
 		// Do some Work
 		printf("Enter Celsius Temperature: ");
 		scanf("%f",&temp);
 		// You can use the print statement here also
 		Temperature(temp);
	}

}

int Addition(a,b){
	int result;
	result = a+b;
	printf("%d + %d = %d",a,b,result);
	return result;
}

int Subtraction(a,b){
	int result;
	result = a-b;
	printf("%d - %d = %d",a,b,result);
	return result;
}

int Multiplication(a,b){
	int result;
	result = a*b;
	printf("%d * %d = %d",a,b,result);
	return result;
}

int Division(a,b){
	float result;
	result = a/b;
	printf("%f / %f = %f",a,b,result);
	return 0;
}

int ModDivision(a,b){
	float result;
	result = a%b;
	printf("%f / %f = %f",a,b,result);
	return 0;
}

int Percentage(a,b){
	float result;
	result = a*(b/100);
	printf("Percentage of %f is = %f",a,result);
	return 0;
}

int Temperature(a){
	float result;
	result = (a*9/5)+32;
	printf("%f @celsus %f @ferenheit",a,result);
	return 0;
}

For more use C documentation there will be everything about c. You can learn extra valuable and many things from there so , keep considering the documentation for reference in future.

C documentation link: https://devdocs.io/c/

You can also use other Websites for learning and see the examples.

 

See My all Projects here: https://www.technicalsayan.in/category/projects/

Spread To Friends
  •  
  •  
  •  
  •  
  •  
  •  
  •  

1 thought on “C Language: Writing your own Basic Calculator in C 2021”

Leave a Comment