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…

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();

}

This program is not correct!!

can you explain the code

// 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;

}

#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

do u get answer from this method

this program is not correct

#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);

}

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;

}

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;

}

let the FOR LOOP BE SIMPLE dude:

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

{

m=m;

}

tadaaahhh.. simple..

#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);

}

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;

}

}