1,105,406 Community Members

program to find the square root of a given number

Member Avatar
scapu
Newbie Poster
3 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 
Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
-2
 

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.


.

Member Avatar
tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 135 [?]
Q&As Helped to Solve: 98 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
chsarp_vijay
Newbie Poster
19 posts since May 2008
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 
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

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
0
 

hey, Vijay, please become familiar with the rules of the forums... you know, the ones that are posted as "ANNOUNCEMENTS" and stuck to the top of every forum here.

(1) http://www.daniweb.com/forums/announcement118-2.html

and

(2) http://www.daniweb.com/forums/announcement118-3.html


.

Member Avatar
anvesh kumar
Newbie Poster
1 post since Jan 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

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

Member Avatar
abcdea
Newbie Poster
1 post since Jul 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
#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;
    }
}
Member Avatar
Govardhan92
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

abcd_win
Newbie Poster
1 post since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
goyalPriya
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article