1.从大到小输出
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数字:<");
scanf("%d%d%d", &a, &b, &c);
if (a < b)
{
int tmp = a;
a = b;
b = tmp;
}
if (a < c)
{
int tmp = a;
a = c;
c = tmp;
}
if (b < c)
{
int tmp = b;
b = c;
c = tmp;
}
printf("%d %d %d\n", a, b, c);
return 0;
}
2.打印3的倍数的数
#include <stdio.h>
int main()
{
int a = 1;
while (a <= 100)
{
if (a % 3 == 0)
printf("%d ", a);
a++;
}
return 0;
}
3.求两个数的最大公约数(辗转相除法)
#include <stdio.h>
int main()
{
int m, n, r = 0;
scanf("%d%d", &m, &n);
while (m % n)
{
r = m % n;
m = n;
n = r;
}
printf("%d\n", n);
return 0;
}
4.打印1000到2000之间的闰年
法1
#include <stdio.h>
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++)
{
if (year % 4 == 0 && year % 100 != 0)
{
printf("%d ", year);
count++;
}
else if (year % 400 == 0)
{
printf("%d ", year);
count++;
}
}
printf("\ncount=%d\n ", count);
return 0;
}
法2
int main()
{
int year = 0;
int count = 0;
for (year = 1000; year <= 2000; year++)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
printf("%d ", year);
count++;
}
}
printf("\ncount=%d\n ", count);
return 0;
}
5.打印100到200之间的素数(试除法)还有很多种方法,并不局限
法1
#include <stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
}
if (j == i)
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d\n", count);
return 0;
}
法2
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j < sqrt(i); j++)
{
if (i % j == 0)
{
break;
}
}
if (j > sqrt(i))
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d\n", count);
return 0;
}
法3
for(i = 101; i <= 200; i += 2)
6.数九的个数(1-100)
int main()
{
int i = 0;
int count = 0;
for (i = 2; i <= 100; i++)
{
if (i % 10 == 9)
{
count++;
}
if (i >= 90 && i < 100)
{
count++;
}
}
printf("count=%d\n ", count);
return 0;
}
7.计算1/1-1/2+1/3-1/4.....+1/99-1/100,打印出结果
int main()
{
int i = 0;
double sum = 0.0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
sum += flag * (1.0 / i);
flag = -flag;
}
printf("sum=%lf\n", sum);
return 0;
}
8.求10个数字中的最大值
int main()
{
int arr[] = { 1,2,3,4,6,7,5,9,0,18 };
int max = arr[0];
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
printf("max=%d\n", max);
return 0;
}
9.在屏幕上输出乘法口诀表
int main()
{
int i = 0;
int j = 0;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-2d ", i, j, i * j);
}
printf("\n");
}
return 0;
}