Advertisement
Chain_L

Untitled

May 27th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*
  4. Factorial program
  5.  */
  6. public class main {
  7.     public static void testing(String[] args){
  8.         System.out.println("This program is using \"for\" loop.\n");
  9.         Scanner myObj = new Scanner(System.in);
  10.         System.out.println("Enter number from 0 to 20.\n");
  11.         int num = myObj.nextInt(), result=1;
  12.  
  13.         if(num<0 || num>20){
  14.             System.out.println("Wrong number!\nTry again!");
  15.             testing(null);
  16.         }
  17.         else {
  18.             for (int i = 1, j = num; j > 0; j = j - 1, i = i + 1) {
  19.                 System.out.println(j);
  20.                 System.out.println(i);
  21.                 result = result * i;
  22.             }
  23.         }
  24.         System.out.println("Factorial of: (" + num + "!) is equal to "+ result);
  25.     }
  26. }
  27.  
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement