In this post, we will go through Kotlin program to find factorial of given number using for loop, while loop.
We will also use BigInteger to find factorial of very large value.
Factorial of a given positive number is given by –
n! = 1 * 2 * 3 * 4 * ... * n Where, n is positive number of which you want to find factorial.Basically, we multiply all the numbers starting from 1 to given positive number.
In this post, we will find factorial of given number using
1. Find Factorial of Given Number (using for loop)
Sourcecode –
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter number:") val numb = read.nextInt() var factorial: Long = 1 for(curNum in 1..numb) { factorial *= curNum } println("Factorial of $numb is $factorial") }
When you run the program, output will be
Enter number: 10 Factorial of 10 is 3628800
Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.
read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable year
We are using for loop to find factorial of given number in kotlin.
In for loop, we are running loop from 1 to given number i.e. numb.
Let’s assume numb is 3. So,
For i = 1,
factorial *= curNum
=> factorial = factorial + curNum
=> factorial = 1 * 1
=> factorial = 1
For i = 2,
=> factorial = 1 * 2
=> factorial = 2
For i = 3,
=> factorial = 2 * 3
=> factorial = 6
So, Factorial of 3 is 6.
That’s what we will get by multiplying all the numbers from 1 to 3 ( i.e. 1 * 2 * 3)
Finally, we print result using println() method.
2. Find Factorial of Given Number using while loop
We have already seen factorial of given number using for loop.
Now, we will find factorial of given number using while loop in Kotlin.
import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter Number:") val numb = read.nextInt() var factorial: Long = 1 var i = 1 while (i <= numb) { factorial *= i i++ } println("Factorial of $numb is $factorial") }
When you run the program, output will be
Enter Number: 10 Factorial of 10 is 3628800
Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.
read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.
Input read by scanner is then stored in variable year
We are using while loop to find factorial of given number instead of for loop. It works similar as for loop.
Let’s assume maximum number is 3. So,
While i = 1, (i <= numb) means (1 < 3). It's true. So, block inside while loop will be executed.
factorial *= i
=> factorial = factorial * i
=> factorial = 1 * 1
=> factorial = 1
Also, i++ means i = i + 1. Value of i will be increased by 1. So, i = 2.
While i = 2, (2 <= 3). It's true. So, block inside while loop will be executed.
=> factorial = 1 * 2
=> factorial = 2
Also, value of i will be increased by 1. So, i = 3.
While i = 3, (3 <= 3). It's true. So, block inside while loop will be executed.
=> factorial = 2 * 3
=> factorial = 6
Also, value of i will be increased by 1. So, i = 4.
While i = 4, (4 <= 3). It's false. So, block inside while loop won't be executed. So, factorial of 3 is 6. Finally, we print result using println() method.
3. Find Factorial Using BigInteger
We have already seen how to find factorial using for loop and while loop.
Now, we will find factorial of given number using for loop with BigInteger.
Why are we using BigInteger to find Factorial ?
As you can see growth in value of factorial by just increasing number by 1, it increase rapidly.
Factorial of 20 is 2432902008176640000.
Now, try to find factorial of 25?
Surprised!
You got some weird -ve number.
What if you want to find factorial of 40?
You can not find it if you are storing the result in Long type variable.
For such purposes, you need a data type that is capable of handling very large values.
That’s why need BigInteger!
Sourcecode –
import java.math.BigInteger import java.util.* fun main() { val read = Scanner(System.`in`) println("Enter number:") val numb = read.nextInt() var factorial: BigInteger = BigInteger.ONE for(curNum in 1..numb) { factorial = factorial.multiply(BigInteger.valueOf(curNum.toLong())) } println("Factorial of $numb is $factorial") }
When you run the program, output will be
Enter number: 40 Factorial of 40 is 815915283247897734345611269596115894272000000000
Here, we used BigInteger to store factorial value. So, we declared factorial of BigInteger data type.
The, we initialized it with BigInteger.ONE.
* can not be applied with BigInteger. We need to use multiply function for this purpose.
Also, we need to convert curNum to BigInteger to make it compatible with factorial so that
multiplication operation can be applied.
We have to convert curNum to Long because .valueOf do not accepts just Int variable in it’s argument.
Thus, we went through Kotlin program to find factorial of given number.