Java Program To Add Two Numbers

Hey guys!! Welcome to flower brackets blog. Java program to add two numbers is one of the common java interview question.

Also Read – Java Calculate Percentage

In this java program to add two numbers we are taking input from user. Because we are asking user to input values to add two numbers in java.

Let’s see an example on addition of two numbers in java,

example: Java Program To Add Two Numbers

import java.util.Scanner;

public class AddTwoNumbers
{
   public static void main(String[] args)
   {
      int a, b, c;
      System.out.println("Please enter two integers to add : ");
      Scanner sc = new Scanner(System.in);

      a = sc.nextInt();
      b = sc.nextInt();
      c = a + b;

      System.out.println("Sum of Two Integers = " + c);
      sc.close();
   }
}

Output:

Java Program To Add Two Numbers


biginteger : Java Program To Add Two Numbers

Let’s see how to add numbers in java using basic math function using java biginteger. Here’s the code implementation,

import java.math.BigInteger;
import java.util.Scanner;

public class BigIntegerDemo
{
   public static void main(String[] args)
   {
      BigInteger a, b;
      System.out.println("Please enter two bigintegers numbers : ");
      Scanner sc = new Scanner(System.in);
      a = sc.nextBigInteger();
      b = sc.nextBigInteger();

      BigInteger add, sub, mul, div, mod, rem, abs;

      add = a.add(b);
      System.out.println("Addition - " + add);

      sub = a.subtract(b);
      System.out.println("Subtraction - " + sub);

      mul = a.multiply(b);
      System.out.println("Multiplication - " + mul);

      div = a.divide(b);
      System.out.println("Multiplication - " + div);

      mod = a.mod(b);
      System.out.println("Modulus - " + mod);

      rem = a.remainder(b);
      System.out.println("Remainder - " + rem);

      abs = a.abs();
      System.out.println("Absolute - " + abs);

      sc.close();
   }
}

In big integer the division will divide only integer value.

Output:

Please enter two big integers numbers: 

13156949
23116

Addition - 13180065
Subtraction - 13133833
Multiplication - 304136033084
Multiplication - 569
Modulus - 3945
Remainder - 3945
Absolute - 13156949


using method

Here let’s see sum or addition of two numbers in java using method.

import java.util.Scanner;

public class AddTwoNumbers
{
   public static void main(String[] args)
   {
      int number1, number2, sum;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number : ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number : ");
      number2 = sc.nextInt();
      // adding two integers in java
      sum = addTwoNumbers(number1, number2);
      System.out.println("Adding two integers in java : " + sum);
      sc.close();
   }
 
   static int addTwoNumbers(int a, int b)
   {
      return a + b;
   }
}

Output:

Please enter first number : 10
Please enter second number : 4
Adding two integers in java : 14


using class and objects

Let’s learn addition program in java using classes and objects. Here’s the code,

import java.util.Scanner;

public class AddTwoNumbersDemo
{
   public static void main(String[] args)
   {
      int number1, number2;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number : ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number : ");
      number2 = sc.nextInt();
      Add a = new Add(number1, number2);
      System.out.println("Adding numbers in java : " + a.sum);
      sc.close();
   }
}

public class Add
{
   int sum = 0;
   Add(int a, int b)
   {
      sum = a + b;
   }
}

Output:

Please enter first number : 5
Please enter second number : 5
Adding numbers in java : 10


while loop

Let’s see addition of two numbers in java using while loop,

import java.util.Scanner;

public class AddTwoNumbersExample
{
   public static void main(String[] args)
   {
      int number1, number2;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number : ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number : ");
      number2 = sc.nextInt();
      while(number1-- != 0)
      {
         number2++;
      }
      System.out.println("Addition of two numbers in java : " + number2);
      sc.close();
   }
}

Output:

Please enter first number : 50
Please enter second number : 50
Addition of two numbers in java : 100


using bufferedreader

Let’s learn how to add two numbers in java using bufferedreader,

import java.io.*;
import java.io.IOException;

public class AddNumbersDemo
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter first number : ");
      int number1 = Integer.parseInt(br.readLine());
      System.out.println("Please enter second number : ");
      int number2 = Integer.parseInt(br.readLine());
      int sum = number1 + number2;
      System.out.println("Addition program in java : " + sum);
   }
}

Output:

Please enter first number : 40
Please enter second number : 10
Addition of two numbers in java : 50


conclusion

So that’s about how to add two numbers in java. We also learnt addition of two numbers in java using method and addition program in java using classes and objects.

I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

You May Also Like