Implement Square Root Method using C#

Hello friends,

I am here again with another example where we will see how to find square root of a number without using .NET library method.

Let’s start with basic one though it’s not efficient as it considers all the number starting from 0.001 till the difference of multiplying both numbers and the number given is less than the previous iteration.

It’s not efficient as it took 3003 iterations to find the square root of 9.

public static float FindSquareRoot1 (int number)
{
float result = 0;
float diff = 0;
float minDiff = Math.Abs(result * result – number);
int count = 0;
float tempResult = 0;
while (true)
{
tempResult = Convert.ToSingle(count) / 1000;
diff = Math.Abs(tempResult * tempResult – number);
if (diff <= minDiff)
{
minDiff = diff;
result = tempResult;
}
else
return result;
count++;
}
}
Now let’s see another way which is efficient as it uses Binary Search.

It’s really efficient as It took just 17 iterations to find the square root of 9.

public static float FindSquareRoot_BS(int number)
{
float precision = 0.0001f;
float min = 0;
float max = number;
float result = 0;
while (max-min > precision)
{
result = (min + max) / 2;
if ((result*result) >= number)
{
max = result;
}
else
{
min = result;
}
}
return result;
}

Please let me know your comments/suggestion or if you have any better ways to do that.

Hope you liked the post. Please share your comments.

Calculate Fibonacci Series using C# programming

Hello mates,
This is one of the most asked question in interviews, calculating and printing Fibonacci series.
Let’s first try the iterative approach that is simple and prints all the Fibonacci series by passing the length.
Please note that we are starting the series from 0 (instead of 1).
  • public static voidFibonacci_Iterative(int len)
  • {
  •     int a = 0, b = 1, c = 0;
  •     Console.Write(“{0} {1}”, a,b);
  •     for (int i = 2; i < len; i++)
  •     {
  •         c= a + b;
  •         Console.Write(” {0}”, c);
  •         a= b;
  •         b= c;
  •     }
  • }

Test Results:

Input: Fibonacci_Iterative(9);
Output:
We can also use the recursive way to print the series against the length as below.
  • public static voidFibonacci_Recursive(int len)
  • {
  •    Fibonacci_Rec_Temp(0, 1, 1, len);
  • }
  • private static voidFibonacci_Rec_Temp(int a, int b, int counter, int len)
  • {
  •     if (counter <= len)
  •     {
  •         Console.Write(“{0} “, a);
  •         Fibonacci_Rec_Temp(b, a + b, counter+1, len);
  •     }
  • }
Here we have used two functions just to only the length as an input parameter. In the second method 4 parameters are required since we need to continue changing the variable’s position (in other words a -> b and b -> a + b).
We also need to increment the counter in each recursion call and to compare it with the length and continue the loop until it exceeds the length parameter.
Test Results:
Input: Fibonacci_Recursive(11);
Output:

Calculate nth Fibonacci number

Calculating the nth Fibonacci number is not difficult, we just need to the value with the right index.
We will first have a look at an iterative approach.
Here we are using an integer array to keep the Fibonacci numbers until n and returning the nthFibonacci number.
  • public static int GetNthFibonacci_Ite(int n)
  • {
  •     int number = n – 1; //Need to decrement by 1 since we are starting from 0
  •     int[] Fib = new int[number + 1];
  •     Fib[0]= 0;
  •     Fib[1]= 1;
  •     for (int i = 2; i <= number;i++)
  •     {
  •        Fib[i] = Fib[i – 2] + Fib[i – 1];
  •     }
  •     return Fib[number];
  • }
Test Results:
Input: GetNthFibonacci_Ite(4);
Output:
We can also look for a recursive version of that. The only thing to consider is, here we need to a number less than 1 to get the nth Fibonacci number.
For example if we want the 8th Fibonacci number then we need to 8-1 (in other words 7) as an input (we can also create a -through method alternatively).
  • public static int GetNthFibonacci_Rec(int n)
  • {
  •     if ((n == 0) || (n == 1))
  •     {
  •         return n;
  •     }
  •     else
  •         returnGetNthFibonacci_Rec(n – 1) + GetNthFibonacci_Rec(n – 2);
  • }
Test Results:
Input: GetNthFibonacci_Rec(8-1);
Output:
Please inform me of your comments/suggestions or if you have any better way to do it.