Hello, Can anybody help me in this program.

Write a C program to find the square root of a given number?

I can understand only C and please i need answer in C only.

In this question i should not use function and i have to write a code for my self. Will anybody help me ...I need this for my test prepration.

Scapu

here you go:

#define GIT_R_DUN   3.00
#include <stdio.h>
#include <math.h>
int main()
{
   double x = GIT_R_DUN;
   printf("root of %f = %f\n",x,sqrt(x));
}

if questioned, tell your professor that NO ONE in the "real world" writes this stuff from scratch, then ask him if he's ever had a "real job". If he still complains, send him here to talk to me.


.

Hi scapu,

Don't lose heart!

Pick out an algorithm, for example Newton's method seems to be rather comfortable to compute the square root of a given number. This method is also identical to the ancient method of the Babylonian to draw the square root of a given number, it s also called the divide-and-average algorithm. http://www.homeschoolmath.net/teaching/square-root-algorithm.php discuss it. Try to design a c program. No matter whether it is complete, compiles with error, or produces imperfect results you should post your tentative steps. Then we will help you.

P.s. I don't agree with the concept of the latter post. The intention of computing the square root that way is not to do it plain "from scratch" but to learn fundamental concepts of numeric analysis, such as approximation, iteration, recurrence, numerical stability, and finally to get acquainted with famous Newton method.

krs,
tesu

float SquareRoot(float number)
  {
      long int inter_value = 0;
      float  x_value = 0.0,
              y_value = 0.0;
      float  f_value = 1.5F;

      x_value = number * 0.5F;
      y_value  = number;
      inter_value  = * ( long int * ) &y_value;
      inter_value  = 0x5f3759df - ( inter_value >> 1 );
      y_value  = * ( float * ) &inter_value;
      y_value  = y_value * ( f_value - ( x_value * y_value * y_value ) );
      y_value  = y_value * ( f_value - ( x_value * y_value * y_value ) );
      return number * y_value;
  }

Just let me know if u face any problems

Regards,
Vijay Bhaskar


Hello, Can anybody help me in this program.

Write a C program to find the square root of a given number?

I can understand only C and please i need answer in C only.

In this question i should not use function and i have to write a code for my self. Will anybody help me ...I need this for my test prepration.

Scapu

commented: read the forum rules, mmkay? +0

Hello, Can anybody help me in this program.

Write a C program to find the square root of a given number?

I can understand only C and please i need answer in C only.

In this question i should not use function and i have to write a code for my self. Will anybody help me ...I need this for my test prepration.

Scapu

cprogamme to find square root

commented: Go away until you've learnt how to use a forum! -4
#include<stdio.h>
#include<conio.h>
float SquareRoot(float num);

void main()
{
    float input, ans;
    clrscr();
    printf("\n Enter The Number : ");
    scanf("%f", &input);
    ans = SquareRoot(input);
    printf("\n Square Root : %f", ans);
    getch();
}

float SquareRoot(float num)
{
    if(num >= 0)
    {
        float x = num;
        int i;
        for(i = 0; i < 20; i ++)
        {
            x =  (((x * x) + num) / (2 * x));
        }
        return x;
    }
}

Actually i am not getting how this code is working?
Could anyone explain this please????

Actually i am not getting how this code is working?
Could anyone explain this please????

Then you need to study harder. The code is easy to follow. Use pencil and paper to follow what it's doing -- kinda like in your math class.

Why are we taking hard bound 20 inside for loop. Can someone explain.

it is just an example. you can input any user defined number through scanf and place it in place of 20..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.