Calculator using switch-case in java .

■》》Ved varshney ♤♧

■》input->

import java.util.*;
public class calculator_switch_case {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter two numbers=");
    int a=sc.nextInt();
    int b=sc.nextInt();
    System.out.println("press 1 for additon.");
    System.out.println("press 2 for subtraction.");
    System.out.println("press 3 for multiplication.");
    System.out.println("press 4 for division.");
    System.out.println("press 5 for modulus.");
    System.out.println("enter your choice=");
    int choice=sc.nextInt();
    switch (choice) {
        case 1:
            System.out.println("the addition is="+(a+b));
            break;
        case 2:
            System.out.println("the subtraction is="+(a-b)); 
            break;
        case 3:
            System.out.println("the multiplication is="+(a*b)); 
            break;
        case 4:
            if(b!=0)
            System.out.println("the division is="+(a/b));
            else 
            System.out.println("division is not possible.");
            break;
        case 5:
            if(b!=0)
            System.out.println("the modulus is="+(a%b));
            else 
            System.out.println(" is not possible.");
            break;
        default:
            System.out.println("invalid choice.");
    }
}
}
                   
■》Output->

 enter two numbers= 4
                                  5
 press 1 for addition.
 press 2 for subtraction.
 press 3 for multiplication.
 press 4 for division.
 press 5 for modulus.
 enter your choice= 4
the multiplication  is =20

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.