In this tutorial, we will learn about loops in Java and we will see the example with output.
Find Greater Numbers Using Loop in java
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 the while loop in java
In this program, we will learn to find the largest number using a while loop. In this, you have to understand the while loop and if statement then you can easily solve it.
Example-1
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 a 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:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
- Write a Java program that finds the factorial of a number
- Write a Java program that finds prime numbers between 1 to n
- Write a Java program that prints all real and imaginary solutions to the quadratic equation
- Odd and Even number in java | Algorithm
- Even number in java | algorithm with source code
- Java greater number using a loop
- Java Area of Rectangle
- What is a polygon? | Area of Triangle in java
- To calculate the Area of Circle in the java program
- Java Addition through user input
- Addition program in java with source code