161k views
1 vote
Write a C program to perform simple C arithmetic calculations. The user is to enter a simple expression (integer operator integer) such as

15 + 30
The program is to extract the 2 operands and the operator, perform the indicated calculation and display the result. For example
15 + 30 = 45
Operators should include + , - , * , / , and %
Operands are positive integers , no sign
Use getchar to input the expression
Allow for variable spacing before the first operands and between operators and operands ( 0, 1, or more spaces)

Allow the user the option of entering another expression
Code a function for each operator to do the appropriate arithmetic operations (5 functions)
No global variables
No string handling functions
No arrays
May only use input and output functions such as getchar and putchar

1 Answer

2 votes

Answer:

Input example:

select the funcion: 1

write the 1sd number2

write the 2nd number1

value is 2.000000

Step-by-step explanation:

#include <stdio.h>

main()

{

//define the variables as float

float a, b, c, e;

char d;

while (1)

{

//input values

printf("select the function: ");

scanf("%c",&d);

printf("write the 1sd number");

scanf("%f",&a);

getchar();

printf("write the 2nd number");

scanf("%f",&b);

getchar();

if (d=='%')

{

a=a*b/100;

}

if (d=='*')

{

a=a*b;

}

if (d=='+')

{

a=a+b;

}

if (d=='/')

{

a=a/b;

}

if (d=='-')

{

a=a-b;

}

printf("value is %f \\",a);

}

printf("final value is %f",a);

getchar();

}

User Dinkar Kumar
by
5.6k points