letsdoitjava

Factorial

Jun 12th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. package factorial;
  2. import java.util.Scanner;
  3.  
  4. public class Factorial {
  5.  
  6.     public static void main(String[] args) {
  7.         // MP2 factorial a Java application that will use input numbers from user and
  8.         // yield calculated factorial answers
  9.  
  10.         // @ author Renee Waggoner
  11.  
  12.         Scanner keyboard = new Scanner(System.in);
  13.         int factorial = 1;
  14.         boolean repeat = true;
  15.         // loop will continue until user entered 'n' as option
  16.         while (repeat) {
  17.             factorial = 1;
  18.             // ask for user to enter a number
  19.             System.out.print("Enter N: ");
  20.             int number = Integer.parseInt(keyboard.nextLine());
  21.             // calculate factorial
  22.             while (number != 0) {
  23.                 factorial = factorial * number;
  24.                 number -= 1;
  25.             }
  26.             // print factorial value
  27.             System.out.print("Factorial = " + factorial);
  28.             // ask for yes or no option
  29.             System.out.print("\nDo you want to continue? (y/n): ");
  30.             // receive choice
  31.             char decision = keyboard.nextLine().charAt(0);
  32.            
  33.             switch (decision) {
  34.             // in this case, do this
  35.             case 'y':
  36.                 repeat = true;
  37.                 break;
  38.             // in this case do this
  39.             case 'n':
  40.                 System.out.println("Have a good day");
  41.                 repeat = false;
  42.                 break;
  43.             }
  44.         }
  45.         keyboard.close();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment