How to Add Two Numbers in Java

In This Tutorial, We will be going to Learn about How To Add Two Numbers in Java.

Initially, in Java with The Different ways available to us. Also, we will learn Taking Inputs in java or to initialize values in an array.

Adding two Numbers In java Can be done in different ways. The Addition is one of the Very Basic Arithmetic operations in various fields of our day to day life.

If you don’t know how to add two Nos. in java, then you are in the right place.

Because, in this tutorial, we will be going to deal with The Various Process through which addition can be done.

Different Situations That May Come During Addition of two numbers

There are Three Basic situations that may arise during the addition. These are:

  1. Adding of two Numbers Which are Given to User Before Only.
  2. Addition of two Numbers with the Inputs Provided by User with Use of ‘Scanner’ class.
  3. Addition of two Numbers With Inputs Provided by User with the Help of ‘BufferedReader’ method.

1: Addition of two Numbers Which are Given to User Before Only :

In this situation, we are Already Been Provided With Initial Numbers, we just have to gonna Add Both and display the Result. Here, We simply take Inputs As initial Values In Different Variables.




In this Situation, we maybe Provided with Any Integer Values, So we add Them and display it.

class Add1
{
  public static void main(String Args[])
  {
    int n1=4,n2=8,sum=0;
    sum=(n1+n2);
    System.out.println("Sum of Provided Numbers,i.e, 4 and 8 is :"+sum);
  }
}

2: Add two numbers in java with the Use of ‘Scanner’ class :

In this method, we Will Be taking input of two numbers from the User With The Help of ‘Scanner’ class.

Then We will be Adding Them and Display The Corresponding Result that is the addition of two variables.

Initially, we need to Learn what ‘Scanner’ class Actually is.

The Scanner is a class Present in ‘java.util’ , a package which is used for obtaining the input, or taking Input of the primitive types like integer, float, double, etc. and strings also many other datatypes.

In our example, we will use the ‘nextInt’ method, which is used to read The Integer Numbers.

Similarly, different methods can be used to Take Different type of Values that is for Strings or Double. For Example, ‘nextFloat’, which is used for taking input of Float values.

Syntax For creating an object for above  is:

  • Scanner Object_name = new Scanner (System.in);

Below Is An example of this Type method :

import java.util.*;
class Add2
{
  public static void main(String Args[])
  {
    int n1,n2,sum=0;
    Scanner sc = new Scanner (System.in);
    System.out.println("Enter First Number: ");
    n1 = sc.nextInt();
    System.out.println("Enter Second Number: ");
    n2 = sc.nextInt();
    sum=(n1+n2);
    System.out.println("Sum of Provided Numbers is :"+sum);
  }
}

When The Above Code gets to execute, the User will be asked to get the input of Two Numbers. Then, the user will provide inputs and the corresponding output would come.

3: Add two Numbers in Java With the Help of ‘BufferedReader’ method:

Initially, we need to know What Basically is ‘BufferedReader’ class.

In Java, ‘BufferedReader’ class is used to read the text, buffering characters so as to provide a systematic reading of arrays and lines.

BufferedReader is much more Synchronized than The ‘Scanner’. Also, it has a larger Default Value of Buffer. In this, it can be used to read the data line by line by the ‘readLine()’ method.

Basically, It inherits Reader Class.

Syntax for Above is : BufferedReader object = new BufferedReader (new InputStreamReader(System.in));

import java.io.*;
class Add3 
{
 	    public static void main(String args[]) throws Exception
            {
      	    int n1, n2, sum;
      	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the two numbers to add:");
            n1 = Integer.parseInt(br.readLine());
            n2 = Integer.parseInt(br.readLine());
            sum = (n1+n2);
            System.out.println("\nSum of two numbers:" + sum);
      	}
}

When the above code Executed, It will ask for two nos. from the user and correspondingly adds two numbers and displays it.

Also, read:

Decimal to Binary conversion in JAVA


Leave a Reply

Your email address will not be published. Required fields are marked *