AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-basics: Sorting Numbers from smallest to largest

Program to take the input from the user and print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. Assume that at most 100 positive numbers will be entered.

programming x 150
basics x 169
Posted On : 2017-10-20 13:20:27.0
profile Anonymous User - anyforum.in Anonymous User
1300
up-rate
3
down-rate

Answers


import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class SortingNumber {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int inputNumber;
Set<Integer> numberSet=new TreeSet<Integer>();
while((inputNumber=input.nextInt())!=0){
numberSet.add(inputNumber);
}
System.out.println(numberSet);
}
}

Posted On : 2017-10-20 17:04:53
Satisfied : 0 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596128122204
Reply This Thread
up-rate
0
down-rate
Comments
Please give a solution without using Set
profile Anonymous User - anyforum.in Anonymous User
13  0  0
Posted On :2017-10-23 23:01:07.0
Leave a Comment
There can be lot of ways of this problem. Please elaborate more what exactly you are looking for. Also it´ll be better if you can give some input and output data. Please check below code.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class SortingNumber {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int inputNumber;
List<Integer> numbers=new ArrayList<Integer>();
while((inputNumber=input.nextInt())!=0){
numbers.add(inputNumber);
}
Collections.sort(numbers);
System.out.println(numbers);
}
}


Note:
-----------------
Use Leave a Comment or Reply a thread option to discuss about the posted answer. Don´t post your comments as answer.
profile Garima Gupta - anyforum.in Garima Gupta
596  1281  22204
Posted On :2017-10-23 23:06:45.0
Leave a Comment
this program also showing output like this:
[14, 24, 29, 34, 44, 48, 56, 65, 67, 89]
please provide a solution for a list without braces and comas
profile Anonymous User - anyforum.in Anonymous User
13  0  0
Posted On :2017-10-25 11:18:47.0
Leave a Comment
Write a program to take the input from the user and print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. Assume that at most 100 positive numbers will be entered.

Sample Input:

56

48

89

14

29

65

34

67

2

4

44

0

Sample output:

2

4

14

29

34

44

48

56

65

67

89

please give a solution for this..thank you sir
profile Anonymous User - anyforum.in Anonymous User
13  0  0
Posted On :2017-10-25 11:21:29.0
Leave a Comment
Hi,
If you want the output in above specified format just iterate the List or Set and print the elements one by one instead of printing complete list together. Please check below code:

for(int i=0;i<numbers.size();i++){
System.out.println(numbers.get(i));
}


If you don´t want to use Set or sort method of collection, Please check below link to sort the numbers explicitly:

Sorting Numbers using the given conditions -Click Here

profile Garima Gupta - anyforum.in Garima Gupta
596  1281  22204
Posted On :2017-10-25 23:14:27.0
Leave a Comment
it worked successfully...thank you very much sir
profile Anonymous User - anyforum.in Anonymous User
13  0  0
Posted On :2017-10-28 11:40:07.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in