Construct a flowchart based on this code and write its equivalent algorithms.
#include <stdio.h>
int main()
{
int x,y;
float result;
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&x);
printf("Enter second number: ");
scanf("%d",&y);
printf("Choose operation to perform (+,-,*,/): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=x+y;
break;
case '-':
result=x-y;
break;
case '*':
result=x*y;
break;
case '/':
result=(float)x/(float)y;
break;
case '%':
result=x%y;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %.2f\n",x,ch,y,result);
// Directly print the number with .2f precision
printf("\n\n Input new number to try again/ n to exit):");
getchar();
ch=getchar();
if (ch !='n')
{
return main();
}
else
{
printf("\n\n Have a Nice
Day!");
}
return 0;
}
FLOW CHART
The code:
#include <stdio.h>
int main()
{
int x,y;
float result;
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&x);
printf("Enter second number: ");
scanf("%d",&y);
printf("Choose operation to perform (+,-,*,/): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+':
result=x+y;
break;
case '-':
result=x-y;
break;
case '*':
result=x*y;
break;
case '/':
result=(float)x/(float)y;
break;
case '%':
result=x%y;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %.2f\n",x,ch,y,result);
// Directly print the number with .2f precision
printf("\n\n Input new number to try again/ n to exit):");
getchar();
ch=getchar();
if (ch !='n')
{
return main();
}
else
{
printf("\n\n Have a Nice Day!");
}
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.