You will learn about C++ program to calculate the area of square using a function and without function.
For the better understanding of this program you have to know more about the following C++ programming topic(s) :
C++ function
For the better understanding of this program you have to know more about the following C++ programming topic(s) :
C++ function
The formula is to calculate the area of a square is:
Area = (side length) * (side length)
Example#1.0: C++ program to calculate the area and perimeter of square without function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
float l, a, p; | |
cout << "Enter the side lenght of square\n"; | |
cin >> l; | |
a = l * l; | |
p = 4.0 * l; | |
cout << "Area of Square is \t\t" << a << endl; | |
cout << "Perimeter of Square is \t" << p << endl; | |
return 0; | |
} |
Output
Enter the side lenght of square
25.5
Area of Square is 650.25
Perimeter of Square is 102
Example#1.1: C++ program to calculate the area and perimeter of square using function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
//function prototype | |
float squareArea(float l); | |
float squarePerimeter(float l); | |
//function main begins program execution | |
int main() | |
{ | |
float l; | |
cout << "Enter the side lenght of square\n"; | |
cin >> l; | |
cout << "Area of Square is \t\t" << squareArea(l) << endl; | |
cout << "Perimeter of Square is \t" << squarePerimeter(l) << endl; | |
//function call | |
return 0; | |
} | |
//implementation of squareArea function | |
float squareArea(float l) | |
{ | |
return (l * l); | |
} | |
//implementation of squarePerimeter function | |
float squarePerimeter(float l) | |
{ | |
return (4.0 * l); | |
} |
Output
Enter the side lenght of square
25.5
Area of Square is 650.25
Perimeter of Square is 102
Press Enter to return to Quincy...
إرسال تعليق
Thank you for vising