Write a java program which accepts two numbers from user as input and perform arithmetic operation

View Discussion

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: 

    This article explains all that one needs to know regarding Arithmetic Operators. 

    These operators involve the mathematical operators that can be used to perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators. 

    Write a java program which accepts two numbers from user as input and perform arithmetic operation

    Now let’s look at each one of the arithmetic operators in Java: 

    1. Addition(+): This operator is a binary operator and is used to add two operands.

    Syntax: 

    num1 + num2

    Example: 

    num1 = 10, num2 = 20 sum = num1 + num2 = 30

    import java.io.*;

    class Addition {

        public static void main(String[] args)

        {

            int num1 = 10, num2 = 20, sum = 0;

            System.out.println("num1 = " + num1);

            System.out.println("num2 = " + num2);

            sum = num1 + num2;

            System.out.println("The sum = " + sum);

        }

    }

    Outputnum1 = 10 num2 = 20 The sum = 30

    2. Subtraction(-): This operator is a binary operator and is used to subtract two operands. 

    Syntax: 

    num1 - num2

    Example: 

    num1 = 20, num2 = 10 sub = num1 - num2 = 10

    import java.io.*;

    class Subtraction {

        public static void main(String[] args)

        {

            int num1 = 20, num2 = 10, sub = 0;

            System.out.println("num1 = " + num1);

            System.out.println("num2 = " + num2);

            sub = num1 - num2;

            System.out.println("Subtraction = " + sub);

        }

    }

    Outputnum1 = 20 num2 = 10 Subtraction = 10

    3. Multiplication(*): This operator is a binary operator and is used to multiply two operands. 

    Syntax: 

    num1 * num2

    Example: 

    num1 = 20, num2 = 10 mult = num1 * num2 = 200

    import java.io.*;

    class Multiplication {

        public static void main(String[] args)

        {

            int num1 = 20, num2 = 10, mult = 0;

            System.out.println("num1 = " + num1);

            System.out.println("num2 = " + num2);

            mult = num1 * num2;

            System.out.println("Multiplication = " + mult);

        }

    }

    Outputnum1 = 20 num2 = 10 Multiplication = 200

    4. Division(/): This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as a result. 

    Syntax: 

    num1 / num2

    Example: 

    num1 = 20, num2 = 10 div = num1 / num2 = 2

    import java.io.*;

    class Division {

        public static void main(String[] args)

        {

            int num1 = 20, num2 = 10, div = 0;

            System.out.println("num1 = " + num1);

            System.out.println("num2 = " + num2);

            div = num1 / num2;

            System.out.println("Division = " + div);

        }

    }

    Outputnum1 = 20 num2 = 10 Division = 2

    5. Modulus(%): This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor). 

    Syntax: 

    num1 % num2

    Example: 

    num1 = 5, num2 = 2 mod = num1 % num2 = 1

    import java.io.*;

    class Modulus {

        public static void main(String[] args)

        {

            int num1 = 5, num2 = 2, mod = 0;

            System.out.println("num1 = " + num1);

            System.out.println("num2 = " + num2);

            mod = num1 % num2;

            System.out.println("Remainder = " + mod);

        }

    }

    Outputnum1 = 5 num2 = 2 Remainder = 1


    In order to continue enjoying our site, we ask that you confirm your identity as a human. Thank you very much for your cooperation.

    Last update on August 19 2022 21:50:54 (UTC/GMT +8 hours)

    Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers.

    Test Data: Input first number: 125

    Input second number: 24

    Pictorial Presentation:

    Write a java program which accepts two numbers from user as input and perform arithmetic operation

    Sample Solution:

    Java Code:

    import java.util.Scanner; public class Exercise6 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input first number: "); int num1 = in.nextInt(); System.out.print("Input second number: "); int num2 = in.nextInt(); System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); System.out.println(num1 + " x " + num2 + " = " + (num1 * num2)); System.out.println(num1 + " / " + num2 + " = " + (num1 / num2)); System.out.println(num1 + " mod " + num2 + " = " + (num1 % num2)); } }

    Sample Output:

    Input first number: 125 Input second number: 24 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5 125 mod 24 = 5

    Flowchart:

    Write a java program which accepts two numbers from user as input and perform arithmetic operation

    Sample Solution:

    Java Code:

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Input the first number: "); int n1 = scanner.nextInt(); System.out.println("Input the second number: "); int n2 = scanner.nextInt(); int sum = n1 + n2; int minus = n1 - n2; int multiply = n1 * n2; int subtract = n1 + n2; int divide = n1 / n2; int rnums = n1 % n2; System.out.printf("Sum = %d\nMinus = %d\nMultiply = %d\nSubtract = %d\nDivide = %d\nRemainderOf2Numbers = %d\n ", sum, minus, multiply, subtract, divide, rnums); } }

    Sample Output:

    Input the first number: 6 Input the second number: 5 Sum = 11 Minus = 1 Multiply = 30 Subtract = 11 Divide = 1 RemainderOf2Numbers = 1

    Flowchart:

    Write a java program which accepts two numbers from user as input and perform arithmetic operation

    Java Code Editor:

    Contribute your code and comments through Disqus.

    Previous: Write a Java program that takes two numbers as input and display the product of two numbers.
    Next: Write a Java program that takes a number as input and prints its multiplication table upto 10.

    What is the difficulty level of this exercise?

    Test your Programming skills with w3resource's quiz.

    Array vs ArrayLists:

    The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

    Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

    At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

    Ref: https://bit.ly/3o8L2KH