How to perform Arithmetic Operation using BigInteger in Java

Arithmetic Operation using BigInteger

In this tutorial you will learn about, How to perform Arithmetic Operation using BigInteger in Java. Typically the BigInteger is a class, Which is belongs to java.math package.

When you wants to deal with the numbers, Which has more digits (ie: more than 10 or 15). The traditional primitive datatypes such as int, long and etc.. will not be suitable because its restricted with certain range. So BigInteger would be the best solution for certain activity. In this Post, I have attached simple program about, Performing Arithmetic Operation using BigInteger in Java.

ArithmeticOperation.java

package com.dineshkrish.math;

import java.math.BigInteger;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ArithmeticOperation {

	public BigInteger add(String num1, String num2) {
		
		BigInteger a = null;
		BigInteger b = null;
		
		if((num1 != null && !num1.isEmpty()) && (num2 != null && !num2.isEmpty())) {
			
			a = new BigInteger(num1);
			b = new BigInteger(num2);
		} else {
			
			a = new BigInteger("0");
			b = new BigInteger("0");
		}
		
		return a.add(b); 
	}
	
	public BigInteger subtract(String num1, String num2) {
		
		BigInteger a = null;
		BigInteger b = null;
		
		if((num1 != null && !num1.isEmpty()) && (num2 != null && !num2.isEmpty())) {
			
			a = new BigInteger(num1);
			b = new BigInteger(num2);
		} else {
			
			a = new BigInteger("0");
			b = new BigInteger("0");
		}
		
		return a.subtract(b); 
	}
	
	public BigInteger multiply(String num1, String num2) {
		
		BigInteger a = null;
		BigInteger b = null;
		
		if((num1 != null && !num1.isEmpty()) && (num2 != null && !num2.isEmpty())) {
			
			a = new BigInteger(num1);
			b = new BigInteger(num2);
		} else {
			
			a = new BigInteger("0");
			b = new BigInteger("0");
		}
		
		return a.multiply(b); 
	}
	
	public BigInteger divide(String num1, String num2) {
		
		BigInteger a = null;
		BigInteger b = null;
		
		if((num1 != null && !num1.isEmpty()) && (num2 != null && !num2.isEmpty())) {
			
			a = new BigInteger(num1);
			b = new BigInteger(num2);
		} else {
			
			a = new BigInteger("0");
			b = new BigInteger("0");
		}
		
		return a.divide(b); 
	}
	
}

Application.java

package com.dineshkrish.math;

import java.util.Scanner;

public class Application {

	public static void main(String[] args) {
		
		ArithmeticOperation operation = new ArithmeticOperation();
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Enter the first number : ");
		String firstNumber = scanner.next();
		System.out.println("Enter the second number : ");
		String secondNumber = scanner.next();
		
		System.out.println("BigInteger Arithmetic Operation");
		System.out.println("-------------------------------");
		
		System.out.println("Addition : "+operation.add(firstNumber, secondNumber));
		System.out.println("Subtraction : "+operation.subtract(firstNumber, secondNumber));
		System.out.println("Multiplication : "+operation.multiply(firstNumber, secondNumber));
		System.out.println("Division : "+operation.divide(firstNumber, secondNumber));
		
	}
}

Output

Enter the first number :
222222222222222222222222

Enter the second number :
111111111111111111111111

BigInteger Arithmetic Operation
——————————-
Addition : 333333333333333333333333
Subtraction : 111111111111111111111111
Multiplication : 24691358024691358024691308641975308641975308642
Division : 2

References

1. Java BigInteger API Documentation