Question

Task 2.5: A simple calculator (25 marks) In this task you will develop a MARIE program...

Task 2.5: A simple calculator (25 marks)

In this task you will develop a MARIE program that implements four basic functions of a calculator: addition, subtraction, multiplication, division, and exponent.

The program should be performing the user selected tasks (one of the mentioned mathematical operations) after the user enters one of the selection inputs, ‘a’, ‘s’, ‘m’, ‘d’ or ‘p’. Here, input ‘a’ selects addition process, ‘s’ selects subtraction process, ‘m’ selects multiplication process, ‘d’ selects division process, and ‘p’ selects exponent process. User enters ‘q’ to quit the program.

You are required to write subroutines to perform these tasks (addition, subtraction, multiplication, division, and power) and write a main program to process user selection inputs and call the appropriate subroutine thereof. The codes must contain comments, as a paragraph before any block of code i.e. subroutine or as inline comments wherever appropriate.

The calculator needs to be able to handle negative inputs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>//use of exit function
#include<math.h>//for predefined mathematical functions
//subroutine for addition
void add(int a,int b)//it takes two arguments as input
{
   printf("%d\n",a+b);//result
}
//subroutine for subtraction
void sub(int a,int b)
{
   printf("%d\n",a-b);
}
//subroutine for multiplication
void mul(int a,int b)
{
   printf("%d\n",a*b);
}
//subroutine for division
void division(int a,int b)
{
   printf("%d\n",a/b);
}
//subroutine for power
void power(int a,int b)
{
   double res;
   res=pow(a,b);
   printf("%lf\n",res);//power function
}
//main program
int main()
{
   char ch,ch1;
   int x,y;
   double f1,f2;//for power function inputs should be double datatype
   printf("user! enter the choice\n");
   printf(" a-addition\n s-subtraction\n m-multiplication\n d-division\n p-exponent\n q-quit\n");
   while(1)
   {
       scanf("%c",&ch);
       switch(ch)
       {
       case 'a':printf("Enter input values");
               scanf("%d%d",&x,&y);
               add(x,y);
               break;
       case 's':printf("Enter input values");
                   scanf("%d%d",&x,&y);
                   sub(x,y);
                   break;
       case 'm':printf("Enter input values");
                   scanf("%d%d",&x,&y);
                   mul(x,y);
                   break;
       case 'd':printf("Enter input values");
                   scanf("%d%d",&x,&y);
                   division(x,y);
                   break;
       case 'p':printf("Enter input values");
                   scanf("%d%d",&f1,&f2);
                   power(x,y);
                   break;
       case 'q':exit(0);
       }
   }
}

/*Output:
user! enter the choice
a-addition
s-subtraction
m-multiplication
d-division
p-exponent
q-quit
a
Enter input values2 3
5
s
Enter input values
3 4
-1
m
Enter input values4 5
20
d
Enter input values6 3
2
p
Enter input values4 16
216.000000
q*/

Add a comment
Know the answer?
Add Answer to:
Task 2.5: A simple calculator (25 marks) In this task you will develop a MARIE program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction,...

    A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction, multiplication and division) of two numbers, using your understanding of Java. Description: You need to write a program that will display a menu when it is run. The menu gives five choices of operation: addition, subtraction, multiplication, division, and a last choice to exit the program. It then prompts the user to make a choice of the calculation they want to do. Once the...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • IN JAVASCRIPT 5. Write the pseudo code to solve the following problem (10pts/10pts) You are asked...

    IN JAVASCRIPT 5. Write the pseudo code to solve the following problem (10pts/10pts) You are asked to write a calculator program. Your calculator must perform Addition, Subtraction, Division and Multiplication. The user will enter two numbers and then an operator. If the user enters an operator other than the four mentioned the program will display an error message otherwise it will display the answer. Demonstrate use of an if statement

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Question 20: Write a python program that will perform the operations of simple calculator. The operations...

    Question 20: Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division. Sample output : Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210

  • Implement a simple four function calculator in the LC3 assembly language that will run on the...

    Implement a simple four function calculator in the LC3 assembly language that will run on the LC-3 simulator. The LC-3 computer only reads a character at a time but is capable of displaying a string of characters. This calculator program will use multiple subroutines that perform purposes such as reading multi-digit numbers, converting them to integers, performing addition, subtraction, multiplication, or division on these numbers, and displaying the result. The inputs to the calculator will not exceed 4 digits. The...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT