for循环
1.打印倒三角
代码:
#include <stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n); //输入n的值
for (i = 0; i<n; i++) //共n行
{
for (j = 0; j<i; j++) //打印空格
printf(" ");
for (j = 0; j<2 * (n - i) - 1; j++) //打印“*”
printf("*");
printf("\n"); //一行结束,换行
}
return 0;
}
结果:
2.九九乘法表
代码:
#include<stdio.h>
int main()
{
int i, j, k;
for (i = 1; i<10; i++)
{
for (j = 1; j<=i; j++)
{
k = i*j;
printf("%dx%d=%d\t", j, i, k);
}
printf("\n");
}
}
结果:
3.鸡兔同笼
代码:
#include<stdio.h>
int main()
{
int chicken = 0;
int rabbit = 0;
for (chicken = 0; chicken <= 50; chicken++)
{
for (rabbit = 0; rabbit <= 25; rabbit++)
{
if (chicken * 2 + rabbit * 4 == 100)
{
printf("有%d只鸡%d只兔\t", chicken, rabbit);
}
}
}
return 0;
}
结果: