본문 바로가기

공부/알고리즘

알고리즘 for문 (코드업)

반응형
	public static void main(String[] args) {
		
		for(int i=1; i<=100; i++) {
			System.out.print(i+" ");
		}
	}
	
}

1부터 100까지 출력

 

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		for(int i=1; i<=n; i++) {
			System.out.print(i+" ");
		}
	}
	
}

1부터 n까지 출력

 

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();

		if (a < b) {
			while (a <= b) {
				System.out.print(a + " ");
				a++; 
			}
		} else if (b < a) {
			while (b <= a) {
				System.out.print(b + " ");
				b++;
			}
		} else {
			System.out.print(a);
		}
	}

}

a랑 b 입력받아서 출력 (a랑 b 중 누가 더 큰지는 모름)

a~b 까지 또는 b~a까지 출력

반응형