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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.
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.
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.
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: