➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
In this post, we will write the C++ program to find the area of Circle Triangle and Rectangle. We will develop all these programs separately.
C++ Program to Find the Area of Circle
#include<iostream>
using namespace std;
int main()
{
float radius, area;
cout << "Enter radius of circle: ";
cin >> radius;
area = 3.14*radius*radius;
cout << "Area = " << area << endl;
return 0;
}
Output:-
Enter radius of circle: 9.5
Area = 283.385
The area of the circle is calculated by 3.14*r2, where r is the
Area of Circle Using Symbolic Constant
#include<iostream>
#define PI 3.14
using namespace std;
int main()
{
float radius, area;
cout << "Enter radius of circle: ";
cin >> radius;
area = PI*radius*radius;
cout << "Area = " << area << endl;
return 0;
}
Here we defined PI as a macro. In the program whenever PI comes, pre-processor replace it with 3.14
C++ Program to Calculate the Area of Triangle having Height and Base
#include<iostream>
using namespace std;
int main()
{
float base, height, area;
cout << "Enter length of base and height: ";
cin >> base >> height;
area = (base * height) / 2;
cout << "Area = " << area << endl;
return 0;
}
Output:-
Enter length of base and height: 22 5.9
Area = 64.9
Area of a triangle having base and height is given by, Area = 1/2 * (base * height)
Three variables base, height, and area of float data types are defined to store the values.
Area of a Triangle having Three Sides
Area of a triangle having three sides a, b and c is given by,
s = ( a + b + c )
area = ( s * (s-a) * (s-b) * (s-c) )1/2
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a, b, c, s, area;
cout << "Enter length of three sides of triangle: ";
cin >> a >> b >> c;
s = (a + b + c) / 2;
area= sqrt( s * (s-a) * (s-b) * (s-c) );
cout << "Area = " << area << endl;
return 0;
}
Output:-
Enter
Area = 16.6437
To, find area we need sqrt() function to calculate square root value. sqrt() function is defined under <math.h>.
C++ Program to Calculate the Area of Rectangle
The area of a rectangle is calculated by length * area.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float length, width, area;
cout << "Enter length and width of triangle: ";
cin >> length >> width ;
area= length* width;
cout << "Area = " << area << endl;
return 0;
}
Output:-
Enter length and width of triangle: 12.5 15.2
Area = 190
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Learn More C++ Programming Examples,
- Introduction to C++ Programming
- Data Types in C++ Programming
- Find Range of Data Types in C++
- Void main(), main() vs int main() in C++
- Hello World! Program in C++
- Addition of Two Numbers in C++
- Add Subtract Multiply Divide 2 Numbers
- Sum and Average of three numbers in C++
- C++ Program to Find Area of Circle Triangle Rectangle
- Calculate Simple Interest in C++
- C++ program to find ASCII value
- C++ program to swap two Number