- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find the square root of a given number
The process of finding the square root of a number can be divided into two steps. One step is to find integer part and the second one is for fraction part.
Algorithm
- Define value n to find the square root of.
- Define variable i and set it to 1. (For integer part)
- Define variable p and set it to 0.00001. (For fraction part)
- While i*i is less than n, increment i.
- Step 4 should produce the integer part so far.
- While i*i is less than n, add p to i.
- Now i have the square root value of n.
Example
public class SquareRoot { public static void main(String args[]){ int n = 24; double i, precision = 0.00001; for(i = 1; i*i <=n; ++i); for(--i; i*i < n; i += precision); System.out.println("Square root of given number "+i); } }
Output
Square root of given number 4.898979999965967
Advertisements