c语言代码练习

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值