write a program to store 10 numbers (including positive and negative numbers) in a Single Dimensional Array . Display all the negative numbers followed by the positive numbers without changing the order of the numbers
Answers
Answered by
18
Answer:
Hello, my solution is written in java, hope it can helpful!
package newPack;
import java.util.*;
public class printNegativeFirst {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);//create Scanner object
int [] arr = new int[10];//create an int array can store 10 elements
for(int i = 0; i < 10; i++) {
arr[i] = input.nextInt();//read the input from the user
}
for(int j : arr)
if(j<0)
System.out.print(j+" ");/*loop through each element and check whether is negative or not
so we can print negative elements first*/
System.out.println();
for(int u : arr)
if(u>0)
System.out.print(u+" ");//loop through left elements
}
}
Explanation:
Similar questions