articles – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 09:51:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg articles – Programmerbay https://programmerbay.com 32 32 Java program to Add, Subtract, Multiply and Divide two numbers https://programmerbay.com/java-program-to-add-subtract-multiply-and-divide-two-numbers/ https://programmerbay.com/java-program-to-add-subtract-multiply-and-divide-two-numbers/#respond Sat, 11 Jun 2022 09:26:27 +0000 https://www.programmerbay.com/?p=2330 Given a Java Program to add, subtract, multiply and divide two numbers. The program accepts two numbers as input from a user on which basic arithmetic operations ( Addition, Multiplication, division, and subtraction ) would be performed. In other words, some basic arithmetic operations have performed.

In this article, we’ll be implementing programs to perform basic arithmetic operations on two number with the following ways:

  1. Simply performing the operations and printing on the output screen
  2. Using Do while with switch statement
  3. Using method

Here’s the snippet to calculate sum, division, multiplication and subtraction of two numbers.

Example 1. Program for addition, subtraction, multiplication and division of two numbers in Java

Program:

import java.util.Scanner;

/*
* Simple Program for addition subtraction multiplication division
* */
public class ArithmeticOperationExample {
    public static void main(String[] args) {

        Scanner ed=new  Scanner(System.in);
        int x,y;
        int sumResult,mulResult,dvdResult,subtractResult;
        System.out.print("Enter First Numbers = ");
        x=ed.nextInt();
        System.out.print("Enter Second Numbers = ");
        y=ed.nextInt();
        sumResult=x+y;
        mulResult=x*y;
        dvdResult=x/y;
        subtractResult=x-y;
        
        System.out.println("Results");
        System.out.println("Addition = "+sumResult);
        System.out.println("Multiplication = "+mulResult);
        System.out.println("Division = "+dvdResult);
        System.out.println("Subtraction = "+subtractResult);

    }
}

Output:

Enter First Numbers = 4
Enter Second Numbers = 2
Results
Addition = 6
Multiplication = 8
Division = 2
Subtraction = 2

Example 2. Java program for addition, subtraction, multiplication and division of two numbers using Do While with Switch statement

Program: 

package generics;

import java.util.Scanner;

/*
 * Program for addition subtraction multiplication division using switch statement
 * */
public class ArithmeticOperationExample {
    public static void main(String[] args) {
        int num1, num2, expr;
        char ch;
        Scanner ed = new Scanner(System.in);

        System.out.println("Please select number to perform an operation");
        System.out.println("1 for Addition ");
        System.out.println("2 for Subtraction ");
        System.out.println("3 for Multiplication ");
        System.out.println("4 for Division ");
        do{
            System.out.print("Please enter an option = ");
            expr = ed.nextInt();
            switch (expr){
                case 1:
                    System.out.print("Please enter first number  = ");
                    num1 = ed.nextInt();
                    System.out.print("Please enter second number  = ");
                    num2 = ed.nextInt();
                    System.out.println("Total sum = "+  (num1+num2));
                    break;
                case 2:
                    System.out.print("Please enter a number = ");
                    num1 = ed.nextInt();
                    System.out.print("Please enter a number that needs to be subtracted = ");
                    num2 = ed.nextInt();
                    System.out.println("Subtraction result = "+  (num1-num2));
                    break;
                    case 3:
                        System.out.print("Please enter a number = ");
                        num1 = ed.nextInt();
                        System.out.print("Please enter a number that needs to be multiplied = ");
                        num2 = ed.nextInt();
                        System.out.println("Multiplication result = "+  (num1*num2));
                    break;
                case 4:
                    System.out.print("Please enter a dividend = ");
                    num1 = ed.nextInt();
                    System.out.print("Please enter divisor = ");
                    num2 = ed.nextInt();
                    System.out.println("Division result = "+  (num1/num2));
                    break;
                default:
                    System.out.println("Please select valid option !");
            }
            System.out.println("Want to continue? y or n ");
            ch=ed.next().charAt(0);
        }while(ch == 'y' || ch == 'Y');
    }
}

Output:

Please select number to perform an operation
1 for Addition 
2 for Subtraction 
3 for Multiplication 
4 for Division 
Please enter an option = 1
Please enter first number  = 4
Please enter second number  = 6
Total sum = 10
Want to continue? y or n 
y
Please enter an option = 2
Please enter a number = 6
Please enter a number that needs to be subtracted = 3
Subtraction result = 3
Want to continue? y or n 
y
Please enter an option = 3
Please enter a number = 5
Please enter a number that needs to be multiplied = 2
Multiplication result = 10
Want to continue? y or n 
y
Please enter an option = 4
Please enter a dividend = 120
Please enter divisor = 6
Division result = 20
Want to continue? y or n 
n

Example 2. Java program for addition, subtraction, multiplication, division program using methods

Program:

public class ArithmeticOperationExample {
    public static void main(String[] args) {
        ArithmeticOperationExample obj = new ArithmeticOperationExample();
        int num1, num2;
        Scanner ed = new Scanner(System.in);
        System.out.print("Please enter a number = ");
        num1 = ed.nextInt();
        System.out.print("Please second number = ");
        num2 = ed.nextInt();

        System.out.println("Multiplication = "+obj.multiply(num1,num2));
        System.out.println("Addition = "+obj.add(num1,num2));
        System.out.println("Subtraction = "+obj.subtract(num1,num2));
        System.out.println("Division = "+obj.divide(num1,num2));
    }

    int multiply(int x, int y){
        return x*y;
    }

    int add(int x, int y){
        return x+y;
    }

    int subtract(int x, int y){
        return x-y;
    }

    int divide(int x, int y){
        return x/y;
    }
}

Output:

Please enter a number = 10
Please second number = 2
Multiplication = 20
Addition = 12
Subtraction = 8
Division = 5

 

]]>
https://programmerbay.com/java-program-to-add-subtract-multiply-and-divide-two-numbers/feed/ 0