In Java
ReWrite the for loop program code example 3, above as a while loop.
Code Example 3 – Try it
import java.util.Scanner;
public static void main(String args[])
{
int count = 0;
int maxNumber = 0;
int enteredNumber = -9999999;
System.out.println("This program determines which entered number is
the largest ");
System.out.println("Enter count of numbers to check: ");
Scanner scanIn = new Scanner(System.in);
double count = scanIn.nextDouble();
scanIn.close();
// Print out message – input number – compare to see if new number
is bigger.. Repeat
for ( int i = 0; i < count ; i++ ) {
System.out.println("Enter number " + i + ": ");
Scanner scanIn = new Scanner(System.in);
double enteredNumber = scanIn.nextDouble();
scanIn.close();
if ( enteredNumber > maxNumber )
{
maxNumber = enteredNumber;
}
}
System.out.println("Largest Number is " + maxNumber);
}
import java.util.Scanner;
public class MyClass{
public static void main(String args[])
{
int count = 0;
int maxNumber = 0;
int enteredNumber = -9999999;
System.out.println("This program determines which entered number is
the largest ");
System.out.println("Enter count of numbers to check: ");
Scanner scanIn = new Scanner(System.in);
count = scanIn.nextInt();
// Print out message – input number – compare to see if new number
is bigger.. Repeat
int i = 0; //Counter Variable
while(i<count) //While loop condition
{
System.out.println("Enter number " + (i+1) + ": ");
enteredNumber = scanIn.nextInt();
if ( enteredNumber > maxNumber )
{
maxNumber = enteredNumber;
}
i++; // Increment of counter variable
}
System.out.println("Largest Number is " + maxNumber);
scanIn.close();
}
}
Sample Runs:
:
Get Answers For Free
Most questions answered within 1 hours.