Square Root of a Number | Write a program in Java to find the square root of a number without using Math.sqrt(). | ISC Computer Science | ICSE Computer Applications

Square root of a number

Algorithm

  1.   Start
  2. n:=0
  3. i:=0
  4. k:=0
  5. Print "Enter a number"
  6. Read n
  7. i=1;
  8. k=0
  9. If i*i<=n, go to step 10 or else go to step 15
  10. If i*i==n, go to step 11 or else go to step 13
  11. k++.
  12. Go to step 
  13. i++.
  14. Go to step 9
  15. If k>0, go to step 16 or else go to step 17
  16. Print "The square root is "+i. Go to step 18
  17. Print "The given number is not a perfect square"
  18. Stop

Program

Buffered Reader

import java.io.*;
class SquareRoot
{
    int n, i, k;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter a number");
        n=Integer.parseInt(br.readLine());
    }
    
    void root()
    {
        i=1;
        k=0;
        while(i*i<=n)
        {
            if(i*i==n)
            {
                k++;
                break;
            }
            i++;
        }
    }
    
    void display()
    {
        if(k>0)
        {
            System.out.println("The square root is "+i);
        }
        else
        {
            System.out.println("The given number is not a perfect square");
        }
    }
    
    void main() throws IOException
    {
        accept();
        root();
        display();
    }
}

Scanner

import java.util.*;
class SquareRoot
{
    int n, i, k;
    
    void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        n=sc.nextInt();
    }
    
    void root()
    {
        i=1;
        k=0;
        while(i*i<=n)
        {
            if(i*i==n)
            {
                k++;
                break;
            }
            i++;
        }
    }
    
    void display()
    {
        if(k>0)
        {
            System.out.println("The square root is "+i);
        }
        else
        {
            System.out.println("The given number is not a perfect square");
        }
    }
    
    void main() 
    {
        accept();
        root();
        display();
    }
}

Documentation

Variable Description

 Data TypeVariable NameDescription
 intTo accept a number from the user  
 intTo store the square root of the number 
 intTo act as a counter variable  

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user 
 voidroot() To find the square root of the number 
 voiddisplay() To display the result of the program 
 voidmain() To act as an entry gateway for the compiler. 

Output


Square root of number output




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences