Knowee
Questions
Features
Study Tools

.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.

Question

.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that does what you asked for:

#include <stdio.h>

int main() {
    int num1, num2;
    char operator;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator);

    printf("Enter second number: ");
    scanf("%d", &num2);

    switch(operator) {
        case '+':
            printf("%d %c %d = %d\n", num1, operator, num2, num1 + num2);
            break;
        case '-':
            printf("%d %c %d = %d\n", num1, operator, num2, num1 - num2);
            break;
        case '*':
            printf("%d %c %d = %d\n", num1, operator, num2, num1 * num2);
            break;
        case '/':
            if(num2 != 0)
                printf("%d %c %d = %.2f\n", num1, operator, num2, (float)num1 / num2);
            else
                printf("Error! Division by zero is not allowed.\n");
            break;
        default:
            printf("Error! Invalid operator.\n");
    }

    return 0;
}

This program first prompts the user for two integers and an operator. It then uses a switch case to determine which operation to perform based on the operator entered by the user. The result of the operation is then printed to the console. If the user tries to divide by zero or enters an invalid operator, an error message is printed.

This problem has been solved

Similar Questions

Single File Programming QuestionProblem StatementJohn's little brother is struggling with math. He decided to design a calculator with basic operations such as addition, subtraction, and multiplication.The calculator should receive two numbers and an operator from the user. It should operate according to the operator entered and display the result. Help him implement the calculator using the Switch statement.Input format :The first line consists of two space-separated integers num1 and num2.The second line consists of a character, representing the operator: + for addition, - for subtraction, * for multiplicationOutput format :The output displays an integer representing the result after the operation is performed.If the operator is invalid, the output displays "INVALID".Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ num1, num2 ≤ 100Sample test cases :Input 1 :1 100 +Output 1 :101Input 2 :2 95 -Output 2 :-93Input 3 :34 56 *Output 3 :1904Input 4 :1 2 ^Output 4 :INVALID

#include<stdio.h>int main(){int a;scanf("%d",&a);int B=a%2;int C=a*a*a;int D=a*a;switch(B){case 0:printf("Even\n");printf("%d\n",C);break;case 1:printf("Odd\n");printf("%d\n",D);break;}return 0;}

Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.

Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function

Single File Programming QuestionProblem StatementBrian is an enthusiastic learner diving into the world of programming! Brian is experimenting with unary operators and wants to understand their impact on a given variable.Can you help him by writing a program that takes an integer as input, performs a series of unary operations a = ++a * --a, and then displays the final value?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.