C++程式:求正方形和矩形的周長
本題給定正方形的邊長 (A) 和矩形的長和寬 (L 和 B)。我們的任務是編寫一個 C++ 程式來計算正方形和矩形的周長。
問題描述
要計算正方形的周長,我們需要正方形的邊長 (a)。我們將使用正方形周長公式:**4a**。
要計算矩形的周長,我們需要矩形的長 (L) 和寬 (B)。我們將使用矩形周長公式:**2(L+B)**。
正方形的周長
正方形是一個四邊形,四個邊相等,四個角都是90度。

正方形周長公式 = (4 * a)
讓我們來看一個例子來理解這個問題:
示例 1
輸入 − 5
輸出 − 20
示例 2
輸入 − 12
輸出 − 48
程式演示了我們的解決方案:
#include <iostream>
using namespace std;
int calcCircumference(int a){
int perimeter = (4 * a);
return perimeter;
}
int main() {
int a = 6;
cout<<"The Perimeter / Circumference of Square is
"<<calcCircumference(a);
}輸出
The Perimeter / Circumference of Square is 24
矩形的周長
矩形是一個四邊形,對邊相等,四個角都是90度。

矩形周長公式 = **2 * (l + b)**
讓我們來看一個例子來理解這個問題:
示例 1
輸入 − l = 7; b = 3
輸出 − 20
示例 2
輸入 − l = 13; b = 6
輸出 − 38
程式演示了我們的解決方案:
#include <iostream>
using namespace std;
int calcCircumference(int l, int b){
int perimeter = (2 * (l + b)); w =
return perimeter;
}
int main() {
int l = 8, b = 5;
cout<<"The Perimeter / Circumference of Rectangle is
"<<calcCircumference(l, b);
}輸出
The Perimeter / Circumference of Rectangle is 26
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP