Section 04 C language program first experience

1. Classroom example

1. The first C program: output "Hello world!"

/*接触的第一个C程序*/
#include <stdio.h>              //头 文 件 
int main()                      //主 函 数
{
    
                                   //函数开始
	printf("Hello world!\n");   //输出语句
	return 0;                   //返回语句
}                               //函数结束
//注意事项:
//1:分号,双引号都不能使用中文;
//2:\n表示换行符
//3:main函数中的返回语句可省略;

2. The second C program: find the sum of a and b

#include <stdio.h>
int main()
{
    
    
	int a, b, sum;
	scanf_s("%d %d", &a, &b);
	sum = a + b;
	printf("%d\n", sum);
}
输出结果:
12 34
46

3. The third C program: feet to meters conversion

#include <stdio.h>
int main()
{
    
    
	float f, m;
	printf("请输入英尺值: ");
	scanf_s("%f", &f);
	m = f / 3.28;
	printf("转为米制单位: %f米\n",m);
}
输出结果:
请输入英尺值: 12
转为米制单位 : 3.658537

2. Practical projects

1. Calculate the perimeter and area of ​​the rectangle

#include <stdio.h>
int main() {
    
    
	int,, 周长, 面积;
	printf("长方形的长和宽: ");
	scanf_s("%d %d", &, &);
	周长 = (+) * 2; 面积 =*;
	printf("长方形的周长为: %d\n", 周长);
	printf("长方形的面积为: %d\n", 面积);
	return 0;
}
输出结果:
长方形的长和宽: 10 20
长方形的周长为 : 60
长方形的面积为 : 200

2. Convert Celsius to Fahrenheit

#include <stdio.h>
int main()
{
    
    
	float F, C;
	printf("请输入摄氏温度值: ");
	scanf_s("%f", &C);
	F = C * 9 / 5 + 32;
	printf("对应的华氏温度为: %.2f\n", F);
	return 0;
}
输出结果:
请输入摄氏温度值: 10
对应的华氏温度为 : 50.00

3. Calculate the resistance value after parallel connection

#include <stdio.h>
int main()
{
    
    
	float R1, R2, R;
	printf("并联的两个电阻值: ");
	scanf_s("%f %f", &R1, &R2);
	R = 1 / (1 / R1 + 1 / R2);
	printf("并联后的电阻值为: %.2f\n", R);
	return 0;
}
输出结果:
并联的两个电阻值: 10 20
并联后的电阻值为 : 6.67

4. Calculate the surface area of ​​the cylinder

#include <stdio.h>
int main()
{
    
    
	const float PI = 3.1415926f;
	float r, h, s;
	printf("请输入半径: ");
	scanf_s("%f", &r);
	printf("请输入高度: ");
	scanf_s("%f", &h);
	s = PI * r * r * 2 + 2 * PI * r * h;
	printf("圆柱表面积: %.2f\n", s);
	return 0;
}
输出结果:
请输入半径: 10
请输入高度 : 20
圆柱表面积 : 1884.96

Guess you like

Origin blog.csdn.net/m0_51439429/article/details/114275105