×

Please Login or Register to continue.

1
(1.1K Views)

I am learning the C programming language. I want to create a simple calculator program using the C language. The program should be working for the operations like addition, subtraction, multiplication, division, and modules. I want to create a calculator for two numbers in the C programming language. Is it possible to create a program for a simple calculator in C? 

(1K Points)
in C Language

Share

1 Answer
(3.4K Points)
(edited)
0

You can create a simple calculator program in C programming using switch statements. The switch statement is used to create multiple options with the case. Every case will contain an operation for the calculator and if any case matches with a switch expression that case code will be executed.

Let's create a C program to create a simple calculator  - 

How to create a program to create a calculator in C programming? 

The calculator is used for addition, subtraction, multiplication, and division. You will need to use operators in the c program.  

1. Declare a sign variable with char data type for calculator operation. 

2. Declare two variables like - no1 and no2. We will take input numbers and sign from the user. 

3. Create cases for operations using only sign (operators) (+, -, *, /): 

4. Execute the calculator program 

// A simple C program for calculator 
#include <stdio.h>
int main() {
    double no1, no2;
        char sign;
         printf("Enter a sign for calculation ie (+, -, *, /): ");
    scanf("%c", &sign);
printf("Please enter two numbers: ");
    scanf("%lf %lf",&no1, &no2);
  
    switch(sign)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",no1, no2, no1+no2);
            break;

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

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

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

    
        default:
            printf("Something wrong or sign is not correct.");
    }

    return 0;
}

Output - 

Enter a sign for calculation ie (+, -, *, /): -
Please enter two numbers: 12
5
12.0 - 5.0 = 7.0

Your C program is ready. Now, you will be able to perform addition, subtraction, multiplication, and division operations using the calculator program in C programming. 


Comment


Related Questions :-

Your Answer

You can post your answer after login..

Live Chat

Hello! How can we assist you today?