English

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. - Computer Applications

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?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 240]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 3 Arrays (Single Dimensional and Double Dimensional)
EXERCISES | Q VII. 3. | Page 240
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×