C程序 查找矩形面积和周长
在这里,我们将用C语言程序求出长方形的面积和周长,如下所示。
示例:
输入:
l = 10
b = 10
输出:
area = 100
perimeter = 40
解释一下。
长方形的面积公式为
Area = length*breadth => l*b
长方形的周长公式为
Perimeter = 2*(length + breadth) => 2*(l+b)
方法 1:
// C program to demonstrate the
// area and perimeter of rectangle
#include <stdio.h>
int main()
{
int l = 10, b = 10;
printf("Area of rectangle is : %d", l * b);
printf("\nPerimeter of rectangle is : %d", 2 * (l + b));
return 0;
}
输出
Area of rectangle is : 100
Perimeter of rectangle is : 40
方法 2:
// C program to demonstrate the
// area and perimeter of rectangle
#include <stdio.h>
int main()
{
int l = 10, b = 10;
int A, P;
A = l * b;
P = 2 * (l + b);
printf("Area of rectangle is : %d", A);
printf("\nPerimeter of rectangle is : %d", P);
return 0;
}
输出
Area of rectangle is : 100
Perimeter of rectangle is : 40
方法3:使用函数
// C program to demonstrate the
// area and perimeter of rectangle
// using function
#include <stdio.h>
int area(int a, int b)
{
int A;
A = a * b;
return A;
}
int perimeter(int a, int b)
{
int P;
P = 2 * (a + b);
return P;
}
int main()
{
int l = 10, b = 10;
printf("Area of rectangle is : %d", area(l, b));
printf("\nPerimeter of rectangle is : %d",
perimeter(l, b));
return 0;
}
输出
Area of rectangle is : 100
Perimeter of rectangle is : 40