Write a program to input 10 numbers 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 and smallest number.

 



import java.util.*;
class Sol9
{
 static void main()
 {
 Scanner sc=new Scanner(System.in);
 int a[]=new int[10],i,l=0,s=0;
 System.out.println(“Enter 10 numbers:”);
 for(i=0;i<10;i++)
 a[i]=sc.nextInt();
 l=s=a[0];
 for(i=1;i<10;i++)
 {
 if(a[i]>l)
 l=a[i];
 if(a[i]<s)
 s=a[i];
 }
 for(i=0;i<10;i++)
 {
 if(a[i]==l)
 a[i]=s;
 else if(a[i]==s)
 a[i]=l;
 }
        System.out.println(“Modified Array:”);
 for(i=0;i<10;i++) { 
 System.out.print(a[i]+“ ”);
 } 
 }
 }

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

It looks like you're using an ad blocker. We rely on ads to keep our site free, please whitelist us!

Click here to continue

Please whitelist our site to continue enjoying our content.

Whitelist | Close