Menu Bar

Horizontal Scrollable Menu

ads

Search This Blog

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.

Example-1

Import java.util.Scanner;

Public class FindTheLargestNumber {

     public static void main (String [] args) {

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

         Scanner input = new Scanner (System.in);

         System.out.println ("Enter Number:");
         Number = input.nextInt ();

         While (counter <10)
         {
             System.out.println ("Enter Number:");
             Number = input.nextInt ();

             Counter ++;

         }


         If (number> 1)
             Largest = number;


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


Example 2:Print greater number using Loop in Java

import java.io.*;
import java.util.*;
class Loop 
{
public static void main(String []args)
{
for(int i=0;i<=10;i++)
System.out.println(i);
int j=11;
while(j!=20)
{
System.out.println(j);
j++;
}
int k=20;
do
{
System.out.println(k);
k++;
}
while(k!=60);
}
}

No comments:

ads3