BigInteger in java

Factorial

If you are interested in coding or math,one famous question you often heard is “ find the factorial of n numbers ”.And you did this question many times but still if someone ask you to find the factorial of some big number like n=100 or n=1000. If you find the factorial of n=100 in the same way as earlier you have find , it may exceed the limit of integer or long.Can you guess how to find the factorial of such a large number like n=100.To handle that problem there is an in-built class of java -BigInteger class.

BigInteger class is used for mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types.For example factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. There is no theoretical limit on the upper bound of the range because memory is allocated dynamically but practically as memory is limited you can store a number which has Integer.

Java program to find large factorials using BigInteger
import java.math.BigInteger;
import java.util.Scanner;

public class Example
{

static BigInteger factorial(int N)
{
BigInteger f = new BigInteger(“1”);
for (int i = 2; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));

return f;
}
public static void main(String args[]) throws Exception
{
int N = 20;
System.out.println(factorial(N));
}
}

--

--

--

Learn Like Noob

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

… something on Tech Day

RDS PostgreSQL Logical Replication COPY from AWS RDS Snapshot

The New Features and Breaking Changes in TypeScript 4.4

Fireworks and TypeScript version 4.4

Introduction to Git and Github

[OC] sketch I made of Peko during zoom class a while ago

Incident Response 2.0 — The Zenduty Incident Command System(ICS)

How to capture slow query logs in wso2 stream processor-based products.

n8n, Extendable Workflow Automation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Raj

Abhishek Raj

Learn Like Noob

More from Medium

Java — A Quick Tour

Type Conversion in Java

Lets create a java sample project part 1

Sorter in Java