如何使用C语言计算矩形面积?

如题所述

你可以使用下面这个程序来实现对矩形面积的计算:

 code

#include <stdio.h>

int area(int width, int height) {

// 计算矩形面积并返回结果

return width * height;

}

int main() {

int width, height;

// 读入矩形的长和宽

printf("Please enter the width and height of the rectangle: ");

scanf("%d %d", &width, &height);

// 调用函数计算矩形面积

int result = area(width, height);

// 输出矩形面积

printf("The area of the rectangle is %d.\n", result);

return 0;

}

在这个程序中,我们定义了一个名为 area 的函数,用来计算矩形的面积。这个函数接收两个参数:矩形的长和宽。函数内部计算面积并返回结果。

在 main 函数中,我们读入矩形的长和宽,然后调用 area 函数计算矩形面积。最后,将矩形面积输出到屏幕上。

这个程序的输出示例如下:

code

Please enter the width and height of the rectangle: 5 7

The area of the rectangle is 35.

温馨提示:答案为网友推荐,仅供参考
相似回答