Starting the Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes. It is nice to check these cases out in advance before you start writing the program.

Starting the Program

flowchart

Here is a start to the program.

import  java.util.Scanner;

// User enters integer N.  
// The program calculates N factorial.
//
class Factorial
{
  public static void main (String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    long N, fact = 1; 

    System.out.print( "Enter N: " );
    N = scan.nextLong();

    if (  )
    {
      while (   )    
      {
 
         ;

         ;
      }
      
      System.out.println( "factorial is " + fact );
    }
    
    else
    {
      System.out.println("N must be zero or greater");
    }
  }
}

Notice how the program matches the flowchart, especially how the while statement is nested in the true branch of the if statement. The indenting (and the braces {} show this structure).

QUESTION 10:

Fill in the four blanks to complete the program. Here are some phrases you might use:

  fact = fact*N 
  N > 1 
  N = N - 1  
  N >= 0