Home / C / c program to find the square root of a number without using sqrt()

c program to find the square root of a number without using sqrt()

Can you write a c program to find the square root of a number without using sqrt() function or other predefined functions. This has been a great challenge in interview as you can do it by using newtons algorithm which you should previously know for implementing. However here are mentioned some useful methods to solve this question.

Method 1

#include<stdio.h>
void main()
{
 float m,n;
 float num;
 n=0.0001;   // This is taken small so that we can calculate upto decimal places also
 printf("ENTER A NUMBER : ");
 scanf("%f",&num);

 for(m=0;m<num;m=m+n)
 {
  if((m*m)>num)
  {
   m=m-n;         // This if() is used to calculate the final value as soon as the square of the number exceeds
   break;      //  the number then we deduct the value exceeded and stop the procedure using break; this is our final value which is stored in m;
  }              
 }
 printf("%.2f",m);
 getch();
 return 1;
}

 

Method 2

#include<stdio.h>
#include<conio.h>
double sqr(double , double);
void main()
{
double i,j,n;
printf("enter a number\n");
scanf("%lf",&n);
j=n*100;
for(i=1;i<=n*5;i++)
{
j=sqr(j,n);
}
printf("\nresult is %lf",j);
getch();
}
double sqr(double j, double n)
{
return (j+(n/j))/2;   // Check out newtons Algorithm for 
}

Method 3

Using function pow instead of sqrt()

#include<stdio.h>
#include<conio.h>
double squareroot(double n)
{
    return pow(n,.5);
}

int  main()
{
    double n,result;
printf("enter a number\n");
scanf("%lf",&n);
result=squareroot(n);
printf("%lf",result);
return 1;
}

Method 4

See the Comments… 

 

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

13 comments

  1. program to find the square root of a number without using math function
    //This program will work till 3 decimal digit
    #include
    #include
    void main()
    {
    double i,j,n;
    printf(“enter a numbern”);
    scanf(“%lf”,&n);
    j=n;
    for(i=1;i<=n*2;i++)
    {
    j=j+(n/j))/2;
    }
    printf("nresult is %lf",j);
    getch();
    }

  2. // square root without using sqrt function
    this is update to previous question as i have to remove it for some reasons…. to find square root dp
    int main()
    {

    int t;
    scanf(“%d”,&t);
    int input[10001];
    char temp_s[25];
    for(int k=0;k<t;k++) {
    int n,query;
    LL temp_res=0,x_ll;
    double x_d;
    scanf("%d%d",&n,&query);

    for(int i=0; i<n; i++) {
    scanf("%d",&input[i]);
    if(input[i]<0) input[i]+=MOD;
    temp_res = (temp_res+input[i])%MOD;
    }
    for(int i=0; ix_d) temp_ans–;
    }
    if(temp_ans == 1) break ;
    temp_ans–;
    temp_ans=temp_ans%MOD;
    ans = (ans+(input[index-1]*temp_ans)%MOD)%MOD;
    index++;
    }
    cout<<ans<<" ";
    }
    cout<<endl;
    }
    return 0;
    }

  3. #include
    #include
    #include
    double squareroot(double n)
    {
    return pow(n,.5);
    }

    void main()
    {
    double n,result;
    printf(“Enter a number\n”);
    scanf(“%lf”,&n);
    result=squareroot(n);
    printf(“square root of %lf is %lf”,n,result);
    getch();
    }

    which method it is

  4. #include
    void main()
    {
    int i,n;
    printf(“enter the no”);
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    if(n%i==0 && i*i==n)
    printf("%d",i);
    }

  5. use this code to find any root of any no.
    #include
    using namespace std;
    #include
    int main()
    {
    float i,r;
    int n,m;
    cout<>n;
    cout<>m;
    r=0.00000001;
    for(i=0;in)
    {
    break;
    }
    }
    cout<<i;
    return 1;
    }

  6. USE THIS CODE TO FIND ANY ROOT OF ANY NUMBER
    #include
    using namespace std;
    #include
    int main()
    {
    float i,r;
    int n,m;
    cout<>n;
    cout<>m;
    r=0.00000001;
    for(i=0;in)
    {
    break;
    }
    }
    cout<<i;
    return 1;
    }

  7. let the FOR LOOP BE SIMPLE dude:

    for ( m=0 ; m*m <= num ; m+=n)
    {
    m=m;
    }

    tadaaahhh.. simple..

  8. #include
    void main()
    {
    int x,n;
    float root;
    x=1;
    printf(“enter the value of n\n”);
    scanf(“%d”,&n);
    for(i=1;i<n;i++)
    {
    root=(x*x+n)/(2*x);
    x=root;
    }
    printf("sqrt(%f)=%f",n,root);
    }

  9. import java.util.Scanner;

    class Test {

    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    double n=sc.nextDouble();
    sc.close();

    double sqrt=SqrtNumber(n);

    System.out.println(sqrt);

    }

    static double SqrtNumber(double num)
    {
    double lower_bound=0;
    double upper_bound=num;
    double temp=0;

    int nCount = 50;

    while(nCount != 0)
    {
    temp=(lower_bound+upper_bound)/2;
    if(temp*temp==num)
    {
    return temp;
    }
    else if(temp*temp > num)

    {
    upper_bound = temp;
    }
    else
    {
    lower_bound = temp;
    }
    nCount–;
    }
    return temp;
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *