C•exercises

catalogue

基础

🩷visual studio新建C语言文件

🩷如果显示无配置,就双击文件夹里的.sln文件,要先在visual studio里打开项目所在文件夹哦

题目1:计算并输出矩形的面积和周长

c 复制代码
#include <stdio.h>

int main() {
	float chang = 0;
	float kuan = 0;
	printf("please input the chang and kuan orderly(split by space):");
	scanf_s("%f %f", &chang, &kuan); //scanf_s中变量加&
	float mianji = chang * kuan;
	float zhouchang = 2.0 * (chang + kuan); 
	printf("mianji is %0.2f, zhouchang is %0.2f", mianji, zhouchang); //0.2f保留两位小数,printf中变量直接写名,不加&
	return 0;
}

题目2:数字位数的分离与求和

c 复制代码
#include <stdio.h>

int main() {
	int num,ge,shi,bai,total,mutiple;
	printf("please input a three-digit number:");
	scanf_s("%d", &num);
	if (num > 100 && num < 999) { //判断方法简单,同时满足用&&哦
		bai = num / 100;
		shi = (num / 10) % 10;
		ge = (num % 100) % 10;
		total = bai + shi + ge;
		mutiple = bai * shi * ge;
		printf("the sum of the digits is %d\n", total);
		printf("the product of the digits is %d\n", mutiple);
	}
	else {
		printf("this number is not a three-digit number!\n");
		return 0; //结束程序,避免错误计算
	}
	return 0;
}

题目3:单位转换-英寸到厘米

c 复制代码
#include <stdio.h>

int main() {
	float inches = 0;
	printf("请输入一个以英寸为单位的长度值:");
	scanf_s("%f", &inches);
	float centimeters = inches * 2.54;
	printf("%.2f英寸转化为厘米为%.2f", inches, centimeters);
	return 0;
}

题目4:整数溢出验证

c 复制代码
#include <stdio.h>
#include <limits.h>  // 包含 INT_MAX 的定义,注意别写错,要写C的,c++的库名是<climits>,会导致使用c++编译器

int main() {
	int a = 0;
	int b = 0;
	printf("请输入两个较大整数,用空格分隔:");
	scanf_s("%d %d", &a, &b); //记得加_s
	printf("INT_MAX = %d\n", INT_MAX);
	printf("a + b = %d\n", a + b);
	printf("a * b = %d\n", a * b);
	return 0;
}
相关推荐
水饺编程2 小时前
第2章:Linux 基础,切换到超级用户
linux·c语言·汇编
小莞尔11 小时前
【51单片机】【protues仿真】基于51单片机超声波测距仪系统
c语言·单片机·嵌入式硬件·物联网·51单片机
学逆向的11 小时前
C 宏函数
c语言·开发语言·网络安全
Code_Solitude11 小时前
C语言:指针题型总结
c语言·开发语言
wyhwust11 小时前
程序设计基础-No5课件知识点
c语言
FLJwu12 小时前
运动相机采购选型避坑实战|第二篇
c语言·开发语言·数码相机·运动相机·oem 量产
lhbweilai12 小时前
分析FreeRTOS内核对象API的源码实现(六)
c语言·stm32·单片机
Brilliantwxx14 小时前
【STM32】RTC 完结篇:日期操作、闹钟中断与附录(时间戳 / 闹钟 / NTP)
服务器·c语言·stm32·单片机·嵌入式硬件·实时音视频
学逆向的14 小时前
C 悬空指针
c语言·开发语言·网络安全
·present·1 天前
CSAPP(lecture 4_01)
c语言·c++