0

I want to execute this again mean after the one task the program ask do u want more calculation how can I do In this program

#include <stdio.h> int main() { int a,b; char operand; printf("Please choose the operand (+,-,*,/):"); scanf("%c",&operand); printf("Enter the value of A and B"); scanf("%d %d",&a,&b); switch(operand) { case '+': printf("%d+%d=%d",a,b,a+b); break; case'-': printf("%d-%d=%d",a,b,a-b); break; case'*': printf("%d*%d=%d",a,b,a*b); break; case'/': printf("%d/%d=%d",a,b,a/b); break; default: printf("Error"); } return 0; }

21st Oct 2019, 5:45 PM
SHASHANK BHANDARI
SHASHANK BHANDARI - avatar
6 Respuestas
+ 1
just put main() before return 0 and you gat what you want, or use a loop which is a better option
21st Oct 2019, 10:25 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Why u take 1 in the while loop?
22nd Oct 2019, 8:34 AM
SHASHANK BHANDARI
SHASHANK BHANDARI - avatar
+ 1
You know in C/C++ any non zero integral value is considered a boolean true, passing in 1 is actually telling it to repeat forever, but we trap the loop with the confirmation of <again> at the bottom, the loop is broken if input for <again> neither equal to 'Y' nor 'y'.
22nd Oct 2019, 8:37 AM
Ipang
+ 1
You can also choose to use `do...while` loop, I think it makes more sense somehow. Because `do...while` loop runs at least once, before the loop condition is evaluated.
22nd Oct 2019, 8:39 AM
Ipang
0
Hey asterisk I don't get the point?
22nd Oct 2019, 2:37 AM
SHASHANK BHANDARI
SHASHANK BHANDARI - avatar
0
Here's a picture of how you can do it with a loop. Hope this clears your doubt. int main() { int a, b; char operand, again; // add <again> while (1) { // code to read operation here (+ - * / etc) // code to read 2 numbers here // code to calculate here (switch) // ask whether to repeat again printf("\nDo you want to calculate again? "); scanf(" %c", &again); if (again != 'Y' && again != 'y') break; } return 0; }
22nd Oct 2019, 8:32 AM
Ipang