Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.

Simple Java program to find Factorial of a number

January 17, 2018

The Java code below shows how to get the factorial of numbers. For instance 4! = 4 x 3 x 2 x 1 = 24.

Since Zero factorial is defined to be one, i.e. 0! = 1, the loop starts from 1.

The code below calculates the factorial of numbers from 1 to 10.


public class DisplayFactorial{
    public static void main(String[] args){
	int j = 1;

	System.out.println("0! = " + 1);

	for(int i=1; i<10; i++){
	    j = j *i;
	    System.out.println(i + "! = " + j);

	}

    }
}

 

When we run the program we will get output similar to the following:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

5 comments on “Simple Java program to find Factorial of a number

  1. I happen to be writing to make you understand what a useful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make the others without problems thoroughly grasp various very confusing topics. You undoubtedly surpassed visitors’ expected results. Thanks for supplying those warm and friendly, trustworthy, revealing not to mention unique tips on your topic to Tanya.

  2. I wanted to put you the tiny remark to be able to say thanks a lot over again on the precious strategies you have provided in this case. It was really strangely open-handed with you to make publicly precisely what many people would’ve marketed as an e book to generate some dough for themselves, most notably given that you could possibly have done it if you ever decided. These points as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth lots more pertaining to this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked at your blog post.

  3. I simply wished to thank you very much yet again. I do not know what I would have worked on without those tricks shown by you relating to this topic. This was the troublesome situation in my view, nevertheless encountering a new professional form you managed it forced me to jump for delight. I’m just happier for the guidance and even have high hopes you find out what a powerful job you’re doing instructing most people through your blog post. I am sure you’ve never encountered all of us.

Leave a Reply

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