目录
求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:
求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:
第1节 永不停止的哭声
让计算机永不停止地在屏幕上打印wa:
#include <stdio.h>
#include <stdlib.h>
int main()
{
while (1 > 0)
{
printf("wa ");
}
system("pause");
return 0;
}
调试结果:
不停地在屏幕上打印0和1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("color 02");
while (1 > 0)
{
printf(" 0 1");
}
system("pause");
return 0;
}
调试结果:
让计算机“永无止境”地打印hello:
#include <stdio.h>
#include <stdlib.h>
int main()
{
while (1 > 0)
{
printf("hello ");
}
system("pause");
return 0;
}
调试结果:
让计算机“永不止境”地在屏幕上显示中文汉字“你好”:
#include <stdio.h>
#include <stdlib.h>
int main()
{
while (1 > 0)
{
printf("你好 ");
}
system("pause");
return 0;
}
调试结果:
第2节 我说几遍就几遍
使用while循环输出100个wa:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a <= 100)
{
printf("wa ");
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
打印1~100:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a <= 100)
{
printf("%d ", a);
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
让计算机先输出100再输出99、98、97、……、1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 100; //初始值从100开始
while (a >= 1) //请注意这里的循环条件变为 a >= 1
{
printf("%d ", a);
a = a - 1; //每循环一次将a的值递减1
}
printf("\n");
system("pause");
return 0;
}
调试结果:
打印1~100中的偶数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 2;
while (a <= 100)
{
printf("%d ", a);
a = a + 2;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
打印1~100中能被3整除的数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 3;
while (a <= 100)
{
printf("%d ", a);
a = a + 3;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
让计算机从100打印到200:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 100;
while (a <= 200)
{
printf("%d ", a);
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
让计算机从1打印到100再打印到1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a < 100)
{
printf("%d ", a);
a = a + 1;
}
while (a >= 1)
{
printf("%d ", a);
a = a - 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
第3节 if对while说:我对你很重要
让计算机输出1~100中所有不能被3整除的数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a <= 100)
{
if (a % 3 != 0)
printf("%d ", a);
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
输出1~100中能被3整除但不能被5整除的所有数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a <= 100)
{
if (a % 3 == 0 && a % 5 != 0)
printf("%d ", a);
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
1~100中所有7的倍数和末尾含7的数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1;
while (a <= 100)
{
if (a % 7 == 0 || a % 10 == 7)
printf("%d ", a);
a = a + 1;
}
printf("\n");
system("pause");
return 0;
}
调试结果:
第4节 求和!求和!!求和!!!
求1 + 2 + 3的值:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
a = 1 + 2 + 3;
printf("%d\n", a);
system("pause");
return 0;
}
调试结果:
求1~100中所有数的和:
int main()
{
int a, i;
a = 0;
i = 1;
while (i <= 100)
{
a = a + i;
i = i + 1;
}
printf("%d\n", a);
system("pause");
return 0;
}
调试结果: