Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculates and prints out the factorial.
- *
- * @author alu_pahrata
- * @version 8-27
- */
- import java.util.Scanner;
- public class Factorial
- {
- public static void main (String[] args){
- int number = 1, multby = 1, input, counter = 0;
- Scanner scan = new Scanner(System.in);
- System.out.println("Welcome to the Factorial Program!");
- System.out.print("Please enter an integer: ");
- input = scan.nextInt();
- System.out.println("Your number is " + input + "!");
- while (input < 0){
- System.out.println("You have entered in a negative number.");
- System.out.print("Please enter in a positive number: ");
- input = scan.nextInt();
- System.out.println();
- }
- /*
- * This checks to see if a user has entered in 0, if they have in fact entered in 0 then the
- * final calculation that gets printed at the end of the program,
- * which in this case is the variable "number", will be set to 1
- * causing the final output to be "0! = 1
- */
- if (counter == input){
- number = 1;
- }
- /*
- * This while loop checks to see if counter is not equal to input
- * if they are not equal then multby and number are multiplied,
- * after that multby and counter are both increased by one
- * This while loop will keep reapeating untill counter is equal to input
- * This is the factorial algorithm.
- */
- while (counter != input){
- number = multby * number;
- multby++;
- counter++;
- }
- System.out.println(input + "! = " + number);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement