Simple Calculator Using If Else

Description

In this program, user will enter operator(+,-,*,/) and two operands.
It will give output based on entered operator.

C/C++

/* C Program of Simple Calculator Using If Else */
//Save it as SimpleCalculatorUsingIfElse.c

#include<stdio.h>
int main(){

    double firstNumber, secondNumber;
    char op;

    printf("Enter the first number : ");
    scanf("%lf",&firstNumber);

    printf("Enter the second number : ");
    scanf("%lf",&secondNumber);

    printf("Enter the operation");
    printf("\nFor addition + ");
    printf("\nFor subtraction - ");
    printf("\nFor multiplication * ");
    printf("\nFor division / : ");
    scanf(" %c",&op);

    if(op == '+') {
        printf("The summation of %lf and %lf = %lf",firstNumber,secondNumber,(firstNumber+secondNumber));
    }else if(op == '-') {
        printf("The subtraction of %lf and %lf = %lf",firstNumber,secondNumber,(firstNumber-secondNumber));
    }else if(op == '*') {
        printf("The multiplication of %lf and %lf = %lf",firstNumber,secondNumber,(firstNumber*secondNumber));
    }else if(op == '/') {
        printf("The division of %lf and %lf = %lf",firstNumber,secondNumber,(firstNumber/secondNumber));
    }else{
        printf("Invalid Input");
    }

    return 0;
}
Input:
Enter the first number : 
7
Enter the second number : 
6
Enter the operation
For addition + 
For subtraction - 
For multiplication * 
For division / : 
*

Output:
The multiplication of 7.0 and 6.0 = 42.0
Simple Calculator

Java

/* Java Program of Simple Calculator Using If Else */
//Save it as SimpleCalculatorUsingIfElse.java

import java.io.*;
import java.util.Scanner;

public class SimpleCalculatorUsingIfElse {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the first number : ");
        double firstNumber = scanner.nextDouble();
        
        System.out.println("Enter the second number : ");
        double secondNumber = scanner.nextDouble();
        
        System.out.println("Enter the operation");
        System.out.println("For addition + ");
        System.out.println("For subtraction - ");
        System.out.println("For multiplication * ");
        System.out.println("For division / : ");
        
        char op = scanner.next().charAt(0);
        
        if(op == '+') {
            System.out.println("The summation of "+firstNumber+" and "+secondNumber+" = "+(firstNumber+secondNumber));
        }else if(op == '-') {
            System.out.println("The subtraction of "+firstNumber+" and "+secondNumber+" = "+(firstNumber-secondNumber));
        }else if(op == '*') {
            System.out.println("The multiplication of "+firstNumber+" and "+secondNumber+" = "+(firstNumber*secondNumber));
        }else if(op == '/') {
            System.out.println("The division of "+firstNumber+" and "+secondNumber+" = "+(firstNumber/secondNumber));
        }else {
            System.out.println("Invalid Input");
        }
    }
}
Input:
Enter the first number : 
7
Enter the second number : 
9
Enter the operation
For addition + 
For subtraction - 
For multiplication * 
For division / : 
+

Output:
The summation of 7.0 and 9.0 = 16.0
Output
Simple Calculator

Related Programs

1) Menu Driven Simple Calculator Using If Else
2) Program of Simple Calculator using switch
3) Program to calculate factorial using Recursion
4) Program to Check Leap Year
5) Program to find the average of numbers in a given range
6) Program to calculate Gross Salary
7) Program to find modulus of two numbers
8) Program to Display Fibonacci Series
9) Program to Display Fibonacci Series using Recursion
10) Program to find LCM(Least Common Multiple)
11) Program to Swap Two Numbers
12) Program to calculate power of a number
13) Program to Check Whether a Number is Prime or Not
14) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)
Share Me

Leave a Reply