C language experiment 1_input and output

C language experiment 1_input and output

Experiment 01 (01) The number of days entered contains several weeks and days

题目描述
从键盘输入一个天数,求这个天数包含了几周几天。
输入描述
一个整数:天数
输出描述
包含几周几天
输入样例
25
输出样例
包含 3 周 4 天
#include <stdio.h>
int main()
{
    
    
    int a, b, c;
    scanf("%d", &a);
    b = a / 7;
    c = a % 7;
    printf("包含%d 周%d 天\n", b, c);
    return 0;
}

Experiment 01 (02) The sum and average of three real numbers

题目描述
编程实现:从键盘输入三个实数(double 型),求这三个数的和及平均值。
输入描述
三个实数
输出描述
三个数的和及平均值
输入样例
34.5 78 43
输出样例
sum=155.500000,avg=51.833333
#include <stdio.h>
int main()
{
    
    
    double a, b, c, sum, avg;
    scanf("%lf%lf%lf", &a, &b, &c);
    sum = a + b + c;
    avg = 1.0 * (sum / 3);
    printf("sum=%lf,avg=%lf\n", sum, avg);
    return 0;
}

Experiment 01 (03) Product of two integers

题目描述
编写函数求两个整数的积,并编写主函数:从键盘输入两个整数,调用该函数求积,输出结
果。
输入描述
两个整数
输出描述
两个整数的积
输入样例
35 50
输出样例
1750
#include <stdio.h>
int main()
{
    
    
    int product();
    product();
    return 0;
}
int product()
{
    
    
    int a, b, c;
    scanf("%d%d", &a, &b);
    c = a * b;
    printf("%d\n", c);
}

Experiment 01 (04) Calculate the area and circumference of a circle

题目描述
从键盘输入一个圆的半径,计算圆的面积及周长。π值取 3.141593,数据采用 double 型处
理,结果保留 4 位小数。
输入描述
圆的半径
输出描述
圆的面积及周长
输入样例
2
输出样例
圆的面积为:12.5664,周长为:12.5664(中文冒号、逗号)
#include <stdio.h>
#define pi 3.141593;
int main()
{
    
    
    double r, s, c;
    scanf("%lf", &r);
    s = r * r * pi;
    c = 2 * r * pi;
    printf("圆的面积为:%.4lf,周长为:%.4lf\n", s, c);
    return 0;
}

Experiment 01 (05) Know the length of the three sides, calculate the area of ​​the triangle

已知三角形的三边长 a,b,c,计算并输出三角形的面积。三边长从键盘输入,采用 double 型
数据存储。
计算三角形面积公式(海伦公式)为:
area = sqrt(s(s-a)(s-b)(s-c))
其中 s = (a+b+c)/2
本题假定:输入的三边能构成一个三角形
输入描述
三边长
输出描述
三角形面积
输入样例
4.5 6 5
输出样例
三角形的面积为:11.009761 (中文冒号)
#include <stdio.h>
#include <math.h>
int main()
{
    
    
    double a, b, c, area, s;
    scanf("%lf%lf%lf", &a, &b, &c);
    s = 1.0 * (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));
    printf("三角形的面积为:%lf\n", area);
    return 0;
}

Experiment 01 (06) Calculate the sum of three digits

题目描述
计算并输出一个三位数(该数为整数)的个位、十位和百位数字之和,三位数由键盘输入。
输入描述
一个数据:三位数
输出描述
三位数字之和
输入样例
153
输出样例
三位数字之和:9
#include <stdio.h>
int main()
{
    
    
    int x, a, b, c, sum;
    scanf("%d", &x);
    a = x / 100;
    b = (x / 10) % 10;
    c = x % 10;
    sum = a + b + c;
    printf("三位数字之和:%d\n", sum);
    return 0;
}

Experiment 01 (07) Find the resistance value after paralleling

题目描述
有三个电阻 r1、r2、r3 并联,编程计算并输出并联后的电阻 r。已知电阻并联公式为:
1/r = 1/r1+1/r2+1/r3
r1、r2、r3 从键盘输入
输入描述
输入三个数据:r1、r2、r3 的值
输出描述
输出一个数据:并联后的电阻值 r
输入样例
12 5 8
输出样例
2.448980
#include <stdio.h>
int main()
{
    
    
    double x, r, r1, r2, r3;
    scanf("%lf%lf%lf", &r1, &r2, &r3);
    x = (1.0 / r1) + (1.0 / r2) + (1.0 / r3);
    r = 1.0 / x;
    printf("%lf\n", r);
    return 0;
}

Experiment 01 (08) calculate the distance between two points

题目描述
从键盘输入平面上已知两点的坐标 A(x1,y1)、B(x2,y2),计算两点之间的距离。结果保留 4 位
小数。
输入描述
两点的坐标 x1,y1,x2,y2
输出描述
两点之间的距离
输入样例
2 3
8 4
输出样例
两点间的距离为:6.0828(标点为中文冒号)
#include <stdio.h>
#include <math.h>
int main()
{
    
    
    double x1, x2, y1, y2, r, x;
    scanf("%lf%lf\n", &x1, &y1);
    scanf("%lf%lf", &x2, &y2);
    x = pow(x1 - x2, 2) + pow(y1 - y2, 2);
    r = sqrt(x);
    printf("两点间的距离为:%.4lf\n", r);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44179485/article/details/112676974