Question

In: Computer Science

C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...

C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call.

I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add things like 2.5 + 3.0 or 2.5 * 2.5, etc.

Thank you, please see my code below.

-------------------------------------------------------------

MY CODE:*****

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

//Functions

int add(int a, int b)

{

    return a + b;

}

int sub(int a, int b)

{

    return a - b;

}

int mul(int a, int b)

{

    return a * b;

}

float div(int a, int b)

{

    return a * 1.0 / b;

}

int main()

{

    int result, a, b, choice;

    float res;

    //User prompts

    printf("Math Calculator: What type of operation would you like me to use? (You will choose numbers after selection) : \n1.Addition\n2.Subtraction\n3.Multiply\n4.Divide\n");

    scanf("%d", &choice);

    printf("Enter first number : ");

    scanf("%d", &a);

    printf("Enter second number : ");

    scanf("%d", &b);


    //Cases

    switch (choice) {

    case 1:

        result = add(a, b);

        printf("The answer equals %d", result);

        break;

    case 2:

        result = sub(a, b);

        printf("The answer equals %d", result);

        break;

    case 3:

        result = mul(a, b);

        printf("The answer equals %d", result);

        break;

    case 4:

        res = div(a, b);

        printf("The answer equals %f", res);

        break;

    default:

        printf("You did not select a valid operation from above.");

        break;

    }


    return 0;

}

Solutions

Expert Solution

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

//Functions

int add(float a, float b)

{

return a + b;

}

int sub(float a, float b)

{

return a - b;

}

int mul(float a, float b)

{

return a * b;

}

float div(float a, float b)

{

return a * 1.0 / b;

}

int main()

{

int result, choice;

float res,a,b;

//User prompts

printf("Math Calculator: What type of operation would you like me to use? (You will choose numbers after selection) : \n1.Addition\n2.Subtraction\n3.Multiply\n4.Divide\n");

scanf("%d", &choice);

printf("Enter first number : ");

scanf("%f", &a);

printf("Enter second number : ");

scanf("%f", &b);



//Cases

switch (choice) {

case 1:

res = add(a, b);

printf("The answer equals %f",res);

break;

case 2:

res = sub(a, b);

printf("The answer equals %f", res);

break;

case 3:

res = mul(a, b);

printf("The answer equals %f", res);

break;

case 4:

res = div(a, b);

printf("The answer equals %f", res);

break;

default:

printf("You did not select a valid operation from above.");

break;

}



return 0;

}

// to accept 2 as 2.0 we need to change the data types to float

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication,...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result. The program should continue running so the user can...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to...
Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to x to the power of y functionality. It should also have memory button, memory recall button and clear.
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my...
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my question above, please help me I need it to study.
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
What is the primary reason that addition and subtraction are considered more complex than multiplication and...
What is the primary reason that addition and subtraction are considered more complex than multiplication and division with floating-point representations? What are subnormal numbers, and how do subnormal numbers help reduce the impact of underflow?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT