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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

15th Jul 2019, 12:44 PM
Rayan 99
Rayan 99 - avatar
4 Answers
+ 1
if (g == not g= = is an assigment operator it give value to g. == is comparison operator
15th Jul 2019, 12:46 PM
Taste
Taste - avatar
+ 1
Don't use = instead of == = is an assignment operator, but you are looking for a comparison operator (==)
15th Jul 2019, 12:47 PM
Airree
Airree - avatar
0
Oh!Thanks
15th Jul 2019, 12:48 PM
Rayan 99
Rayan 99 - avatar
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 "=="
15th Jul 2019, 2:46 PM
Manuel Pinzón G.