Java Program to perform addition and Substraction of 2 numbers – 5 ways

Arithmetic Operator:

An Operator is a symbol that tells the computer to perform various operation like Mathematical or Logical Operations. Arithmetic operators are used to Construct mathematical expressions. Java Provides different types of Arithmetic operators. They are Addition, Substraction, Multiplication, and Division.

   The Process of programming in java is divided into 3 phases:

  • 1. Writing Simple Java Program:
  • Create a simple java Program by typing it into a text editor. Then save the program with the same name as that of the class name. You can wrote simple java application in Eclipse also.
  • 2. Compiling a Java Source File:
  • After Completion of Java program, open Command prompt. Type the Command “ javac filename.java ” . If their is  any error then remove first.
  • 3. Running a Java Application:
  • To run java Application in the command prompt ,type ” Filename.java” .

Their are different ways to perform addition and substraction in java. You can use Scanner class, Array, Direct value initialization, Buffered Reader Class and without using Addition(+) oprator to perform addition of numbers in java. Below is the java program to perform addition and substraction of two numbers.

1. Addition of 2 numbers using Scanner Class:

Program:

import java.io.*;
import java.lang.*;
import java.util.*;
class Addition
{
public static void main(String args[])
{
Scanner c= new Scanner(System.in);
System.out.println("Enter value of 'x'");
int x= c.nextInt();
System.out.println("Enter value of 'y'");
int y= c.nextInt();
int z= x+y;
System.out.println("The addition of 2 number is"+z);
}
}

Output:

Enter value of 'x' 30
Enter value of 'Y' 20
The addition of 2 number is 50

 

2. Addition of Two Number using Direct Method:

Program:

import java.io.*;
import java.lang.*;

class Addition
{
public static void main(String args[])
{
int x=5, y=6;
int z= x+y;
System.out.println("The addition of 2 number is"+z);
}
}

Output:

The addition of 2 number is 11

 

3. Addition Of Two Number using Buffered Reader class:

Program:

import java.io.*;
import java.lang.*;
Class Addition
{
public static void main(String args[])
{
int a, b, c;
BufferedReader br= new BufferedReader(new inputStreamReader(System.in));
System.out.println("Enter the value of a");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the value of b");
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println("The addition of Two Number is "+c);
}
}

Output:

Enter the value of a 
5
Enter the value of b
5
The addition of Two Number is 10

 

4. Addition of  Numbers Using Array:

Program:

import java.io.*;
import java.lang.*;
Class Addition
{
public static void main(String args[])
{
int sum=0;
int[] arr={1, 2, 3, 4, 5);
for( int n : arr)
{
sum=sum+n;
}
System.out.println("The addition is "+sum);
}
}

Output:

The addition is 15

 

5. Addition of Two Numbers without Using Addition(+) Operator :

Program:

import java.io.*;
import java.lang.*;
import java.util.*;
class Addition
{
public static void main(String args[])
{
Scanner c= new Scanner(System.in);
System.out.println("Enter value of 'x'");
int x= c.nextInt();
System.out.println("Enter value of 'y'");
int y= c.nextInt();
while(x--!=0)
{
y++;
}
System.out.println("The addition of 2 number is"+y);
}
}

Output:

Enter value of 'x' 
10
Enter value of 'y'
10
The addition of 2 number is 20

6. Substraction of Two Number using Direct Method:

Program:

import java.io.*;
import java.lang.*;

class Substraction
{
public static void main(String args[])
{
int x=10, y=6;
int z= x-y;
System.out.println("The Substraction of 2 number is"+z);
}
}

Output:

The substraction of 2 number is 4

7. Substraction of two number using Scanner Class:

Program:

import java.io.*;
import java.lang.*;
import java.util.*;
class Substraction
{
public static void main(String args[])
{
Scanner c= new Scanner(System.in);
System.out.println("Enter value of 'x'");
int x= c.nextInt();
System.out.println("Enter value of 'y'");
int y= c.nextInt();
int z= x-y;
System.out.println("The Substraction of 2 number is"+z);
}
}

Output:

Enter value of 'x'
10
Enter value of 'y'
4
The Substraction of 2 number is 6

8. Substraction of two number using Buffered Reader class:

Program:

import java.io.*;
import java.lang.*;
Class Substraction
{
public static void main(String args[])
{
int a, b, c;
BufferedReader br= new BufferedReader(new inputStreamReader(System.in));
System.out.println("Enter the value of a");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the value of b");
b=Integer.parseInt(br.readLine());
c=a-b;
System.out.println("The Substraction of Two Number is "+c);
}
}

Output:

Enter the value of a
15
Enter the value of b 
10
The Substraction of Two Number is 5

Exercises:

Find the Output of the below code ?

Exercise 1:

import java.io.*;
import java.lang.*;

class Substraction
{
public static void main(String args[])
{
int x=20, y=6;
int z= x-y;
System.out.println("The Substraction of 2 number is"+z);
}
}

Exercise 2:

import java.io.*;
import java.lang.*;

class Addition
{
public static void main(String args[])
{
int x=10, y=6;
int z= x+y;
System.out.println("The Addition of 2 number is"+z);
}
}

 

Latest Post:

Difference between Class and Object in java ?

Difference between Abstract Class and Interface in java?

Java program to print ” Hello World” message.

Leave a Comment