How to find a number using Pythagoras Theorem using C#?

The Pythagorean theorem states that 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. In C#, we can use the Math.Sqrt() method to calculate the hypotenuse or find missing sides.

Syntax

The basic formula for Pythagorean theorem is −

c² = a² + b²
c = Math.Sqrt(a * a + b * b)

Where c is the hypotenuse, and a and b are the other two sides of the right triangle.

Pythagorean Theorem a b c c² = a² + b² 90°

Finding the Hypotenuse

When you know the lengths of two sides, you can calculate the hypotenuse using the formula −

using System;

public class Program {
   public static void Main(string[] args) {
      double side1, side2, hypotenuse;
      
      side1 = 10;
      side2 = 5;
      
      hypotenuse = Math.Sqrt(side1 * side1 + side2 * side2);
      
      Console.WriteLine("Side 1: " + side1);
      Console.WriteLine("Side 2: " + side2);
      Console.WriteLine("Hypotenuse: " + hypotenuse);
   }
}

The output of the above code is −

Side 1: 10
Side 2: 5
Hypotenuse: 11.1803398874989

Finding a Missing Side

If you know the hypotenuse and one side, you can find the other side using the rearranged formula −

using System;

public class Program {
   public static void Main(string[] args) {
      double hypotenuse, knownSide, unknownSide;
      
      hypotenuse = 13;
      knownSide = 5;
      
      unknownSide = Math.Sqrt(hypotenuse * hypotenuse - knownSide * knownSide);
      
      Console.WriteLine("Hypotenuse: " + hypotenuse);
      Console.WriteLine("Known side: " + knownSide);
      Console.WriteLine("Unknown side: " + unknownSide);
   }
}

The output of the above code is −

Hypotenuse: 13
Known side: 5
Unknown side: 12

Pythagorean Theorem Calculator Method

You can create a reusable method to calculate any missing value in a right triangle −

using System;

public class PythagoreanCalculator {
   public static double FindHypotenuse(double a, double b) {
      return Math.Sqrt(a * a + b * b);
   }
   
   public static double FindSide(double hypotenuse, double knownSide) {
      return Math.Sqrt(hypotenuse * hypotenuse - knownSide * knownSide);
   }
   
   public static void Main(string[] args) {
      Console.WriteLine("Finding hypotenuse:");
      double result1 = FindHypotenuse(3, 4);
      Console.WriteLine("Hypotenuse = " + result1);
      
      Console.WriteLine("\nFinding missing side:");
      double result2 = FindSide(5, 3);
      Console.WriteLine("Missing side = " + result2);
   }
}

The output of the above code is −

Finding hypotenuse:
Hypotenuse = 5

Finding missing side:
Missing side = 4

Conclusion

The Pythagorean theorem in C# is implemented using the Math.Sqrt() method to calculate the square root of the sum of squares. This mathematical formula is essential for finding unknown sides in right triangles and has many practical applications in programming, graphics, and geometry calculations.

Updated on: 2026-03-17T07:04:35+05:30

630 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements