现在的位置: 首页 > 例题 > 正文

循环语句:连续输出n个偶数

2011年03月06日 例题 ⁄ 共 103字 ⁄ 字号 暂无评论

for循环

Sample In

5

Sample Out

0 2 4 6 8

使用for循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
int main(int argc, char *argv[])
{
	int i, n;
 
	scanf("%d", &n);
 
	for(i=0; i<n; i++) {
		printf("%4d", 2*i);
	}
 
	printf("n");
	return 0;
}

使用while循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
 
int main(int argc, char *argv[])
{
	int i, n;
 
	scanf("%d", &n);
 
	i = 0;
	while (i<n) {
		printf("%4d", 2*i);
		i++;
	}
 
	printf("n");
	return 0;
}

使用do while循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
 
int main(int argc, char *argv[])
{
	int i, n;
 
	scanf("%d", &n);
 
	i = 0;
 
	do {
		printf("%4d", 2*i);
		i++;
	} while (i<n);
 
	printf("n");
	return 0;
}

抱歉!评论已关闭.