NJUPT-“C Language“ experiment report 4

I. Experimental purpose and experimental requirements

(1) Proficient in the definition of parameterless function, parameterized function, function with no return value and function with return value.

(2) Understand the execution process of function calls and the value passing mode between formal parameters and actual parameters.

(3) Master the methods of nested function calls and recursive calls.

(4) Understand the idea of modular programming, and flexibly use the idea of functional modular design to solve practical problems in software development.

2. Experimental content

1, write two functions double getPer(double r); And double getArea(double r); Respectively find the circumference and area of the circle. Enter the radius of the circle from the main function, call two custom functions respectively to find the corresponding circumference and area, and output the complete information. (Note: The function declaration is given and must use the given function type, function name, and parameters).

#include <stdio.h>
 
double getPer(double r);
double getArea(double r);
 
int main() 
{
    double r;
    double perimeter, area;
    
    printf("请输入圆的半径:");
    scanf("%lf", &r);
    
    perimeter = getPer(r);
    area = getArea(r);
    
    printf("圆的半径为:%.2lf\n", r);
    printf("圆的周长为:%.2lf\n", perimeter);
    printf("圆的面积为:%.2lf\n", area);
    
    return 0;
}
 
double getPer(double r) 
{
    double perimeter = 2 * 3.14 * r;
    return perimeter;
}
 
double getArea(double r)
 {
    double area = 3.14 * r * r;
    return area;
}

2, write the function fun, calculate s =1+1/2! + a third! +... +1/n! , where the value of n is determined by the user. Enter n in the main function to call the function.

#include <stdio.h>
double factorial(int n);
double calculateS(int n);
int main() {
    int n;
    double s;
    int i;
    printf("请输入一个正整数n:");
    scanf("%d", &n);
    s = calculateS(n);
 
    printf("s = %.5lf\n", s);
    return 0;
}
double factorial(int n)
 {
    double result = 1;
    int i;
for (i = 1; i <= n; i++)
 {
        result *= i;
    }
    return result;
}
 
double calculateS(int n) 
{
    double s = 0;
    int i;
for (i = 1; i <= n; i++)
 {
        double term = 1 / factorial(i);
        s += term;
    }
    return s;
}

3. Write void DrawTriangle(int n, char c); To achieve the function of printing an isosceles triangle composed of n lines by the character variable c. The main function calls the function to print an isosceles triangle composed of 5 lines of '*' and 10 lines of '#' respectively.

#include <stdio.h>
void DrawTriangle(int n, char c);
int main() 
{
    int n;
    // 打印5行由字符‘*’组成的等腰三角形
    printf("打印5行的等腰三角形:\n");
    DrawTriangle(5, '*'); 
    // 打印10行由字符‘#’组成的等腰三角形
    printf("\n打印10行的等腰三角形:\n");
    DrawTriangle(10, '#'); 
    return 0;
}
void DrawTriangle(int n, char c)
 {
    int i, j;
    // 外层循环控制行数
for (i = 1; i <= n; i++)
 {
        // 内层循环控制每行的字符个数
        for (j = 1; j <= i; j++)
 {
            printf("%c", c);
        }
        printf("\n"); // 换行
    }
}

4, write a function int digitSum(int n); Enters a non-negative integer and returns the sum of the numbers that make it up. For example, calling digitSum(1729) should return 1+7+2+9, whose sum is 19. (Note: can be modified from the textbook example 3.17 (circular implementation) or Example 4.10 (recursive implementation)).

#include <stdio.h>
int digitSum(int n);
int main()
 {
    int n;
    int sum;
    printf("请输入一个非负整数:");
    scanf("%d", &n); 
    sum = digitSum(n);  
    printf("数字之和为:%d\n", sum);
    return 0;
}
int digitSum(int n) 
{
    int currDigit; // 当前数字
    int remainingDigits; // 剩余数字
    if (n == 0)
 {
        return 0;
    } 
    currDigit = n % 10;
    remainingDigits = n / 10;
    return currDigit + digitSum(remainingDigits);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高教百科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值