In this example, you will learn about C++ program to calculate the area of square with and without using a function.
For the better understanding of the program, you should know about following C++ programming topic :
The formula to calculate the area of a square is:
Area = length * length
/* program to calculate the area of square */
#include <iostream>
using namespace std;
int main() //function main begins program execution
{
int square_area, square_side;
cout << "Enter the side of square:";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
return 0;
} // end main
Output
/* program to calculate the area of square using function */
#include <iostream>
using namespace std;
void area(); //function prototype
int main() //function main begins program execution
{
area(); //function call
return 0;
} // end main
void area() //called function
{
int square_area, square_side;
cout << "Enter the side of square:";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
} //end function area
Output