5.3k views
4 votes
Write a C program to prform simple C aritlimetic calculations. The user is to enter a simple expression(integer operaior integer) such as 15+30=45. The program is to exlract the 2 operands and the operator, perform the indicated calculation and display the result. For examole15 + 30 = 45. Operators should includc t, -, * , l, and %'.Operands are prcsitive integers, no sign. Use getchar to input the cxpression. Allow for variable spacing before the first operand and between operators and operands.

User Lopson
by
4.7k points

1 Answer

3 votes

Answer:

Here is the C program:

#include <stdio.h> //to use input output functions

//functions prototype

unsigned int mod(unsigned int a, unsigned int b);

unsigned int mul(unsigned int a, unsigned int b);

unsigned int sub( unsigned int a,unsigned int b);

float divide(unsigned int a,unsigned int b);

unsigned int add( unsigned int a,unsigned int b);

int main() { //start of main method

unsigned int a, b; //declare variables to store the operands

char d; //declare variable to store the operator

printf("Enter an operator: "); //prompts user to enter an operator

scanf("%c",&d); //reads the operator from use

getchar(); //gets a character

while (d!='q') { //keeps iterating until user enters q to quit

printf("Enter 1st operand: "); //prompts user to enter first operand

scanf("%d",&a); //reads first operand from user

getchar(); //reads character

printf("Enter 2nd operand: "); //prompts user to enter second operand

scanf("%d",&b); //reads second operand from user

getchar();

if (d=='%') { //if the character of operator is a mod

printf("%d",a); //prints operand 1

putchar(d); //displays operator

printf("%d",b); //displays operand 2

printf(" = "); //displays =

mod(a,b); } //displays computed modulo of two input operands

if (d=='*') //if the input character is for multiplication operator

{printf("%d",a); //prints operand 1

putchar(d); //displays operator

printf("%d",b); //displays operand 2

printf(" = "); //displays =

mul(a,b); } //displays computed multiplication

if (d=='+') { //if the input character is for addition operator

printf("%d",a); //prints operand 1

putchar(d); //displays operator

printf("%d",b); //displays operand 2

printf(" = "); // displays =

add(a,b); } //displays computed addition

if (d=='/') { //if the input character is for division operator

printf("%d",a); // prints operand 1

putchar(d); //displays operator

printf("%d",b); //displays operand 2

printf(" = "); //displays =

divide(a,b); } //displays computed division

if (d=='-') { //if the input character is for subtraction operator

printf("%d",a); //prints operand 1

putchar(d); //displays operator

printf("%d",b); // displays operand 2

printf(" = "); //displays =

sub(a,b); } //displays computed subtraction

printf("Enter an operator: "); //asks again to enter an operator

scanf("%c",&d); //reads operator from user

getchar(); } } //gets character

unsigned int mod( unsigned int a, unsigned int b){ //function to compute modulo of two integers with no sign

int c = a%b; //computes mod

printf("%d",c); } //displays mod result

unsigned int add(unsigned int a, unsigned int b){ // function to compute addition of two integers

int c = a+b; //computes addition

printf("%d\\",c); } //displays result of addition

unsigned int mul(unsigned int a, unsigned int b){ //function to compute multiplication of two integers

int c = a*b; //multiplies two integers

printf("%d\\",c); } //displays result of multiplication

float divide(unsigned int a, unsigned int b){ //function to compute division of two integers

float c = a/b; //divides two integers and stores result in floating point variable c

printf("%f\\",c); } //displays result of division

unsigned int sub(unsigned int a, unsigned int b){ //function to compute subtraction of two integers

int c = a-b; //subtracts two integers

printf("%d\\",c); } //displays result of subtraction

Step-by-step explanation:

The program is well explained in the comments mentioned with each line of the program. The program uses while loop that keeps asking user to select an operator and two operands to perform arithmetic calculations. The if conditions are used to check what arithmetic calculation is to be performed based on user choice of operand and calls the relevant function to perform calculation. There are five functions that perform addition, modulo, subtraction, division and multiplication and display the result of computation. The screenshot of output is attached.

Write a C program to prform simple C aritlimetic calculations. The user is to enter-example-1
User Blum
by
4.2k points