JAVA Program to find factorial of a number

JAVA Program to find factorial of a number


In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n:

{\displaystyle n!=n\cdot (n-1)\cdot (n-2)\cdot (n-3)\cdot \cdots \cdot 3\cdot 2\cdot 1\,.}
For example:
6! = 6*5*4*3*2*1 = 720


Join Us

For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..



Steps to be followed for finding the factorial of a number.

  1. Input the number.
  2. Initialise a variable fact=1;
  3. Create a loop for 1 to Number
  4. Multiply each value of loop to the fact.
  5. Print the value of fact

Sounds Good ? Let's get started

CODE:

/*
 * Author: TheSchoolProgrammer
 * www.theschoolprogrammer.com
 * Question:Write a program to accept a number and print its factorial.
 */
import java.util.*;
public class MyClass {

    public static void main(String args[]) {
      Scanner sc=new Scanner(System.in);
      
      System.out.println("Enter the number");
      int n=sc.nextInt();
      int i,fact=1;
      for(i=1;i<=n;i++)
      {
          fact=fact*i;
      }
      System.out.println("Factorial of "+n+" is: "+fact);
      
    }
}

Output:

Example 1
Input: Enter the number
       5
Output: Factorial of 5 is 120

Example 2
Input: Enter the number
      4
Output: Factorial of 4 is 24 

Also check:

Found this useful? Share with your friends. Share

Comments

  1. NJ speeds sports betting at William Hill's New Jersey
    The William Hill 의정부 출장마사지 Sportsbook 정읍 출장안마 app will bring live-stream 여수 출장샵 betting to nearly 원주 출장안마 every sportsbook 전주 출장안마 in New Jersey. “We have made it possible to use all of our

    ReplyDelete
You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send