
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Area of a square from diagonal length in C++
A diagonal of a polygon is the line joining two vertices that are not adjacent to each other.
The formula for calculating the area of a square with diagonal length is a2 = d2 / 2, where a and d are the side and diagonal length of the square, respectively. The visual representation of the derivation of the formula is given below:
Here are some example scenarios to calculate the area of a square from the diagonal length using the above formula:
Scenario 1
Input: d = 10 Output: 50 Explanation: Using the formula: area = d2 / 2 = 102 / 2 = 100 / 2 = 50 => area = 50
Scenario 2
Input: d = 15 Output: 112.5 Explanation: Using the formula: area = d2 / 2 = 152 / 2 = 225 / 2 = 112.5 => area = 112.5
Program to Find Area of Square From Diagonal
In this example, we have used the formula (a2 = d2/2) to find the area of the square if the diagonal is given:
#include <iostream> #include <math.h> using namespace std; int main() { double d = 10; double area = (d * d) / 2.0; cout << "Area of square of diagonal " << d << " is " << area; return 0; }
The output of the above code is as follows:
Area of square of diagonal 10 is 50
Advertisements