Dynamic Calculator With natural Input
This program takes input as an expression which contains two operands as for now, the program then finds the operator and displays which operation is done on that operation along with the result. This program is desigened in C language.
Description:
Two input integers along with the operator is taken as input as input, Now the individual functions are called with respect to the operator used, this is handelled using a switch case. Let the input be 12+3 then it displays the sum is 15.
#include<stdio.h> // Function add for addition of values passed by calc int add(int f, int g) { int res=0; res = f+g; return res; // Returns an int value for printing in calc } // Function sub for Subtraction of values passed by calc int sub(int f, int g) { int res=0; res = f-g; return res; // Returns an int value for printing in calc } // Function mul for multiplication of values passed by calc int mul(int f, int g) { int res=0; res = f*g; return res; // Returns an int value for printing in calc } // Function div for division of values passed by calc float div(int f, int g) { float res=0; res = f*1.0/g; return res; // Returns an int value for printing in calc } // Function calc for performing claculations of values passed by main void calc(int c, char e, int d) { switch(e) { case '+': printf("The Sum is : %d",add(c,d)); break; case '-': printf("The Difference is : %d",sub(c,d)); break; case '*': printf("The Product is : %d",mul(c,d)); break; case '/': printf("The Result if: %f ", div(c,d)); break; default: printf("Invalid input"); } } // Function main for kick Start program int main() { int a=0,b=0; char c; printf("Enter The Expression :"); scanf("%d %c %d",&a,&c,&b); calc(a,c,b); return 0; }
Description:
Two input integers along with the operator is taken as input as input, Now the individual functions are called with respect to the operator used, this is handelled using a switch case. Let the input be 12+3 then it displays the sum is 15.
Comments
Post a Comment