Skip to content

Commit ee58e68

Browse files
committed
홀짝 구분하기
1 parent 1bc9350 commit ee58e68

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Lv0/separateEvenOdd.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 홀짝 구분하기
2+
3+
## 📌 문제 설명
4+
5+
자연수 n이 입력으로 주어졌을 때 만약 n이 짝수이면 "n is even"을, 홀수이면 "n is odd"를 출력하는 코드를 작성해 보세요.
6+
7+
### 제한 조건
8+
9+
- 1 ≤ n ≤ 1,000
10+
11+
### 입출력 예
12+
13+
입력 #1
14+
15+
```text/plain
16+
100
17+
```
18+
19+
출력 #1
20+
21+
```text/plain
22+
100 is even
23+
```
24+
25+
입력 #2
26+
27+
```text/plain
28+
1
29+
```
30+
31+
출력 #2
32+
33+
```text/plain
34+
1 is odd
35+
```
36+
37+
38+
# 🧐 접근
39+
40+
삼항연산자를 이용해 출력하면 된다.
41+
42+
```java
43+
import java.util.Scanner;
44+
public class Solution {
45+
public static void main(String[] args) {
46+
Scanner sc = new Scanner(System.in);
47+
int n = sc.nextInt();
48+
System.out.print(String.format("%d is %s", n, (n % 2 == 0) ? "even" : "odd"));
49+
}
50+
}
51+
```
52+
53+
# 💡 풀이
54+
55+
`String.format()`을 사용해서 위 처럼 출력해도 되고 아래 방식으로 출력해도 무관하다.
56+
```java
57+
System.out.print(n + " is " + ((n % 2 == 0) ? "even" : "odd"));
58+
```
59+
60+
# 📘 그 외의 풀이
61+
62+
### ==================

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
- [56. 정수 부분](./Lv0/valueOfInt.md)
6262
- [57. 문자열 붙여서 출력하기](./Lv0/appendString.md)
6363
- [58. 문자열 곱하기](./Lv0/multipleOfString.md)
64+
- [59. 홀짝 구분하기](./Lv0/separateEvenOdd.md)
6465

6566
# 📘 Lv.1
6667

0 commit comments

Comments
 (0)