Java program to add, subtract, multiply and divide two numbers
Java Programming

Java program to add, subtract, multiply and divide two numbers

 

The switch statement is a multi-way decision-making statement in which we can select only one . The switch statement starts with the “switch” keyword like (switch(expression)). Switch defines one or more case values which we know as case labels. Case labels consist of case keywords followed by a colon(:), and every case is terminated by a break statement.

Example 1. Java Program for Addition, subtraction, multiplying, and dividing two numbers using switch(Scanner class)

import java.util.Scanner;

class menu

{

public static void main(String asa[])

{

int aa, bb,choicee;

//create Scanner(Scanner class) object to object input from  keyboard(Run Time Value)

Scanner inputt=new Scanner(System.in);

System.out.print(“Enter 2 (aa,bb)nums……”);

aa=inputt.nextInt();       //read the first number(aa)

bb=inputt.nextInt();       //read the second number(bb)

System.out.println(“1. ADDITION”);

System.out.println(“2. SUBTRACTION”);

System.out.println(“3. MULTIPLICATION”);

System.out.println(“4. DIVISON”);

System.out.println(“Pls enter the no. btw 1 to 4 for calculation(arithmetic +,-,*,%,/)”);

choicee=inputt.nextInt();

switch(choicee)

{

case 1:

       System.out.println(“Sum ===”+(aa+bb));

       break;

case 2:

       System.out.println(“Sub ===”+(aa-bb));

       break;

case 3:

       System.out.println(“product ===”+(aa*bb));

       break;

case 4:

       System.out.println(“Div===”+(aa/bb));

       break;

default:

      System.out.println(“Enter number btw 1 to 4”);

}

}

}

Arithmetic calculation using a switch

Explanation….

In this example, the user is prompted to input two numbers(aa and bb) and the values of choice whose value determines the type of the operation. The value of the choice entered by the user is compared with each case in the order they appear in the switch statement. When a match occurs, the statement to that case is executed and the break statement causes the immediate exit from the switch statement. If the value of the choice doesn’t match then the default case is executed.

Example 2. Java Program for sum, subtract, multiply and divide two numbers using switch(Command Line Arguments)

class arithmetic

{

public static void main(String ar[])

{

int first,sec,thrd;

first=Integer.parseInt(ar[0]);

sec=Integer.parseInt(ar[1]);

thrd=Integer.parseInt(ar[2]);

System.out.println(” (int first,sec,thrd)first variable is for switch and sec &thrd for calculation”);

switch(first)

{

                case 1:

                                System.out.println(“addition=”+(sec+thrd));

                                break;

                case 2:

                                System.out.println(“subtraction=”+(sec-thrd));

                                break;

                case 3:

                                System.out.println(“multiplication=”+(sec*thrd));

                                break;

                case 4:

                                System.out.println(“divison=”+(sec/thrd));

                                break;

                case 5:

                                System.out.println(“mod=”+(sec%thrd));

                                break;

                default:

                                System.out.println(“pls enter numeric value”);

}}}

Arithmetic calculation(command line arguments)

Example 3. Java Program for Addition, subtraction, multiplying and dividing two numbers using a switch(without break)

class arithmetic1

{

public static void main(String ar[])

{

int first,sec,thrd;

first=Integer.parseInt(ar[0]);

sec=Integer.parseInt(ar[1]);

thrd=Integer.parseInt(ar[2]);

System.out.println(” (int first,sec,thrd)first  variable is for switch and sec &thrd for calculation”);

switch(first)

{

                case 1:

                                System.out.println(“addition=”+(sec+thrd));

                case 2:

                                System.out.println(“subtraction=”+(sec-thrd));

                case 3:

                                System.out.println(“multiplication=”+(sec*thrd));

                case 4:

                                System.out.println(“divison=”+(sec/thrd));

                case 5:

                                System.out.println(“mod=”+(sec%thrd));

                                break;

                default:

                                System.out.println(“pls enter numeric value”);

}

}

}

Arithmetic calculation using the switch statement

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment