Monday , December 18 2017
Home / C / C Program to make a Calculator – Source Code

C Program to make a Calculator – Source Code

C Calculator Program Source Code : C program with full illustration in comments is given here for making a Calculator in C. You can make this Calculator program to perform any operations you want but we have made this only for following operations:

  1. Addition (a+b)
  2. Subtraction (a-b)
  3. Multiplication (a*b)
  4. Division (a/b)
  5. Modulus (a%b)
  6. Factorial (a!)
  7. Power (a^b)

Remember you can add any numb er of operations you want to do in this calculator you have to do only these things to add any operation.

  1. Add a function name in Function Prototype Declaration
  2. Add a switch case with assigned operator to call the function you have mentioned for calculating the result.
  3. Print the result or calculated value of the operation in the Function definition of the new added function.

You can make this program in other languages too i.e in C++ , Java , Python, etc. We will upload the other program too soon. Below is the C program to make a calculator.

// Calculator Program code using C language
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>

#define KEY "Enter the Operation you want to calculate +,-,*,/,%,^,!:"

// Declaring Functions to be used ahead 
void add();
void subtract();
void multiply();
void divide();
void modulus();		
void pow();
int fact();
void calc_op();

// Main Program Logic begins from here
int main()
{
    int temp=1;
    char op;

    // Function call 
    calc_op();

    while(temp)
    {
        printf("n");
        printf("%s : ", KEY);

        op=getche();

        switch(op)
        {
            case '+': add();
                      break;

            case '-': subtract();
                      break;

            case '*': multiply();
                      break;

            case '/': divide();
                      break;

            case '%': modulus();
                      break;

            case '!': fact();
                      break;

            case '^': pow();
                      break;

            case 'D':
            case 'd': calc_op();
                      break;

            case 'Q':
            case 'q': exit(0);
                      break;
            case 'c':
            case 'C': clrscr();
                      calc_op();
                      break;

            default : clrscr();

    printf("n     You have entered unavailable option");
    printf("     *n");
    printf("n  *Please Enter any one of below available ");
    printf("options  n");
                      calc_op();
        }
    }
}

//Function Definitions

void calc_op()
{
    //clrscr();  to clear screen
    
    printf("n C calculator nn");

    printf("   * Press 'Q' or 'q' to quit this program or calculator ");
    printf("  * Press 'D' or 'd' to display options below");
    printf("Press 'C' or 'c' to clear the screen display  nn");

    printf("Enter + for Addition n");
    printf("Enter - to Subtract n");
    printf("Enter * to Multiply n");
    printf("Enter / to Divide n");
    printf("Enter % for Modulus operation n");
    printf("Enter ^ for X Power Y n");
    printf("Enter ! to calculate Factorial nn");
}

void add()
{
    int n, sum=0, k=0, number;
    printf("n How many numbers you want to add:");
    scanf("%d",&n);
    printf("Please enter %d numbers one by one: n",n);
    while(k<n)
    { 
        scanf("%d",&number);
        sum+number;
        k=k+1;
    }
    printf("Sum of %d numbers = %d n",n,sum);
}

void subtract()
{ 
    int l, m, n = 0; 
    printf("n Enter the first number  : "); 
    scanf("%d", &l); 
    printf("Enter the second number : "); 
    scanf("%d", &m); 
    n=l-m;
    printf("n%d - %d = %dn", l, m, n); 
}

void multiply()
{
    int a, b, mul=0; 
    printf("n Enter first number : "); 
    scanf("%d", &a); 
    printf(" Enter second number: "); 
    scanf("%d", &b);
    mul=a*b;
    printf("n Multiplication of entered numbers = %dn",mul);
}

void divide()
{
    int a, b, d=0; 
    printf("nPlease enter first number  : "); 
    scanf("%d", &a); 
    printf("Please enter second number : "); 
    scanf("%d", &b);
    d=a/b;
    printf("nDivision of entered numbers=%dn",d);
}

void modulus()
{
    int a, b, d=0; 
    printf("n Enter first number   : "); 
    scanf("%d", &a); 
    printf(" Enter second number  : "); 
    scanf("%d", &b);
    d=a%b;
    printf("n Modulus of entered numbers = %dn",d);
}

void pow()
{
    double a,num, p;
    printf("nEnter two numbers to find the power n");
    printf("number: ");
    scanf("%lf",&a);

    printf("power : ");
    scanf("%lf",&num);

    p=pow(a,num);

    printf("n%lf to the power %lf = %lf n",a,num,p);
}

int fact()
{
    int j,calc_fact=1,num;

    printf("nEnter a number to find factorial : ");
    scanf("%d",&num);

    if (num<0)
    {
        printf("n Factorial can be calculated for positive integers only not for negative integers  n");
        return 1;
    }               

    for(j=1;j<=num;j++)
    calc_fact=calc_fact*j;
    printf("n");
    printf("Factorial of the number %d is:%dn",num,calc_fact);
    return 0;
}

OUTPUT :

Also See:   Program in C to draw a House

Program to make a Calculator in C Language

Other search terms:

C program to make a calculator, C Language calculator Source Code , Online source code for C Calculator

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

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