打印前10个奇数自然数的C语言程序

104 阅读1分钟

编写一个C程序,使用for循环打印前10个奇数自然数。

#include <stdio.h>

int main()
{
	printf("The First 10 Odd Natural Numbers are\n");

	for (int i = 1; i <= 10; i++)
	{
		printf("%d\n", 2 * i - 1);
	}
}

image.png

此 C 进程使用 while 循环显示前 10 个奇数自然数。

#include <stdio.h>

int main()
{
	int i = 1;

	printf("The First 10 Odd Natural Numbers are\n");

	while (i <= 10)
	{
		printf("%d\n", 2 * i - 1);
		i++;
	}
}
The First 10 Odd Natural Numbers are
1
3
5
7
9
11
13
15
17
19

此 C 示例使用 do while 循环打印前 10 个奇数自然数。

#include <stdio.h>

int main()
{
	int i = 1;

	printf("The First 10 Odd Natural Numbers are\n");

	do
	{
		printf("%d\n", 2 * i - 1);

	} while (++i <= 10);
}
The First 10 Odd Natural Numbers are
1
3
5
7
9
11
13
15
17
19