Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- Factorial program
- */
- public class main {
- public static void testing(String[] args){
- System.out.println("This program is using \"for\" loop.\n");
- Scanner myObj = new Scanner(System.in);
- System.out.println("Enter number from 0 to 20.\n");
- int num = myObj.nextInt(), result=1;
- if(num<0 || num>20){
- System.out.println("Wrong number!\nTry again!");
- testing(null);
- }
- else {
- for (int i = 1, j = num; j > 0; j = j - 1, i = i + 1) {
- System.out.println(j);
- System.out.println(i);
- result = result * i;
- }
- }
- System.out.println("Factorial of: (" + num + "!) is equal to "+ result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement