java programming

The following script will compute factorials using java. Just copy and paste the below script into your preferred IDE (Integrated Development Environment) and run it.

 

/*
	Programmer:	Jarrod
	Date:		June 25, 2018
	Filename:	FactorialApp.java
	Purpose:	To calculate factorial for user input 1 through 9 (or 16
	for enhancement non-long... or 40 for enhancement with long).
*/

//packages to import
import java.util.Scanner;
import java.text.NumberFormat;

public class FactorialApp
{
	public static void main(String args[])
	{
	//welcome the user
	System.out.println("Welcome to the Factorial Calculator");
	System.out.println(); //print a blank line

		//construct Scanner object
		Scanner sc = new Scanner(System.in);

		//set up do-while, perform calculations, and display output
		String choice = "n";

		do
		{
		//get input from the user
		System.out.print("Enter an integer value greater than 0 and less than 10:   ");
		int n = sc.nextInt();
		long factorial = 1L;

			//use a for loop to calculate the future value
			for(int i = 1; i <= n; i++)
			{
				factorial = factorial * i;

			} //end for loop

		//construct formatting object and display results
		System.out.println("\nThe factorial of " + n + " is " + factorial);

			//see if the user wants to continue
			System.out.print("\nContinue? (y/n): ");
			choice = sc.next();
			System.out.println();

	} while(choice.equalsIgnoreCase("y"));//continue program based on input y or Y
     } //end main()
} //end class