C# program to find the square root of a given number without using Math.Sqrt() method

Here, we are going to learn how to find the square root of a given number without using Math.Sqrt() method in C#?
Submitted by Nidhi, on October 09, 2020

Here we will find the square root of a number using the Sqrt() method of Math class by calculating power 1/2 of a specified number.

Program:

The source code to find the square root of a given number without using Math.Sqrt() is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the square root of a given 
//number without using Math.Sqrt() method.

using System;

class SquareRoot
{
    static int Main()
    {
        int     number  =   0;
        double  squareRoot=   0;
        
        Console.Write("Enter the value of number: ");
        number = Convert.ToInt32(Console.ReadLine());
        
        squareRoot = Math.Ceiling(Math.Pow(number, (double)1 / 2));
        Console.WriteLine("Square Root is : " + squareRoot);

        return 0;
    }
}

Output:

Enter the value of number: 36
Square Root is : 6
Press any key to continue . . .

Explanation:

Here, we created a class SquareRoot that contains the Main() method. The Main() method is an entry point for the program. Here we created two variables number and squareRoot that are initialized with 0. Then we read the value of the variable number. After that calculated the square root using Sqrt() method and then printed the square root on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.