// Factorial_9_9.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-15 File created. // 2006-02-15 Last modification. // A solution to Exercise 9-9. // As this is a program that can receive parameters from // the command line, this should be executed in a command // prompt window where the command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. // This program will, though, ask for data from the user // if no command line parameters are given. import java.util.* ; class Factorial_9_9 { static void print_factorial( int given_integer ) { System.out.print( "\n The factorial of " + given_integer + " is " + given_integer + "! = " ) ; int calculated_factorial = 1 ; while ( given_integer > 1 ) { calculated_factorial = calculated_factorial * given_integer ; System.out.print( given_integer + "*" ) ; given_integer -- ; } System.out.print( "1 = " + calculated_factorial ) ; } public static void main( String[] command_line_parameters ) { Scanner keyboard = new Scanner( System.in ) ; int integer_from_keyboard ; if ( command_line_parameters.length == 1 ) { integer_from_keyboard = Integer.parseInt( command_line_parameters[ 0 ] ) ; } else { System.out.print( "\n Type in a positive integer: " ) ; integer_from_keyboard = keyboard.nextInt(); } print_factorial( integer_from_keyboard ) ; } }