Monday, 29 July 2013

WAP in C++ to find SquareRoot of given number by user?


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

    }

}