Write a program for addition, subtraction, multiplication and division of two numbers in java

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.

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

Program:

import java.util.Scanner; public class Airthmatic {    public static void main(String[] args) {       Scanner  ed=new  Scanner(System.in);        int x,y;        int sum,mul,dvd,sub;        System.out.println("Enter Two Numbers = ");        x=ed.nextInt();        y=ed.nextInt();        sum=x+y;        mul=x*y;        dvd=x/y;        sub=x-y;        System.out.println("Results");        System.out.println("Addition = "+sum);        System.out.println("Multiplication = "+mul);        System.out.println("Division = "+dvd);        System.out.println("Subtraction = "+sub);     } }

Output:

Another Example :

import java.util.Scanner; public class Arithmatic { public static void main(String[] args) { int num1 , num2 ; Scanner input = new Scanner(System.in); System.out.print("Enter First number : "); num1 = input.nextInt(); System.out.print("Enter Second number : "); num2 = input.nextInt(); System.out.println(" Addition : " +(num1 + num2)+ ", Subtraction : "+(num1 - num2 )+", Multiplication : "+(num1 * num2 )+", Division : "+(num1 / num2)); } }

Output:

Enter First number : 6 Enter Second number : 2 Addition : 8, Subtraction : 4, Multiplication : 12, Division : 3

Java program for Addition, Subtraction, Multiplication and Division. Here we will discuss the most common mathematical operations such as addition, subtraction, multiplication and division In java. The compiler has been added as well so that you can execute the programs yourself, along with suitable examples and sample outputs. The programs as aforementioned are:

  • Addition.
  • Addition using Static Method.
  • Subtraction.
  • Multiplication.
  • Multiplication without using the Multiplication(*) operator.
  • Division.
  • Division without using the Division (/) operator.

Java Program – Addition

1) We are using the standard formula for adding two numbers.c=a+b.

2) Read the values using scanner object sc.nextInt() and store these values in the variables a,b and calculate addition of a,b and print the c value.

Addition Of Two Numbers Java Program

import java.util.Scanner;

class Add

{

public static void main(String[] arg)

{

int a,b;

Scanner sc=new Scanner(System.in);

System.out.println("Enter first number");

a=sc.nextInt();

System.out.println("Enter second number");

b=sc.nextInt();

int c=a+b;

System.out.println("Addition of two numbers is : "+c);

}

}

Addition(Using Static Method)

1) addition(int x, int y) is the static method, which calculates the addition of two numbers and returns the value.

2) addition(a,b) method calls at the main method then that static method calculates the addition of two numbers and returns the value and value will be assigned to the variable c.

Addition Of Two Numbers Java Program Using Static Method

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import java.util.Scanner;

class Add

{

public static void main(String[] arg)

{

int a,b,c;

Scanner sc=new Scanner(System.in);

System.out.println("Enter first number");

a=sc.nextInt();

System.out.println("Enter second number");

b=sc.nextInt();

c=addition(a,b);

System.out.println("Addition of two numbers is : "+c);

}

static int addition(int x,int y)

{

return x+y;

}

}

Subtraction Java Program

1) We are using the formula for subtraction is c=a-b.

2) Read the values using scanner object sc.nextInt() and store these values in the variables a,b. Subtract the smaller value from bigger value and result will be assigned to c and print the c value.

Subtraction Of Two Numbers Java Program

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import java.util.Scanner;

class Sub

{

public static void main(String[] arg)

{

int a,b,c;

Scanner s=new Scanner(System.in);

System.out.println("Enter first number");

a=s.nextInt();

System.out.println("Enter second number");

b=s.nextInt();

if(a>b)

c=a-b;

else

c=b-a;

System.out.println("Subtraction of two numbers is : "+c);

}

}

Java Multiplication Program

1) The formula for multiplication of two numbers is c=a*b.

2) Read the values using scanner object sc.nextInt() and store these values in the variables x,y and calculate multiplication of these numbers then print the z value.

Multiplication Of Two Numbers Java Program

import java.util.Scanner;

class Mul

{

public static void main(String[] args)

{

int x,y;

Scanner sc=new Scanner(System.in);

System.out.println("Enter first number");

x=sc.nextInt();

System.out.println("Enter first number");

y=sc.nextInt();

int z=x*y;

System.out.println("Multiplication of two numbers is : "+z);

}

}

Multiplication of two numbers without using the Multiplication(*) operator

1) We are calculating the multiplication of two numbers without using “*” operator.

2) Read the values using scanner object sc.nextInt() and store these values in the variables x,y.

3) If the condition at while x!=0 is true then z=(z+y) and x value decreased by 1, repeats until x!=0. If x=0 then while loop terminates then print the z value.

Multiplication Of Two Numbers Java Program Without Using * Operator

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import java.util.Scanner;

class Mul

{

public static void main(String[] args)

{

int x,y,z=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter first number");

x=sc.nextInt();

System.out.println("Enter first number");

y=sc.nextInt();

while(x!=0)

{

z+=y;

x--;

}

System.out.println("Multiplication of two numbers is : "+z);

}

}

Division

1) The standard formula for division of two numbers is  c=a/b where b!=0.

2) Read the numerator and denominator values using scanner object sc.nextInt() and store these values in the variables n,d and calculate the division of these numbers then print the “res” value.

Division Of Two Numbers Java Program

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import java.util.Scanner;

class Div

{

public static void main(String[] arg)

{

int n,d,res;

Scanner sc=new Scanner(System.in);

System.out.println("Enter numerator value");

n=sc.nextInt();

System.out.println("Enter denominator value");

d=sc.nextInt();

if(d!=0)

{

res=n/d;

System.out.println("Division of two numbers is : "+res);

}

else

System.out.println("denominator value should not be 0");

}

}

Division of two numbers without using the Division(/) operator

1) We are calculating the division of two numbers without using the “/” operator.

2) Read the numerator and denominator values using scanner object sc.nextInt()  and store these values in the variables a,b. if b!=0 then a=a-b, and c value increased by 1 until  the condition at  while a>=b is false.After all the iterations, it prints the c value.

Division Of Two Numbers Java Program Without Using / Operator

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import java.util.Scanner;

class Div

{

public static void main(String[] arg)

{

int a,b,c=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter numerator value");

a=sc.nextInt();

System.out.println("Enter denominator value");

b=sc.nextInt();

if(b!=0)

{

while(a>=b)

{

a=a-b;

c++;

}

System.out.println("Division of two numbers is : "+c);

}

else

System.out.println("denominator value should not be 0");

}

}

More Java Programs: