Views: 178
Following program shows you how to print factorial of given number.
In this program we get input from user and prints factorial of that number using for loop
import java.util.Scanner;
public class BasicMath18 {
public static void main(String[] args) {
System.out.println("Please enter a number:");
Scanner in = new Scanner(System.in);
int input = in.nextInt();
int result = 1;
for (int i = 1; i <= input; i++) {
result = result * i;
}
System.out.println(input + " factorial is: " + result);
}
}
Output:
Please enter a number:
5
5 factorial is: 120
On By
Navya
Top Tutorials
492.5K
32K
21.8K
18.7K
18.7K
6.7K
6.7K
4.9K
3.3K
3.2K
Top Questions
18.7K
14.5K
8.3K
6.6K
6.2K
5.7K
4.8K
4.8K
4.3K
3.9K
Top Articles
1.9K
1.6K
947
Top Blogs
796
779
55
Top News
754
686
670
398
385
385