用JS编写用自定义方法分别求出长方形的面积和体面积的程序
时间: 2024-06-03 20:08:54 浏览: 5
function calculateArea(length, width) {
return length * width;
}
function calculateVolume(length, width, height) {
return length * width * height;
}
let length = 10;
let width = 5;
let height = 2;
let area = calculateArea(length, width);
let volume = calculateVolume(length, width, height);
console.log(`The area of the rectangle is ${area}.`);
console.log(`The volume of the rectangular prism is ${volume}.`);
相关问题
Java main通过自定义方法分别实现求长方形的面积、长方体的体积
下面是Java代码实现:
```java
public class Main {
public static void main(String[] args) {
double length = 5;
double width = 3;
double height = 2;
double rectangleArea = getRectangleArea(length, width);
double cuboidVolume = getCuboidVolume(length, width, height);
System.out.println("长方形面积为:" + rectangleArea);
System.out.println("长方体体积为:" + cuboidVolume);
}
public static double getRectangleArea(double length, double width) {
return length * width;
}
public static double getCuboidVolume(double length, double width, double height) {
return length * width * height;
}
}
```
在这个例子中,我们定义了两个自定义方法 `getRectangleArea` 和 `getCuboidVolume`,用于分别计算长方形的面积和长方体的体积。
在 `main` 方法中,我们定义了长方形的长、宽和长方体的长、宽、高,并分别调用了 `getRectangleArea` 和 `getCuboidVolume` 方法来计算它们的面积和体积,并将结果打印到控制台上。
输出结果为:
```
长方形面积为:15.0
长方体体积为:30.0
```
用C语言编写程序,定义函数分别实现求圆面积和长方形面积,并在主函数中输出面积
以下是用C语言编写的程序,实现求圆面积和长方形面积的函数,并在主函数中输出面积:
```c
#include <stdio.h>
// 定义求圆面积的函数
double circle_area(double r) {
return 3.14159265358979323846 * r * r;
}
// 定义求长方形面积的函数
double rectangle_area(double l, double w) {
return l * w;
}
int main() {
double r = 5.0; // 圆的半径
double l = 3.0; // 长方形的长度
double w = 4.0; // 长方形的宽度
double circle_area_result = circle_area(r); // 求圆面积
double rectangle_area_result = rectangle_area(l, w); // 求长方形面积
printf("圆的面积为:%f\n", circle_area_result);
printf("长方形的面积为:%f\n", rectangle_area_result);
return 0;
}
```
输出结果为:
```
圆的面积为:78.539816
长方形的面积为:12.000000
```