Java Greater Number Using Loop

Java greater number using loop

Each time my output prints the last number given through the scanner as the largest number. The program needs to be modified in a way that it scans through the number I input and prints the largest number.

Find the largest number using while loop in java example -1

In this program, we will learn to find the largest number using while loop. In this you have to understand while loop and if statement then you can easily solve it.

import java.util.Scanner;

public class LargestNumber {

public static void main (String [] args) {

int counter = 1;
int number;
int largest = 0;

Scanner inputs = new Scanner (System.in);
System.out.println ("Enter a Number:");
number = inputs.nextInt();

while (counter < 10)
{

System.out.println ("Enter the Number:");
number = inputs.nextInt();

if (largest < number){
largest = number;
}

counter ++;
}
System.out.println ("largest number" + largest);
}
}

Output:

PS C:\Users\Pramod\Desktop\MPS>  c:; cd 'c:\Users\Pramod\Desktop\MPS'; & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Roaming\Code\User\workspaceStorage\68adb333e7c4b36a52ea53bc8c24e1ec\redhat.java\jdt_ws\MPS_776f50d0\bin' 'LargestNumber' 
Enter a Number:
23
Enter the Number:
4
Enter the Number:
34
Enter the Number:
67
Enter the Number:
34
Enter the Number:
87
Enter the Number:
97
Enter the Number:
34
Enter the Number:
34
23
largest number97

Example 2: Print greater number using Loop in Java

import java.io. *;
import java.util. *;
public


class LargestNumLoop {
public static void main(String[]args)
{
int small = Integer.MAX_VALUE;
int number;
int large = 0;
int secondlarge = 0;

Scanner inputs = new Scanner (System.in );
System.out.println ("Enter a Number:");
number = inputs.nextInt();

large = number;
small = number;
secondlarge = number;

for (int x = 9; x > 0; x--)
{

System.out.println ("Enter" +x+ " more Number:");
number = inputs.nextInt();

if (number >= large){
secondlarge = large;
large = number;
}
else if (number > secondlarge){
secondlarge = number;
}
if (secondlarge < small){
small = number;
}

}
System.out.println ("largest number is: " + large+ "and the smallest number is:"+small);
}
}

Output:

PS C:\Users\Pramod\Desktop\MPS>  c:; cd 'c:\Users\Pramod\Desktop\MPS'; & 'c:\Users\Pramod\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:56038' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Pramod\AppData\Roaming\Code\User\workspaceStorage\68adb333e7c4b36a52ea53bc8c24e1ec\redhat.java\jdt_ws\MPS_776f50d0\bin' 'LargestNumLoop' 
Enter a Number:
23
Enter9 more Number:
43
Enter8 more Number:
76
Enter7 more Number:
87
Enter6 more Number:
99
Enter5 more Number:
55
Enter4 more Number:
76
Enter3 more Number:
34
Enter2 more Number:
67
Enter1 more Number:
55
largest number is: 99and the smallest number is:23
PS C:\Users\Pramod\Desktop\MPS>

Recommended Post:

Find the solution to the salesforce Question.

Spread the love

Leave a Reply

%d bloggers like this: