1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
GuDViN [60]
3 years ago
7

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.

Computers and Technology
1 answer:
larisa86 [58]3 years ago
4 0

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\n",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\n",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\n",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\n",c);  }  //displays result of subtraction

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.

You might be interested in
PYTHON Write a grade program using a function called computegrade that takes a score as its parameter and return a grade as a st
makvit [3.9K]

def computeGrade(float grade, str a){

a = "A" if a > 0.9 else a = "B" if a > 0.8 else "C"

return a;

}

print(a)

3 0
3 years ago
your computer running Windows 10 is doing some very strange things with operating system you are fairly certain it is not a hard
AlekseyPX
It might be a virus, when my computer starts going crazy i just bought a fixme stick and they work amazing or trying getting an expert to fix it if it worse
6 0
2 years ago
Read 2 more answers
What is the way of retrieving information from the Internet?
TEA [102]
We have to search the desired topic on the search box in the homepage
4 0
2 years ago
Read 2 more answers
Which font is most suitable for an academic article on a website? Which is most suitable for casual information?
CaHeK987 [17]

There are thousands of fonts, there is no clear answer of what is best for ___ website or ___ page. It's all what you think looks good and sometimes depends on the colors on the site.

4 0
3 years ago
Read 2 more answers
2.5 code practice I need answers please
Leni [432]

Answer:

import random

a = random.randint(1,10)

b = random.randint(1,10)

answer = a * b

print (str(a) + " X " + str(b) + " = " + str(answer))

Explanation:

Happy to help you mate

7 0
3 years ago
Other questions:
  • What term refers to a piece of software that interfaces with the hardware on your computer?
    10·2 answers
  • What are the key goal, performance, and risk indicators?
    15·1 answer
  • If Asa changes the text to bold, he has changed the style. True False
    8·2 answers
  • Which command is not one of the available Change Case options?
    11·1 answer
  • Find the inverse function of f(x)= 1+squareroot of 1+2x
    6·1 answer
  • A rocket always rotates about its___?
    9·2 answers
  • Even if you cannot afford it you should donate at least 5 of your earnings to charity true or false
    8·1 answer
  • JUST MAXED OUT THE COMMENTS anyways come here
    7·2 answers
  • Describe a NIC card and can you have more than one?
    13·1 answer
  • Which example illustrates the idea of "collecting data"?
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!