Java Program To Add Two Numbers

Sharing is healthy !!

Java program to add two numbers is one of the common java interview question.

In this program we will be using Scanner class in java.util package. Because we are asking user to input values to add two numbers.

Let’s see the example,

example: Java Program To Add Two Numbers

Also Read – Java Calculate Percentage

import java.util.Scanner;

public class Add_Two_Numbers {

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);
}
}

Output:

Java Program To Add Two Numbers


biginteger

Let’s see basic math function in java big integer. 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

conclusion

So that’s about java program to add two numbers. We also learnt biginteger and it’s basic math function in java.

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

Sharing is healthy !!

Leave a Reply

Your email address will not be published.