0
What is wrong in this code?I wanted to make basic C calculator?But it doesn't execute else if statement if if statement is false
#include<stdio.h> int main(){ int g; int b,c; printf("What do you want to do?\n1.Addition\n2.Substraction\n3.Multipication\n4.Division\n" ); scanf("%d",&g); if(g=1) {printf("Enter two number:\n"); scanf("%d%d",&b,&c); printf("%d+%d=%d",b,c,(b+c));} else if(g=2) {printf("Enter two number:\n"); scanf("%d%d",&b,&c); printf("%d-%d=%d",b,c,(b-c));} else if(g=3) {printf("Enter two number:\n"); scanf("%d%d",&b,&c); printf("%d*%d=%d",b,c,(b*c));} else if(g=4) {printf("Enter two number:\n"); scanf("%d%d",&b,&c); printf("%d/%d=%d",b,c,(b/c));} else {printf("Invalid input.Programme ended. . . .");} return 0; }
4 ответов
+ 1
if (g ==
not g=
= is an assigment operator it give value to g.
== is comparison operator
+ 1
Don't use = instead of ==
= is an assignment operator, but you are looking for a comparison operator (==)
0
Oh!Thanks
0
You're forgetting the symbol of "==" is the same, and "=" is an assignment. When using the IF statement, WHILE or DO-WHILE you need to use "=="