Wednesday, 24 December 2014

Write a Java Program to Print the Maximum and Minimum Numbers in the array

Input: 

    3,5,2,8,1,10

Output:

     Min Number: 1
     Max Number: 10


-->We will give only single input of array of Numbers Separated by Coma.

Program:

import java.io.*;
import java.util.*;
import java.util.StringTokenizer;

public class arrtok
{
    public static void main(String args[])
    {
        String n,temp;
        int i=0,j=0,p=0,temp1;
        int a[]=new int[20];
        Scanner s=new Scanner(System.in);
        n=s.next();
        StringTokenizer st = new StringTokenizer(n,",");
        while(st.hasMoreTokens())
        {
            temp=st.nextToken();
            a[i]=Integer.parseInt(temp);
            i++;
            j++;
      }
      System.out.println("Elements in Array are");
      for(i=0;i<j;i++)
      {
      System.out.println(a[i]);
      }
      for(i=0;i<j;i++)
      {
      p=i;
      while(p>0&&(a[p]<a[p-1]))
      {
      temp1=a[p];
      a[p]=a[p-1];
      a[p-1]=temp1;
      p--;
      }
      }
      System.out.println("Min Number: "+a[0]);
      System.out.println("Min Number: "+a[j-1]);

    }
}

See more...

Java Program to find largest number in an array
Java Program to find Minimum and Maximum Numbers in an Array
Find Largest and Smallest Number in an array
Finding minimum and maximum values in an array
Java Minimum and Maximum values in Array
Java Program to Find the largest and smallest number in n numbers
Java Program to display Minimum and Maximum Numbers in an Array
Java Program to print Minimum and Maximum Numbers in an Array
How to find Minimum and Maximum Numbers in an Array

0 comments:

Post a Comment