for 문
내부의 조건에 부합하면 계속해서 특정한 구문을 실행합니다
반복문을 탈출하고자 하는 위치에 break 구문을 삽입합니다
for(초기화; 조건; 반복 끝 명령어){
}
반복문을 활용하여 1부터 100까지의 정수를 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
|
#include <stdio.h>
int main(void) {
for (int i = 0; i <= 100; i++) {
printf("%d\n" , i);
}
system("pause");
return 0;
}
|
cs |
반복문을 활용해 1부터 N까지의 합 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int n, sum = 0;
printf("n을 입력하세요");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum = sum + i;
}
printf("%d\n", sum);
system("pause");
return 0;
}
|
cs |
while 문
while문을 활용해 특정문자를 입력받아 N번 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int n;
char a;
scanf("%d %c", &n, &a);
while (n--) {
printf("%c ", a);
}
system("pause");
return 0;
}
|
cs |
특정 숫자의 구구단 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int i = 1;
while (i <= 9) {
printf("%d * %d = %d\n", n, i, n * i);
i++;
}
system("pause");
return 0;
}
|
cs |
while 문을 활용해 구구단 전체를 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
int main(void) {
int i = 1;
while (i <= 9) {
int j = 1;
while (j <= 9) {
printf("%d * %d =%d\n", i, j, i * j);
j++;
}
printf("\n");
i++;
}
system("pause");
return 0;
}
|
cs |
for 문을 활용해 구구단 전체를 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 9; i++) {
for (int j = 1;j <= 9; j++) {
printf("%d * %d = %d\n", i, j, i * j);
}
printf("\n");
i++;
}
system("pause");
return 0;
}
|
cs |