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;
}
相关推荐
春生野草15 小时前
个人笔记——C语言字符串、树
c语言·开发语言·笔记
ysu_031416 小时前
用Vibe-Coding从零实现俄罗斯方块
c语言·游戏程序
冻柠檬飞冰走茶20 小时前
PTA基础编程题目集 7-24 约分最简分式(C语言实现)
c语言·开发语言·数据结构·算法
weixin_5859511721 小时前
CH592F 2.4G 低功耗
c语言·开发语言
盒子里的加菲猫21 小时前
什么?不会C语言也可以搭建窗口级应用程序?(Electron)
c语言·开发语言·electron
caishenzhibiao1 天前
无极多空共振 同花顺期货通指标
java·c语言·c#
modelmd1 天前
C 语言基础数据类型详解:大小与内存存储
c语言
hjlgs1 天前
七. C语言形参-实参
c语言·开发语言
ziguo11221 天前
C/C++ 中的 struct:从 C 到 C++ 的进化
c语言·c++