Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package factorial;
- import java.util.Scanner;
- public class Factorial {
- public static void main(String[] args) {
- // MP2 factorial a Java application that will use input numbers from user and
- // yield calculated factorial answers
- // @ author Renee Waggoner
- Scanner keyboard = new Scanner(System.in);
- int factorial = 1;
- boolean repeat = true;
- // loop will continue until user entered 'n' as option
- while (repeat) {
- factorial = 1;
- // ask for user to enter a number
- System.out.print("Enter N: ");
- int number = Integer.parseInt(keyboard.nextLine());
- // calculate factorial
- while (number != 0) {
- factorial = factorial * number;
- number -= 1;
- }
- // print factorial value
- System.out.print("Factorial = " + factorial);
- // ask for yes or no option
- System.out.print("\nDo you want to continue? (y/n): ");
- // receive choice
- char decision = keyboard.nextLine().charAt(0);
- switch (decision) {
- // in this case, do this
- case 'y':
- repeat = true;
- break;
- // in this case do this
- case 'n':
- System.out.println("Have a good day");
- repeat = false;
- break;
- }
- }
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment