Analysis of Algorithms (AOA)

Iterative/Simple Minimum and Maximum Number

/*
	Program to determine Maximum and Minimum Number using Iteration Method in Java
	Author: PracsPedia		www.pracspedia.com
*/
import java.io.*;
import java.util.*;
public class IterativeMinMax
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter no. of elements");
		int n=sc.nextInt();
		int a[]=new int[n];
		System.out.println("Enter elements");

		for(int i=0;i < n;i++)
			a[i]=sc.nextInt();
		
		int min, max, l;
		min = max = a[0];
		l = a.length;
		
		for(int i = 0; i < l; i++)
		{
			if(a[i] > max)
				max = a[i];
			if(a[i] < min)
				min = a[i];
		}
		System.out.println("Max:  " + max);
		System.out.println("Min:  " + min);
	}
}
Download Source Code Program List

Sample Output

iterative min max output