Jul 22, 2019, 10:11 AM

News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids


Function for square root without using sqrt() function

Started by thiruvasagamani, Sep 22, 2008, 05:33 PM

previous topic - next topic
Go Down

thiruvasagamani

Write a function for square root without using sqrt() function

Code: [Select]
#include <stdio.h>
#include <conio.h>

float sqroot( float x)
{
float a , b;
a = x; // copy given value to 'a'
do
{
b = a; // copy value of 'a' to 'b' before 'a' is modify
a = (a + x/a) / 2; // modify 'a' value until we reach sqroot result
}
while( a!= b); // execute loop until a == b
return( a); // 'a ' or 'b' is sqroot of 'x'
}

main()
{
float x;
float sqroot( float);
printf( enter any number \n");
scanf("%f", &x);
if( x > 0)
printf(" SQRT(%f) = %f\n", sqroot( x)); // display sqrt(49) = 7
else
if(x < 0)
printf(" SQRT(%f) = %fi \n", sqroot( -x)); // display sqrt( -49) = 7i
else
printf(" square root is 0 \n");
getch();
}

o/p:

If you enter 'x' values as 49 output is 7
If 'x' value is -25 output is 5i
If 'x' value is 256 output is 16
Thiruvasakamani Karnan


Ashok G

Thanks it is just easy and simple to be integrated with any language

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: SMS Backup | CineBuzz :: Latest Cinema News | My Kids Diary :: Kids magical moment
Copyright 2005 - 2019 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 14 years - Powered by IT Acumens Dedicated Server