C Program for Pythagorean Theorem

C Program for Pythagorean Theorem

C Program for Pythagorean Theorem

Introduction

The Pythagorean theorem is one of the most fundamental principles in mathematics, especially in geometry. It relates the lengths of the sides of a right-angled triangle and is widely used in various fields such as engineering, physics, and computer science.

According to this theorem, in a right-angled triangle, the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. This theorem provides a mathematical way to calculate the missing side of a triangle when two sides are known.


Formula

The mathematical representation of the Pythagorean theorem is as follows:

  • c² = a² + b², where:
    • c is the length of the hypotenuse.
    • a and b are the lengths of the other two sides of the triangle.

This formula can be rearranged to calculate any side of the triangle:

  • a = √(c² - b²)
  • b = √(c² - a²)

Code

Below is a C program to calculate the hypotenuse of a right-angled triangle or one of the other sides if the necessary inputs are provided:

#include <stdio.h>
#include <math.h>

int main() {
    int choice;
    float a, b, c;

    printf("Pythagorean Theorem Calculator\n");
    printf("1. Find the Hypotenuse\n");
    printf("2. Find a Side\n");
    printf("Enter your choice (1 or 2): ");
    scanf("%d", &choice);

    if (choice == 1) {
        printf("Enter the lengths of the two sides (a and b): ");
        scanf("%f %f", &a, &b);
        c = sqrt(a * a + b * b);
        printf("The length of the hypotenuse is: %.2f\n", c);
    } else if (choice == 2) {
        printf("Enter the length of the hypotenuse (c): ");
        scanf("%f", &c);
        printf("Enter the length of one side (a or b): ");
        scanf("%f", &a);
        b = sqrt(c * c - a * a);
        printf("The length of the other side is: %.2f\n", b);
    } else {
        printf("Invalid choice. Please restart the program.\n");
    }

    return 0;
}
    

Output

When you execute the above program, it will prompt you to choose between calculating the hypotenuse or one of the other sides. Here are sample outputs based on user inputs:

Example 1:
Pythagorean Theorem Calculator
1. Find the Hypotenuse
2. Find a Side
Enter your choice (1 or 2): 1
Enter the lengths of the two sides (a and b): 3 4
The length of the hypotenuse is: 5.00

Example 2:
Pythagorean Theorem Calculator
1. Find the Hypotenuse
2. Find a Side
Enter your choice (1 or 2): 2
Enter the length of the hypotenuse (c): 5
Enter the length of one side (a or b): 3
The length of the other side is: 4.00
    

Explanation

This program works in two modes, depending on the user's choice:

  • Finding the Hypotenuse: The program prompts the user to enter the lengths of the two shorter sides a and b. Using the formula c = √(a² + b²), it calculates the hypotenuse and displays the result.
  • Finding a Side: The program prompts the user to enter the length of the hypotenuse c and one other side a. It calculates the remaining side using the formula b = √(c² - a²) and displays the result.

The program handles user input using scanf and performs calculations with the sqrt function from the math library. It ensures versatility by allowing the user to calculate both the hypotenuse and one of the other sides of a right-angled triangle.