cplusplus
cplusplus

 

 

Introduction

A right-angled triangle is a type of triangle where one of the angles is exactly 90 degrees. In this type of triangle, the side opposite to the right angle is the hypotenuse, and it can be calculated using the Pythagorean theorem.

The Pythagorean theorem states that the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides (often referred to as the legs). This relationship is expressed as:

        c² = a² + b²

Where c is the length of the hypotenuse, and a and b are the lengths of the other two sides.

The objective of this program is to calculate the length of the hypotenuse given the lengths of the two other sides of a right-angled triangle using this formula.

Code Implementation

The following C++ program calculates the length of the hypotenuse of a right-angled triangle:


#include
#include // For sqrt function

using namespace std;

int main() {
double side1, side2, hypotenuse;

// Input lengths of the two sides
cout << “Enter the length of the first side: “; cin >> side1;

cout << “Enter the length of the second side: “; cin >> side2;

// Calculate the hypotenuse using the Pythagorean theorem
hypotenuse = sqrt(pow(side1, 2) + pow(side2, 2));

// Output the result
cout << “The length of the hypotenuse is: ” << hypotenuse << endl;

return 0;
}

Program Explanation

The program is structured as follows:

  • Header Files:
    • #include <iostream> is included to allow input and output operations.
    • #include <cmath> is included for mathematical functions such as sqrt and pow to calculate the square root and power of numbers.
  • Main Function:
    • The program begins execution in the main function.
    • The program prompts the user to enter the lengths of the two sides of the triangle using cin.
    • The length of the hypotenuse is calculated using the Pythagorean theorem formula: c = sqrt(a² + b²), where a and b are the side lengths, and sqrt is used to calculate the square root.
    • The result is displayed using cout.

How to Run the Program

To run this C++ program, follow these steps:

  1. Save the code to a file with the .cpp extension, for example hypotenuse.cpp.
  2. Open a terminal or command prompt on your computer.
  3. Navigate to the directory where the file is saved.
  4. Compile the program using a C++ compiler. For example, if you are using the g++ compiler, you can run:
                    g++ hypotenuse.cpp -o hypotenuse
    
  5. Run the compiled program:
                    ./hypotenuse
    
  6. Enter the lengths of the two sides when prompted, and the program will display the hypotenuse.
© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)