[ Write a program to input 10 number into an integer array and interchange the largest number with the smallest number within the array and print the modified array. Assume that there is only one largest & smallest number.
Program Code :
import java.util.*;
class Change_Max_with_Min
{
public static void main(String args[])
{
int i,max,min,maxp=0,minp=0,t;
int n[]=new int[10];
Scanner sc=new Scanner(System.in);
//Accepting the 10 Array Element
for (i=0;i<10;i++)
{
System.out.print("Enter the Array Element = ");
n[i]=sc.nextInt();
}
max=n[0];
min=n[0];
//Checking Largest & Smallest Number within Array Element
for (i=0;i<9;i++)
{
if(n[i]>max)
{
max=n[i];
maxp=i;
}
if(n[i]<min)
{
min=n[i];
minp=i;
}
}
//Interchanging the Smallest Array Element with Largest Array Element
t=n[maxp];
n[maxp]=n[minp];
n[minp]=t;
System.out.print("Modified Array Elements are:-");
for (i=0;i<9;i++)
{
System.out.print(n[i]+", ");
}
}
}
No comments