How to calculate factorial of a given number - Java Language Basics

Java examples for Language Basics:int

Description

How to calculate factorial of a given number

Demo Code


public class Main {
 
        public static void main(String[] args) {
               //from   w  ww . java 2 s .  co  m
                int number = 5;
               
                int factorial = number;
               
                for(int i =(number - 1); i > 1; i--)
                {
                        factorial = factorial * i;
                }
       
                System.out.println("Factorial of a number is " + factorial);
        }
}

Result


Related Tutorials