Tech Study

Write C program to create simple calculator using switch Statement

Introduction

Write C program to create calculator using switch Statement

# include <stdio.h>

int main() {

    char operator;
    double firstNumber,secondNumber;

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

   //Reading operands from user
    printf("Enter two operands: ");
    scanf("%lf %lf",&firstNumber, &secondNumber);

    switch(operator)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! please enter correct operator");
    }

    return 0;
}

Result

Write C program to create calculator using switch Statement
Write C program to create calculator using switch Statement

TaggedWrite C program to create simple calculator using switch Statement

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more