Showing posts with label While Looping Structure. Show all posts
Showing posts with label While Looping Structure. Show all posts

Analysis - Java Sample Program

// Analysis.java
// Analysis of examination results using nested control statements.
import java.util.Scanner; // class uses class Scanner

public class Analysis 
{
   public static void main( String[] args ) 
   {
      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      // initializing variables in declarations
      int passes = 0; // number of passes
      int failures = 0; // number of failures
      int studentCounter = 1; // student counter
      int result; // one exam result (obtains value from user)

      // process 10 students using counter-controlled loop
      while ( studentCounter <= 10 ) 
      {
         // prompt user for input and obtain value from user
         System.out.print( "Enter result (1 = pass, 2 = fail): " );
         result = input.nextInt();

         // if...else is nested in the while statement           
         if ( result == 1 )          // if result 1,
            passes = passes + 1;     // increment passes; 
         else                        // else result is not 1, so
            failures = failures + 1; // increment failures

         // increment studentCounter so loop eventually terminates
         studentCounter = studentCounter + 1;  
      } // end while

      // termination phase; prepare and display results
      System.out.printf( "Passed: %d\nFailed: %d\n", passes, failures );

      // determine whether more than 8 students passed
      if ( passes > 8 )
         System.out.println( "Bonus to instructor!" );
   } // end main
} // end class Analysis

Mystery 3 - Java Sample Program

//Mystery3.java
public class Mystery3 
{
   public static void main( String[] args )
   {
      int row = 10;
      int column;

      while ( row >= 1 ) 
      {
         column = 1;

         while ( column <= 10 ) 
         {
            System.out.print( row % 2 == 1 ? "<" : ">" );
            ++column;
         } // end while

         --row;
         System.out.println();
      } // end while
   } // end main
} // end class Mystery3

Mystery 2 - Java Sample Program

//Mystery2.java
public class Mystery2 
{
   public static void main( String[] args )
   {
      int count = 1;

      while ( count <= 10 ) 
      {
         System.out.println( count % 2 == 1 ? "****" : "++++++++" );
         ++count;
      } // end while
   } // end main
} // end class Mystery2

Mystery - Java Sample Program

//Mystery.java
public class Mystery 
{
   public static void main( String[] args )
   {
      int y;
      int x = 1;
      int total = 0;

      while ( x <= 10 ) 
      {
         y = x * x;
         System.out.println( y );
         total += y;
         ++x;
      } // end while

      System.out.printf( "Total is %d\n", total );
   } // end main
} // end class Mystery

Calculate - Java Sample Program

// Calculate.java
// Calculate the sum of the integers from 1 to 10 
public class Calculate 
{
   public static void main( String[] args )
   {
      int sum;
      int x;

      x = 1;   // initialize x to 1 for counting
      sum = 0; // initialize sum to 0 for totaling

      while ( x <= 10 ) // while x is less than or equal to 10      
      {
         sum += x; // add x to sum
         ++x; // increment x
      } // end while

      System.out.printf( "The sum is: %d\n", sum );
   } // end main
} // end class Calculate

Triangle of Stars - Void Method - Java Sample Program

// Program: Print a triangle of stars
// Given the number of lines, this program prints a triangle of stars.

import java.util.*;
public class TriangleOfStars
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int numberOfLines;
int numberOfBlanks;
int counter = 1;

System.out.print("Enter the number of star lines " + "(1 to 20) to be printed: ");

numberOfLines = console.nextInt();

System.out.println();

while (numberOfLines < 0 || numberOfLines > 20)
{
System.out.println("The number of star lines should " + "be between 1 and 20");
System.out.print("Enter the number of star lines " + "(1 to 20) to be printed: ");

numberOfLines = console.nextInt();

System.out.println ();
}

numberOfBlanks = 30;
for (counter = 1; counter <= numberOfLines; counter++)
{
printStars(numberOfBlanks, counter);
numberOfBlanks--;
}
} // end main

public static void printStars(int blanks, int starsInLine)
{
int count = 1;

for (count = 1; count <= blanks; count++)
System.out.print(" ");

for (count = 1; count <= starsInLine; count++)
System.out.print(" *");

System.out.println();
} //end printStars
}

Sample Output:

Enter the number of star lines (1 to 20) to be printed: 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

In the method main, the user is first asked to specify how many lines of stars to print. (In
this program, the user is restricted to 20 lines because a triangular grid of up to 20 lines fits
nicely on the screen.) Because the program is restricted to only 20 lines, the while loop
in the method main ensures that the program prints the triangular grid of stars only if the
number of lines is between 1 and 20.

Class Average - Java While Loop Sample Program

 Suppose we are given a file consisting of students’ names and their test scores, a number
between 0 and 100 (inclusive). Each line in the file consists of a student name followed by
the test score. We want a program that outputs each student’s name followed by the test
score and the grade. The program also needs to output the average test score for the class.
Consider the following program.

// This program reads data from a file consisting of students'
// names and their test scores. The program outputs each
// student's name followed by the test score and the grade. The
// program also outputs the average test score for all students.

import java.io.*; //Line 1
import java.util.*; //Line 2

public class ClassAverage //Line 3
{ //Line 4
public static void main(String[] args) throws FileNotFoundException //Line 5
{ //Line 6
String firstName; //Line 7
String lastName; //Line 8

double testScore; //Line 9
char grade = ' '; //Line 10
double classAverage; //Line 11
double sum = 0; //Line 12
int count = 0; //Line 13

Scanner inFile = new Scanner(new FileReader("stData.txt")); //Line 14
PrintWriter outFile = new PrintWriter("stData.out"); //Line 15

while (inFile.hasNext()) //Line 16
{ //Line 17
firstName = inFile.next();//read the first name Line 18
lastName = inFile.next(); //read the last name Line 19
testScore = inFile.nextDouble(); //read the test score Line 20
sum = sum + testScore; //update sum Line 21
count++; //increment count Line 22

//determine the grade
switch ((int) testScore / 10) //Line 23
{ //Line 24
case 0: //Line 25
case 1: //Line 26
case 2: //Line 27
case 3: //Line 28
case 4: //Line 29
case 5: //Line 30
grade = 'F'; //Line 31
break; //Line 32
case 6: //Line 33
grade = 'D'; //Line 34
break; //Line 35
case 7: //Line 36
grade = 'C'; //Line 37
break; //Line 38
case 8: //Line 39
grade = 'B'; //Line 40
break; //Line 41
case 9: //Line 42
case 10: //Line 43
grade = 'A'; //Line 44
break; //Line 45
default: //Line 46
System.out.println("Invalid score."); //Line 47
}//end switch //Line 48

outFile.printf("%-12s %-12s %4.2f %c %n",firstName, lastName,testScore, grade); //Line 49
}//end while //Line 50

outFile.println(); //Line 51

if (count != 0) //Line 52
outFile.printf("Class Average: %.2f %n",  sum / count); //Line 53
else //Line 54
outFile.println("No data."); //Line 55
outFile.close(); //Line 56
} //Line 57
} //Line 58

Sample Output:
****************************************
Input File:
Steve Gill 89
Rita Johnson 91.5
Randy Brown 85.5
Seema Arora 76.5
Samir Mann 73
Samantha McCoy 88.5
Output File:
Steve Gill 89.00 B
Rita Johnson 91.50 A
Randy Brown 85.50 B
Seema Arora 76.50 C
Samir Mann 73.00 C
Samantha McCoy 88.50 B
Class Average: 84.00
****************************************

 The preceding program works as follows. The statements in Lines 7 to 11 declare
variables required by the program. The statements in Lines 12 and 13 initialize the
variables sum and count. The statement in Line 14 declares inFile to be a reference
variable of type Scanner and associates it with the input file. The statement in Line 15
declares outFile to be a reference variable of type PrintWriter and associates it with
the output file.

 The while loop from Lines 16 to 50 reads each student’s first name, last name, and test
score, and outputs the name followed by the test score and grade. Specifically, the
statement in Line 18 reads the first name, the statement in Line 19 reads the last name,
and the statement in Line 20 reads the test score. The statement in Line 21 updates the
value of sum. (After reading all the data, the value of sum stores the sum of all the test
scores.) The statement in Line 22 updates the value of count. (The variable count stores
the number of students in the class.) The switch statement from Lines 23 to 48
determines the grade from testScore and stores it in the variable grade.

The statement in Line 49 outputs a student’s first name, last name, test score, and grade.
The if...else statement in Lines 52 to 55 outputs the class average, and the statement
in Line 56 closes the file associated with outFile, which is stData.out.

Flag Controlled Loop - Java While Loop Sample Program

//Flag-controlled while loop.
//Guessing the number game.
//Filename: FlagControlledLoop

import java.util.*;

public class FlagControlledLoop
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//declare the variables
int num; //variable to store the random number
int guess; //variable to store the number

//guessed by the user
boolean done; //boolean variable to control the loop

num = (int) (Math.random() * 100); //Line 1
done = false; //Line 2

while (!done) //Line 3
{ //Line 4
System.out.print ("Enter an integer greater" + " than or equal to 0 and " + "less than 100: "); //Line 5
guess = console.nextInt(); //Line 6
System.out.println(); //Line 7

if (guess == num) //Line 8
{ //Line 9
System.out.println("You guessed the " + "correct number."); //Line 10
done = true; //Line 11
} //Line 12
else if (guess < num) //Line 13
System.out.println("Your guess is " + "lower than " + "the number.\n" + "Guess again!"); //Line 14
else //Line 15
System.out.println("Your guess is " + "higher than " + "the number.\n" + "Guess again!"); //Line 16
} //end while //Line 17
} //Line 18
}


Sample Output:
******************************************************************
Enter an integer greater than or equal to 0 and less than 100: 25
Your guess is higher than the number.
Guess again!

Enter an integer greater than or equal to 0 and less than 100: 5
Your guess is lower than the number.
Guess again!

Enter an integer greater than or equal to 0 and less than 100: 10
Your guess is higher than the number.
Guess again!

Enter an integer greater than or equal to 0 and less than 100: 8
Your guess is higher than the number.
Guess again!

Enter an integer greater than or equal to 0 and less than 100: 6
Your guess is lower than the number.
Guess again!

Enter an integer greater than or equal to 0 and less than 100: 7
You guessed the correct number.

 The preceding program works as follows: The statement in Line 1 creates an integer
greater than or equal to 0 and less than 100 and stores this number in the variable num.

 The statement in Line 2 sets the boolean variable done to false. The while loop starts
at Line 3 and ends at Line 17. The expression in the while loop at Line 3 evaluates the
expression !done. If done is false, then !done is true and the body of the while loop
executes; if done is true, then !done is false, so the while loop terminates.

 The statement in Line 5 prompts the user to enter an integer greater than or equal to 0
and less than 100. The statement in Line 6 stores the number entered by the user in the
variable guess. The expression in the if statement in Line 8 determines whether
the value of guess is the same as num , that is, if the user guessed the number correctly.
If the value of guess is the same as num, then the statements in Lines 10 and 11 execute.

The statement in Line 10 outputs the message:
You guessed the correct number.

 The statement in Line 11 sets the variable done to true. The control then goes back to
Line 3. Because done is true, !done is false and the while loop terminates.
If the expression in Line 8 evaluates to false, then the else statement in Line 13
executes. The statement part of this else is an if. . .else statement, starting at Line 13
and ending at Line 16. The if statement in Line 13 determines whether the value of
guess is less than num. In this case, the statement in Line 14 outputs the message:

Your guess is lower than the number.
Guess again!

 If the expression in the if statement in Line 13 evaluates to false, then the statement in
Line 16 executes, which outputs the message:
Your guess is higher than the number.
Guess again!

The program then prompts the user to enter an integer greater than or equal to 0 and less
than 100.

Fibonacci - Java Sample Program

//*************************************************************
// Program: nth Fibonacci number
// Given the first two numbers of a Fibonacci sequence, this
// determines and outputs the desired number of the Fibonacci
// sequence.
// Filename: FibonacciNumber.java
//*************************************************************

import javax.swing.JOptionPane;
public class FibonacciNumber
{
public static void main (String[] args)
{
//Declare variables
String inputString;
String outputString;
int previous1;
int previous2;
int current = 0;
int counter;
int nthFibonacci;

inputString =JOptionPane.showInputDialog("Enter the first "
+ "Fibonacci number: "); //Step 1
previous1 = Integer.parseInt(inputString); //Step 2
inputString = JOptionPane.showInputDialog("Enter the second "
+ "Fibonacci number: "); //Step 3
previous2 = Integer.parseInt(inputString); //Step 4

outputString = "The first two numbers of the "
+ "Fibonacci sequence are: "
+ previous1 + " and " + previous2; //Step 5

inputString =JOptionPane.showInputDialog("Enter the position "
+ "of the desired number in "
+ "the Fibonacci sequence: "); //Step 6

nthFibonacci = Integer.parseInt(inputString); //Step 7

if (nthFibonacci == 1) //Step 8.a
current = previous1;
else if (nthFibonacci == 2) //Step 8.b
current = previous2;
else //Step 8.c
{
counter = 3; //Step 8.c.1
//Steps 8.c.2 - 8.c.5
while (counter <= nthFibonacci)
{
current = previous2 + previous1; //Step 8.c.2
previous1 = previous2; //Step 8.c.3
counter++; //Step 8.c.5
}
}

outputString ¼ outputString + "\nThe "
+ nthFibonacci
+ "th Fibonacci number of "
+ "the sequence is: "
+ current; //Step 9

JOptionPane.showMessageDialog(null, outputString,"Fibonacci Number",
JOptionPane.INFORMATION_MESSAGE); //Step 10
System.exit(0);
}
}

//Sample Output:

Class Average - Java Sample Program

 Suppose we are given a file consisting of students’ names and their test scores, a number
between 0 and 100 (inclusive). Each line in the file consists of a student name followed by
the test score. We want a program that outputs each student’s name followed by the test
score and the grade. The program also needs to output the average test score for the class.
Consider the following program.

// This program reads data from a file consisting of students'
// names and their test scores. The program outputs each
// student's name followed by the test score and the grade. The
// program also outputs the average test score for all students.
// Filename: ClassAverage.java

import java.io.*; //Line 1
import java.util.*; //Line 2
public class ClassAverage //Line 3
{ //Line 4
public static void main(String[] args)
{ //Line 6
String firstName; //Line 7
String lastName; //Line 8
double testScore; //Line 9
char grade = ' '; //Line 10
double classAverage; //Line 11
double sum = 0; //Line 12
int count = 0; //Line 13

Scanner inFile = new Scanner(new FileReader("stData.txt")); //Line 14
PrintWriter outFile = new PrintWriter("stData.out"); //Line 15

while (inFile.hasNext()) //Line 16
{ //Line 17
firstName = inFile.next();//read the first name Line 18
lastName = inFile.next(); //read the last name Line 19
testScore = inFile.nextDouble(); //read the test score Line 20
sum = sum + testScore; //update sum Line 21
count++; //increment count Line 22

//determine the grade
switch ((int) testScore / 10) //Line 23
{ //Line 24
case 0: //Line 25
case 1: //Line 26
case 2: //Line 27
case 3: //Line 28
case 4: //Line 29
case 5: //Line 30
grade = 'F'; //Line 31
break; //Line 32
case 6: //Line 33
grade = 'D'; //Line 34
break; //Line 35
case 7: //Line 36
grade = 'C'; //Line 37
break; //Line 38
case 8: //Line 39
grade = 'B'; //Line 40
break; //Line 41
case 9: //Line 42
case 10: //Line 43
grade = 'A'; //Line 44
break; //Line 45
default: //Line 46
System.out.println("Invalid score."); //Line 47
}//end switch //Line 48

outFile.printf("%-12s %-12s %4.2f %c %n",firstName, lastName,testScore, grade); //Line 49
}//end while //Line 50

outFile.println(); //Line 51

if (count != 0) //Line 52
outFile.printf("Class Average: %.2f %n",sum / count); //Line 53
else //Line 54
outFile.println("No data."); //Line 55
outFile.close(); //Line 56
} //Line 57
} //Line 58

/*
Sample Output
*****************************************
Input File:

Steve Gill 89
Rita Johnson 91.5
Randy Brown 85.5
Seema Arora 76.5
Samir Mann 73
Samantha McCoy 88.5

Output File:

Steve Gill 89.00 B
Rita Johnson 91.50 A
Randy Brown 85.50 B
Seema Arora 76.50 C
Samir Mann 73.00 C
Samantha McCoy 88.50 B

Class Average: 84.00
*****************************************
*/


// ******Explanation******
 The preceding program works as follows. The statements in Lines 7 to 11 declare
variables required by the program. The statements in Lines 12 and 13 initialize the
variables sum and count. The statement in Line 14 declares inFile to be a reference
variable of type Scanner and associates it with the input file. The statement in Line 15
declares outFile to be a reference variable of type PrintWriter and associates it with
the output file.

 The while loop from Lines 16 to 50 reads each student’s first name, last name, and test
score, and outputs the name followed by the test score and grade. Specifically, the
statement in Line 18 reads the first name, the statement in Line 19 reads the last name,
and the statement in Line 20 reads the test score. The statement in Line 21 updates the
value of sum. (After reading all the data, the value of sum stores the sum of all the test
scores.) The statement in Line 22 updates the value of count. (The variable count stores
the number of students in the class.) The switch statement from Lines 23 to 48
determines the grade from testScore and stores it in the variable grade. The statement
in Line 49 outputs a student’s first name, last name, test score, and grade.

 The if...else statement in Lines 52 to 55 outputs the class average, and the statement
in Line 56 closes the file associated with outFile, which is stData.out.

Counter-Controlled While Loop - Java Sample Program

//Counter-controlled while loop
//Filename: CounterControlledWhileLoop

import java.util.*;

public class CounterControlledWhileLoop
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int limit; //store the number of items in the list
int number; //variable to store the number
int sum; //variable to store the sum
int counter; //loop control variable

System.out.print("Line 1: Enter the number of " + "integers in the list: "); //Line 1
limit = console.nextInt(); //Line 2
System.out.println(); //Line 3
sum = 0; //Line 4
counter = 0; //Line 5
System.out.println("Line 6: Enter " + limit + " integers."); //Line 6

while (counter < limit) //Line 7
{
number = console.nextInt(); //Line 8
sum = sum + number; //Line 9
counter++; //Line 10
}

System.out.printf("Line 11: The sum of the %d " + "numbers = %d%n", limit, sum); //Line 11

if (counter != 0) //Line 12
System.out.printf("Line 13: The average = %d%n",(sum / counter)); //Line 13
else //Line 14
System.out.println("Line 15: No input."); //Line 15
}
}

/*
Sample Output: 
In this sample run, the user input is shaded.
*********************************************************
Line 1: Enter the number of integers in the list: 12
Line 6: Enter 12 integers.
8 9 2 3 90 38 56 8 23 89 7 2
Line 11: The sum of the 12 numbers = 335
Line 13: The average = 27
*********************************************************

Explanation:
 The preceding program works as follows: The statement in Line 1 prompts the user to
input the data for processing. The statement in Line 2 reads the next input and stores it in
the variable limit. The value of limit indicates the number of items to be read. The
statements in Lines 4 and 5 initialize the variables sum and counter to 0. The while
statement in Line 7 checks the value of counter to determine how many items have
been read. If counter is less than limit, the while loop proceeds for the next iteration.

 The statement in Line 8 stores the next number in the variable number. The statement in
Line 9 updates the value of sum by adding the value of number to the previous value. The
statement in Line 10 increments the value of counter by 1. The statement in Line 11
outputs the sum of the numbers. The statements in Lines 12 through 15 output either the
average or the text: Line 15: No input.

 Note that in this program, in Line 4, sum is initialized to 0. In Line 9, after storing the
next number in number in Line 8, the program adds the next number to the sum of all
the numbers scanned before the current number. The first number read is added to zero
(because sum is initialized to 0), giving the correct sum of the first number. To find the
average, divide sum by counter. If counter is 0, then dividing by 0 terminates the
program and you get an error message. Therefore, before dividing sum by counter, you
must check whether or not counter is 0.

 Notice that in this program, the statement in Line 5 initializes the LCV counter to 0.
The expression counter < limit in Line 7 evaluates whether counter is less than
limit. The statement in Line 8 updates the value of counter. Note that in this program,
the while loop can also be written without using the variable number as follows:
while (counter < limit)
{
sum = sum + console.nextInt();
counter++;
}
*/

Telephone Digits - Java Sample Program

/*
The following program reads the letter codes 'A' through 'Z' and prints the corresponding
telephone digit. This program uses a sentinel-controlled while loop. To stop
the program, the user is prompted for the sentinel, which is '#'. This is also an example
of a nested control structure, where if... else, switch, and the while loop are
nested.
*/

//********************************************************
// Program: Telephone Digits
// This is an example of a sentinel-controlled while loop.
// This program converts uppercase letters to their
// corresponding telephone digits.
// Filename: TelephoneDigitProgram.java
//********************************************************

import javax.swing.JOptionPane;
public class TelephoneDigitProgram
{
public static void main (String[] args)
{
char letter; //Line 1
String inputMessage; //Line 2
String inputString; //Line 3
String outputMessage; //Line 4

inputMessage = "Program to convert uppercase "
+ "letters to their corresponding "
+ "telephone digits.\n"
+ "To stop the program enter #.\n"
+ "Enter a letter:"; //Line 5

inputString = JOptionPane.showInputDialog(inputMessage); //Line 6
letter = inputString.charAt(0); //Line 7

while (letter != '#' ) //Line 8
{
outputMessage = "The letter you entered is: "
+ letter + "\n"
+ "The corresponding telephone "
+ "digit is: "; //Line 9

if (letter >= 'A' && letter <= 'Z') //Line 10
{
switch (letter) //Line 11
{
case 'A':
case 'B':
case 'C': outputMessage = outputMessage + "2"; //Line 12
break; //Line 13
case 'D':
case 'E':
case 'F': outputMessage = outputMessage + "3"; //Line 14
break; //Line 15
case 'G':
case 'H':
case 'I': outputMessage = outputMessage + "4"; //Line 16
break; //Line 17
case 'J':
case 'K':
case 'L': outputMessage = outputMessage + "5"; //Line 18
break; //Line 19
case 'M':
case 'N':
case 'O': outputMessage = outputMessage + "6"; //Line 20
break; //Line 21
case 'P':
case 'Q':
case 'R':
case 'S': outputMessage = outputMessage + "7"; //Line 22
break; //Line 23
case 'T':
case 'U':
case 'V': outputMessage = outputMessage + "8"; //Line 24
break; //Line 25
case 'W':
case 'X':
case 'Y':
case 'Z': outputMessage = outputMessage + "9"; //Line 26
}
}
else //Line 27
outputMessage = outputMessage + "Invalid input"; //Line 28

JOptionPane.showMessageDialog(null, outputMessage, "Telephone Digit",
JOptionPane.PLAIN_MESSAGE); //Line 29

inputMessage = "Enter another uppercase letter "
+ "to find its corresponding "
+ "telephone digit.\n"
+ "To stop the program enter #.\n"
+ "Enter a letter:"; //Line 30

inputString = JOptionPane.showInputDialog(inputMessage); //Line 31
letter = inputString.charAt(0); //Line 32
}//end while
System.exit(0); //Line 33
}
}

/*
Sample Output:



*/

Sentinel-Controlled While Loop - Java Sample Program

//Sentinel-controlled while loop
//Filename:SentinelControlledWhileLoop.java

import java.util.*;
public class SentinelControlledWhileLoop
{
static Scanner console = new Scanner(System.in);
static final int SENTINEL = -999;

public static void main(String[] args)
{
int number; //variable to store the number
int sum = 0; //variable to store the sum
int count = 0; //variable to store the total numbers read

System.out.println("Line 1: Enter positive integers "+ "ending with " + SENTINEL); //Line 1
number = console.nextInt(); //Line 2

while (number != SENTINEL) //Line 3
{
sum = sum + number; //Line 4
count++; //Line 5
number = console.nextInt(); //Line 6
}

System.out.printf("Line 7: The sum of %d " + "numbers = %d%n", count, sum); //Line 7

if (count != 0) //Line 8
System.out.printf("Line 9: The average = %d%n", (sum / count)); //Line 9
else //Line 10
System.out.println("Line 11: No input."); //Line 11
}
}

/*
Sample Output: (In this sample run, the user input is shaded.)
***************************************************
Line 1: Enter positive integers ending with -999
34 23 9 45 78 0 77 8 3 5 -999
Line 7: The sum of 10 numbers = 282
Line 9: The average = 28
***************************************************

 This program works as follows: The statement in Line 1 prompts the user to enter
numbers and to terminate by entering -999. The statement in Line 2 reads the first
number and stores it in the variable number. The while statement in Line 3 checks
whether number is not equal to SENTINEL. If number is not equal to SENTINEL, the
body of the while loop executes.

 The statement in Line 4 updates the value of sum by
adding number to it. The statement in Line 5 increments the value of count by 1. The
statement in Line 6 stores the next number in the variable number. The statements in
Lines 4 through 6 repeat until the program reads -999. The statement in Line 7 outputs
the sum of the numbers, and the statements in Lines 8 through 11 output the average of
the numbers.

 Notice that the statement in Line 2 initializes the LCV number. The expression number
!= SENTINEL in Line 3 checks whether the value of number is not equal to SENTINEL.
The statement in Line 6 updates the LCV number. Also, note that the program continues
to read data as long as the user has not entered -999.

*/