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;
}