Dremendo Tag Line

Input 10 numbers and print the largest using for loop in Java

for Loop - Question 8

In this question, we will see how to input 10 numbers and print the largest number among them in Java programming using for loop. To know more about for loop click on the for loop lesson.

Q8) Write a program in Java to input 10 numbers and print the largest number among them using for loop.

Program

import java.util.Scanner;

public class Q8
{
    public static void main(String args[])
    {
        int i,n,lar=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 10 numbers");
        for(i=1; i<=10; i=i+1)
        {
            n=sc.nextInt();
            if(i==1)
            {
                lar=n;		// store the first number
            }
            else if(n>lar)		// compare the previous stored number with the latest entered number
            {
                lar=n;
            }
        }
        System.out.println("Largest Number = " + lar);
    }
}

Output

Enter 10 numbers
15
3
6
9
84
21
34
59
5
12
Largest Number = 84
video-poster