Tuesday , December 6 2016
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

 

Method 2

Method 3

Using function pow instead of sqrt()

Method 4

See the Comments… 

 

Also See:   Program in C for coin toss

Check Also

Hide Mouse Pointer C Program

How to Hide a Mouse Pointer Program in C : This program will show and hide ...

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 *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.