WAP in C to make simple calculator using user defined function
It's a C program through which you can make Simple Calculator through User Defined Function.
In it we can do addition of two numbers, subtraction of two number , multiplication of two number an division of two numbers ,But on our need we can increase the variables and be able to do many numbers addition ,subtraction , multiplication and division .
Here we have use fflush(stdin); function to take standard input as after entering integer value we can't take string characters so to take string we use this function..
#include
#include
int add(int,int);
int sub(int,int);
int mul(int,int);
float div(int,int);
void main()
{
int a,b,c;
char i;
clrscr();
printf("Enter two no.:");
scanf("%d %d",&a,&b);
printf("Enter your choice: ");
fflush(stdin);
scanf("%c",&i);
switch(i)
{
case '+':
c=add(a,b); //calling of add function.
printf("\nAddition of:%d",c);
break;
case '-':
c=sub(a,b); //calling of sub function.
printf("\nSubtraction is :%d",c);
break;
case '*':
c=mul(a,b); //calling of mul function.
printf("\nMultiplication is:%d",c);
break;
case '/':
float(c)=div(a,b); //calling of div function.
printf("\nDivision is:%.2f",c);
break;
default:
printf("Invalid number");
break;
}
getch();
}
int add(int x,int y) //definition of add function.
{
return(x+y);
}
int sub(int x,int y) //definition of sub function.
{
return(x-y);
}
int mul(int x,int y) //definition of mul function.
{
return(x*y);
}
float div(int x,int y) //definition of div function.
{
return(float(x)/y);
}
The Output will be:- Here are the outputs of all operation..............(+,-,*,/).
It's a C program through which you can make Simple Calculator through User Defined Function.
In it we can do addition of two numbers, subtraction of two number , multiplication of two number an division of two numbers ,But on our need we can increase the variables and be able to do many numbers addition ,subtraction , multiplication and division .
Here we have use fflush(stdin); function to take standard input as after entering integer value we can't take string characters so to take string we use this function..
#include
#include
int add(int,int);
int sub(int,int);
int mul(int,int);
float div(int,int);
void main()
{
int a,b,c;
char i;
clrscr();
printf("Enter two no.:");
scanf("%d %d",&a,&b);
printf("Enter your choice: ");
fflush(stdin);
scanf("%c",&i);
switch(i)
{
case '+':
c=add(a,b); //calling of add function.
printf("\nAddition of:%d",c);
break;
case '-':
c=sub(a,b); //calling of sub function.
printf("\nSubtraction is :%d",c);
break;
case '*':
c=mul(a,b); //calling of mul function.
printf("\nMultiplication is:%d",c);
break;
case '/':
float(c)=div(a,b); //calling of div function.
printf("\nDivision is:%.2f",c);
break;
default:
printf("Invalid number");
break;
}
getch();
}
int add(int x,int y) //definition of add function.
{
return(x+y);
}
int sub(int x,int y) //definition of sub function.
{
return(x-y);
}
int mul(int x,int y) //definition of mul function.
{
return(x*y);
}
float div(int x,int y) //definition of div function.
{
return(float(x)/y);
}
The Output will be:- Here are the outputs of all operation..............(+,-,*,/).
No comments:
Post a Comment