factorial of a number

Factorial of a number is the product of the numbers from 1 up to the number.

Write a program to accept a number and print its factorial. For example the factorial of 4 is 24(4 X 3 X 2 X 1)

For factorial of a number recursive program click the given link.

https://java4school.com/factorial-of-a-number-recursive-program

import java.io.*; 
class factorial 
{ 
public static void main()throws IOException 
{ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Enter a number of your choice"); 
int n=Integer.parseInt(br.readLine()); 
int fact=1; 
for(int i=1;i<=n;i++) 
{ 
fact*=i; 
} 
System.out.println("The factorial of the number is"+ fact); 
} 
} 

Output: Enter a number of your choice
4
The factorial of the number is 24

To learn how to take input using BufferedReader watch the video

https://youtu.be/bTOMwnwpXqs

LEAVE A REPLY

Please enter your comment!
Please enter your name here