Simple calculator (using if...else if)

Program:

#include<stdio.h>
int main()
{
char op;
float n1,n2,res;
printf("Enter two numbers: \n");
scanf("%f %f",&n1,&n2);
printf("Enter Operation (+,-,*,/): ");
fflush(stdin);
scanf("%c",&op);
if(op=='+')
    res=n1+n2;
else if (op=='-')
    res=n1-n2;
else if (op=='*')
    res=n1*n2;
else if (op=='/')
    res=n1/n2;
else
{
printf("Invalid");
}

printf("Result: %.2f",res);
return 0;

}

Output:



No comments:

Post a Comment

x^y using recursion

Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...