InterviewSolution
| 1. |
Write a program by using Scanner class to accept a set of positive and negative numbers randomly.Print all the negative numbers before the positive numbers without changing the order of the numbers.Sample input:1,-3,6,-9,5,-6,-7,4Sample output:-9,-7,-6,-3,1,4,5,6 |
|
Answer» Here is your answer ______________________________________________________________________ import java.util.*; class selection_sort { public void main() { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in an array"); int n=sc.nextInt(); int a[]=new int[n]; System.out.println("Enter the array values randomly ! I will sort it ! I know magic"); for(int i=0;i { a[i]=sc.nextInt(); } int MIN=0; int temp=0; for(int i=0;i<(n-1);i++) { min=i; for(int j=i+1;j { if(a[j] { min=j; } } temp=a[i]; a[i]=a[min]; a[min]=temp; } System.out.println("Sorted array in ascending order gives all negative first then positive hehe"); for(int i=0;i { System.out.println(a[i]); } } } If you randomly enter any integers , the output will be: First negatives will be printed (if any ) then POSITIVES will be printed. Output example Input : a[0] = 1 a[1]=9 a[2]=-5 Output: -5 1 9 Hope it HELPS you _____________________________________________________________________ |
|