# Java Program To Add Two Numbers

Hey guys!! Welcome to flower brackets blog. Java program to add two numbers is one of the common 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 static void main(String[] args)
{
int a, b, c;
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:

#### 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;

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

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 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
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 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();
System.out.println("Adding numbers in java : " + a.sum);
sc.close();
}
}

{
int sum = 0;
{
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 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

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

{
public static void main(String[] args) throws IOException
{
System.out.println("Please enter first number : ");
System.out.println("Please enter second number : ");
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.