0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Square Root of a Number

In C#, you can find the square root of a number using Math.Sqrt(number). For example, double result = Math.Sqrt(16); will give you 4.
📋

Examples

Input16
Output4
Input25
Output5
Input0
Output0
🧠

How to Think About It

To find the square root of a number, think of it as finding a value that when multiplied by itself gives the original number. We use the built-in Math.Sqrt function in C# which does this calculation for us easily.
📐

Algorithm

1
Get the input number from the user.
2
Use the built-in function to calculate the square root of the number.
3
Display the result to the user.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        double number = Convert.ToDouble(Console.ReadLine());
        double squareRoot = Math.Sqrt(number);
        Console.WriteLine($"Square root of {number} is {squareRoot}");
    }
}
Output
Enter a number: 16 Square root of 16 is 4
🔍

Dry Run

Let's trace the input 16 through the code

1

Input number

User enters 16, so number = 16

2

Calculate square root

squareRoot = Math.Sqrt(16) which equals 4

3

Output result

Prints 'Square root of 16 is 4'

StepVariableValue
1number16
2squareRoot4
3outputSquare root of 16 is 4
💡

Why This Works

Step 1: Reading input

We read the number from the user using Console.ReadLine() and convert it to a double for decimal support.

Step 2: Calculating square root

We use Math.Sqrt() which is a built-in C# method that returns the square root of the given number.

Step 3: Displaying result

We print the result using Console.WriteLine() with string interpolation to show the original number and its square root.

🔄

Alternative Approaches

Using exponentiation operator
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        double number = Convert.ToDouble(Console.ReadLine());
        double squareRoot = Math.Pow(number, 0.5);
        Console.WriteLine($"Square root of {number} is {squareRoot}");
    }
}
This uses <code>Math.Pow</code> with 0.5 as the exponent to find the square root, which is mathematically equivalent but slightly less direct than <code>Math.Sqrt</code>.
Manual approximation (Newton's method)
csharp
using System;
class Program {
    static double SqrtNewton(double n) {
        double x = n;
        double y = 1;
        double e = 0.00001; // precision
        while (x - y > e) {
            x = (x + y) / 2;
            y = n / x;
        }
        return x;
    }
    static void Main() {
        Console.Write("Enter a number: ");
        double number = Convert.ToDouble(Console.ReadLine());
        double squareRoot = SqrtNewton(number);
        Console.WriteLine($"Square root of {number} is {squareRoot}");
    }
}
This method approximates the square root using Newton's iterative method, useful for learning but less efficient than built-in functions.

Complexity: O(1) time, O(1) space

Time Complexity

Using Math.Sqrt is a constant time operation because it uses optimized CPU instructions or library calls.

Space Complexity

The program uses a fixed amount of memory for variables, so space complexity is constant.

Which Approach is Fastest?

Math.Sqrt is fastest and most reliable; manual methods like Newton's are slower and mainly educational.

ApproachTimeSpaceBest For
Math.SqrtO(1)O(1)Fast, simple, built-in
Math.Pow with 0.5O(1)O(1)Alternative, less direct
Newton's methodO(k) where k is iterationsO(1)Learning approximation, no built-in
💡
Use Math.Sqrt for a simple and fast way to find square roots in C#.
⚠️
Forgetting to convert the input string to a number before calculating the square root causes errors.