Java program to find the factorial of a given number
KW.java
importjava.util.Scanner;classKW{publicstaticvoidmain(Stringargs[]){System.out.println("———————————————————————————————————————————");System.out.println("Program to find the factorial of a given number");System.out.println("———————————————————————————————————————————");longi,fact=1,n;Scannersc=newScanner(System.in);System.out.print("Enter the number ");n=sc.nextInt();if(n>20||n<0){System.out.println("OOP's can't find "+n+"!");}else{for(i=1;i<=n;i++){fact=fact*i;}System.out.println(n+"! = "+fact);}System.out.println("———————————————————————————————————————————");}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to find the factorial of a given number
———————————————————————————————————————————
Enter the number 20
20! = 2432902008176640000
———————————————————————————————————————————
kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to find the factorial of a given number
———————————————————————————————————————————
Enter the number 50
OOP's can't find 50!
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.