[백준 브론즈 Ⅴ] 2741번: N 찍기

DONI·2021년 8월 7일
0

Baekjoon Online Judge

목록 보기
21/31
post-thumbnail

문제

자연수 N이 주어졌을 때, 1부터 N까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.


입력

첫째 줄에 100,000보다 작거나 같은 자연수 N이 주어진다.


출력

첫째 줄부터 N번째 줄 까지 차례대로 출력한다.


예제 입력 1

5

예제 출력 1

1
2
3
4
5


소스코드

  • Java 첫 번째 방법 : for 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		for (int i = 0; i < n; i++)
			System.out.println(i + 1);
	}
}
  • Java 두 번째 방법 : while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();		
		int i = 0;
		while (i < n) {
			System.out.println(i + 1);
			i++;
		}
	}
}
  • Java 세 번째 방법 : do-while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();		
		int i = 0;
		do {
			System.out.println(i + 1);
			i++;
		} while (i < n);
	}
}

[바로가기] 2741번: N 찍기

profile
틀린 내용이 있다면 댓글 또는 이메일로 알려주세요 ❤ꔛ❜

0개의 댓글