C语言简单编程案例(一)

本文提供了多个C语言编程实例,包括输出Hello World、输入整数求和与最大值、解决银行存款利息问题、判断特定年份月份的天数以及根据三边长度确定三角形类型等。这些实例覆盖了基本的输入输出、条件判断和算术运算。

1.//hello,world

#include<stdio.h>
int main()
{
	printf("Hello World!");
	return 0;
}

2.//输入两个整数,求和

#include<stdio.h>
int main()
{
	int a, b;
	int sum;
	scanf("%d,%d", &a, &b);
	sum = a + b;
	printf("%d+%d=%d", a, b, sum);
	return 0;
}

3.//输入两整数,求最大值

#include<stdio.h>
int main()
{
	int a, b;
	scanf("%d,%d", &a, &b);
	printf("max:%d", a > b ? a : b);
	return 0;
}

4.//银行存款问题

#include<stdio.h>
int main()
{
	int years;
	int benjin;
	float nianxi;
	float totalMoney;
	scanf("%d,%d", &benjin, &years);
	if (years == 1)
		nianxi = 0.035;
	else if (years == 2)
		nianxi = 0.044;
	else if (years == 3)
		nianxi = 0.05;
	else if (years == 5)
		nianxi = 0.055;
	totalMoney = benjin * nianxi * years + benjin;
	printf("totalMoney:%.2f", totalMoney);
}

5.//“闰年虫”:输入 year 年份和月 month,求该月的天数

#include<stdio.h>
int main()
{
	int year, month, days;
	int run;
	printf("请输入要查询的年份和月数:");
	scanf("%d,%d", &year, &month);
	//if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //小括号有些不必要
		run = 1;//闰年
	else
		run = 0;
	if (month == 2 && run == 1)
		days = 29;
	else if (month == 2 && run == 0)
		days = 28;
	else
	{
		switch (month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:days = 31; break;
		default:days = 30;
		}
	}
	printf("%d年%d月有%d天。", year, month, days);
	return 0;
}

6.//输入三角型的三条边,判断三角形的形状。假设输入的三边边长均 > 0

#include<stdio.h>
int main()
{
	float a, b, c;
	printf("请输入三角形的三条边长:");
	scanf("%f,%f,%f", &a, &b, &c);
	if (a + b > c && a + c > b && b + c > a)
	{
		if (a == b == c)
			printf("equilateral triangle");
		else if (a == b || a == c || b == c)
			printf("isoceles triangle");
		else
			printf("triangle");
	}
	else
		printf("non-triangle");
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值