#include <stdio.h>
int a,b;
int add(a,b){
int sum = a+b;
return(sum);
}
int sub(a,b){
int diff = a-b;
return(diff);
}
int mult(a,b){
int prod = a*b;
return(prod);
}
int div(a,b){
int quot = a/b;
return(quot);
}
int rem(a,b){
int rem = a%b;
return(rem);
}
main()
{
int x,y,ans;
char op;
while(op!='x'||op!='X'){
printf("
[A] Addition
** Subtraction
[c] Multiplication
[D] Division
[E] Remainder
");
printf("
Which operation? ");
scanf("%c",&op);
if(op=='a'||op=='A'){
printf("
Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = add(x,y);
printf("
Answer is: %d
",ans);
}else if(op=='b'||op=='B'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = sub(x,y);
printf("
Answer is: %d
",ans);
}else if(op=='c'||op=='C'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = mult(x,y);
printf("
Answer is: %d
",ans);
}else if(op=='d'||op=='D'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = div(x,y);
printf("
Answer is: %d
",ans);
}else if(op=='e'||op=='E'){
printf("Input 1st no: ");
scanf("%d",&x);
printf("Input 2nd no: ");
scanf("%d",&y);
ans = rem(x,y);
printf("
Answer is: %d
",ans);
}else if(op=='x'||op=='X'){
break;
}else{
printf("
Try again
");
}
}
printf("
**Program Terminated**
");
}
we were given an assignment in class today. in the above code, i am trying to achieve where a user chooses an operation(a,b,c,d,e), and inputs 2 numbers and outputs the answers. it works fine but after the first loop, the first 3 lines of the loop are executed twice… :puzzled:
why is this and how do i fix it?