Answer to Question #308227 in C for ahmed

Question #308227

Write a program to make a simple calculator using switch-case. The calculator takes the operation (+ or – or * or /) and takes the two input arguments and print the results.


1
Expert's answer
2022-03-13T08:57:46-0400
#include <stdio.h>

float calc(float a, float b, char oper)
{
    float rez;
    switch(oper){
    case '+':
        rez = a + b;
        break;
    case '-':
        rez = a - b;
        break;
    case '*':
        rez = a * b;
        break;
    case '/':
        rez = a / b;
        break;
    }
    return rez;
}

int main()
{
    char oper;
    float a, b;
        
    printf("Enter argument 1: ");    
    scanf("%f", &a);
    printf("Enter argument 2: ");    
    scanf("%f", &b);
    getchar();
    printf("Enter operation (+ or - or * or /): ");    
    oper = fgetchar();
    getchar();
    printf("\nResult:\n");
    printf("\n%.1f %c %.1f = %.2f", a, oper, b, calc(a, b, oper));
 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS