Calculator with function in c

Functions in c

 Here is the code of the calculator with help of function in the c programming language


code:


#include <stdio.h>

int sum(int, int);         //Declaration of function for sum

int minus(int, int);        //Declaration of function for subtraction

int multiply(int, int);    //Declaration of function for product

int divide(int, int);       //Declaration of function for division

int main()

{

int num1, num2;         //num1 and num2 declare to store values of user, which he want to calculate

char operation;        //operation declare to ask the user what operation he want to use

int ans = 0;            //this is used to store the answer after the calculation of function

printf("\n Enter the num1 :\t");

scanf_s("%d", &num1);

printf("\n Enter the num2 :\t");

scanf_s("%d", &num2);

printf("\nEnter 1 for sum, 2 for minus, 3 for multiply, 4 for divide :\t");

scanf_s(" %c", &operation);

//this program will run for 1 time compulsory

while (1)

{

if (operation == '1')

{

ans = sum(num1, num2);

printf("\nAnd the sum is = %d", ans);

}

else if (operation == '2')

{

ans = minus(num1, num2);

printf("\nAnd the minus is = %d", ans);

}

else if (operation == '3')

{

ans = multiply(num1, num2);

printf("\nAnd the  product is = %d", ans);

}

else if (operation == '4')

{

ans = divide(num1, num2);

printf("\nAnd the ans of division is = %d", ans);

}

else//if user enter besides this (1,2,3,4,q,Q) then this will work

printf("\nInvalid choice, try again");

printf("\nOK, if you want to use it again then you should not press q or Q in place of operation, if you do that the program will stop!");

printf("\n Enter the num1 :\t");

scanf_s("%d", &num1);

printf("\n Enter the num2 :\t");

scanf_s("%d", &num2);

//fflush(stdin);

printf("\nEnter 1 for sum, 2 for minus, 3 for multiply, 4 for divide (and if you press q or Q then program will stop) :\t");

scanf_s(" %c", &operation);

if (operation == 'q' || operation == 'Q')        //if user enter q or Q then this loop will exit.

break;

//complete the code 


getch();

return 0;

}

int sum(int i, int j)         // for addition function

{


int sum = 0;

sum = i + j;

return (sum);

}

int minus(int i, int j)            //for subtraction funtion

{

int minus = 0;

minus = i - j;

return(minus);


}

int multiply(int i, int j)                //for multiply function

{

int multiply = 0;

multiply = i*j;

return (multiply);

}

int divide(int i, int j)            //for division function

{

int divide = 0;

divide = i / j;

return (divide);

}

example-in-c

output:

 Enter the num1 :       3

 Enter the num2:       4

Enter 1 for sum, 2 for minus, 3 for multiply, 4 for divide:    1

And the sum is = 7
OK, if you want to use it again then you should not press q or Q in place of operation, if you do that the program will stop!
 Enter the num1 :

Comments

  1. Great guidance. Keep doing well.👍
    If you are a tech freak visit my site also if you want to make career in android development. Make sure to check this out.

    HOW TO START A CAREER IN ANDROID APP DEVELOPMENT?

    ReplyDelete

Post a Comment

Hire a programmer

Popular posts from this blog

CSS selector

C program to convert decimal to binary number

C Program to convert binary to decimal number