- /*PROG 4.10 MAXIMUM OF N ELEMENTS */
- import java.io.*;
- import java.util.*;
- class JPS13
- {
- public static void main(String args[])
- {
- int n, max, t=1, m;
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter how many numbers");
- n = sc.nextInt();
- System.out.println("Enter the number");
- m = sc.nextInt();
- max = m;
- while (t<=n-1)
- {
- System.out.println("Enter the number");
- m = sc.nextInt();
- if(max<m) ;
- max=m;
- t++;
- }
- System.out.println("Maximum element is " + max);
- }
- }
Initially the number of elements taken is n. The first number is taken outside the loop and assumed to be maximum; this number is stored in max. The remaining numbers are taken inside the loop. On each iteration, the number is compared with the max. If the max is less than the number taken, the number will be the maximum one. This is checked through if statement. In the end when control comes out from while loop, max is displayed.


雷达卡
京公网安备 11010802022788号







