0
Why the calculator is not working properly?
#include <stdio.h> #include <stdlib.h> int main() { int a; int b; char op; printf("First no\n"); scanf("%d",&a); printf("Enter operator\n"); scanf("%c ",&op); printf("Second no\n"); scanf("%d",&b); if(op=='+') { printf("%d\n",a+b); } if(op=='-') { printf("%d\n",a-b); } else if(op=='*') { printf("%d\n",a*b); } else{ printf("%d",a/b); } return 0; }
3 Answers
+ 4
You forget to write switch statement and check your else if condition statement use them properly
+ 4
Coding Gl See my code
I used switch case
#include <stdio.h>
int main() {
int a,b;
int opt;
printf("Enter the first Integer :");
scanf("%d",&a);
printf("Enter the second Integer :");
scanf("%d",&b);
printf("\nInput your option :\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("The Addition of %d and %d is: %d\n",a,b,a+b);
break;
case 2:
printf("The Substraction of %d and %d is: %d\n",a,b,a-b);
break;
case 3:
printf("The Multiplication of %d and %d is: %d\n",a,b,a*b);
break;
case 4:
printf("The Division of %d and %d is : %d\n",a,b,a/b);
break;
default:
printf("Input correct option\n");
break;
}
return 0;
}
0
actually it is not giving me chance to input second no.Please help
here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
char op;
printf("First no\n");
scanf("%d",&a);
printf("Enter operator\n");
scanf("%c ",&op);
printf("Second no\n");
scanf("%d",&b);
if(op=='+')
{
printf("%d\n",a+b);
}
else if(op=='-')
{
printf("%d\n",a-b);
}
else if(op=='*')
{
printf("%d\n",a*b);
}
else
{
printf("%d",a/b);
}
return 0;
}