Write a Java Program to Find Factorial of a Number

Java Program to Find Factorial of a Number

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Find Factorial of a Number
Java Program to Find Factorial of a Number

Write a Java Program to Find Factorial of a Number

PROGRAM: Java Program to Find Factorial of a Number

/* Java Program to Find Factorial of a Number*/

1: Find Factorial of a number using for loop

public class Factorial {

public static void main(String[] args) {

int num = 10;
long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf(“Factorial of %d = %d”, num, factorial);
}
}

2: Find Factorial of a number using BigInteger

import java.math.BigInteger;

public class Factorial {

public static void main(String[] args) {

int num = 30;
BigInteger factorial = BigInteger.ONE;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial = factorial.multiply(BigInteger.valueOf(i));
}
System.out.printf(“Factorial of %d = %d”, num, factorial);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *