For Loop Example

 

Welcome Everyone

Smallest to Largest Sort Program



import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		int times;
        
		ArrayList<Integer> numberList = new ArrayList<>();
		
		System.out.print("How many numbers will you enter: ");
		times = scan.nextInt();
			
		for(int i = 0; times > i ; i++) {
			System.out.print("Enter number: ");
			int temp = scan.nextInt();
			numberList.add(temp);
		}
		
		for(int i = 0; times > i ; i++) {
			
			for(int j = i+1; times>j;j++) {
				
				if(numberList.get(i)  > numberList.get(j)) {
					int temp = numberList.get(i);
					numberList.set(i, numberList.get(j));
					numberList.set(j, temp);
				}
				
			}
			
		}
		
		for(int number : numberList) {
			System.out.println(number);
		}
		
	}
	
}



Good Bye Everyone

Image Resource: https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSp0pmKOQD1sdNlGCYRLWzIv5mwf9xBNpbqaw&usqp=CAU

0 Comments