Advertisements
Advertisements
Question
Write a program in Java to store 10 numbers (including positive and negative numbers) in a Single Dimensional Array (SDA). Display all the negative numbers followed by the positive numbers.
Sample Input:
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | n[6] | n[7] | n[8] | n[9] |
15 | 21 | −32 | −41 | 54 | 61 | 71 | −19 | −44 | 52 |
Sample Output: −32, −41, −19, −44, 15, 21, 54, 61, 71, 52
Answer in Brief
Solution
import java.util.Scanner;
public class AQ3{
public static void main(String h[])
{int n;
String pos = "", neg = "";
Scanner scn = new Scanner(System.in);
System.out.println("Enter positive or negative numbers....0 to quit");
while (true)
{n = scn.nextInt();
if (n > 0)
pos += n + ",";
else if (n < 0)
neg += n + ",";
else
break;
}
System.out.println(neg + "" + pos);
}}
shaalaa.com
Is there an error in this question or solution?