UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I find the square root of a decimal number without using built in functions in C?

Asked by Last Modified  

Follow 3
Answer

Please enter your answer

5+ years of experience in Manual Software testing

void main() { float n,sqrt,temp; clrscr(); printf("Enter a number\n"); scanf("%f",&n); temp=0; sqrt=n/2; while(sqrt!=temp) { temp=sqrt; sqrt=(n/sqrt+sqrt)/2; } printf("square root=%f",sqrt); getch(); }
Comments

You need to create your own program/function for Long Division Method of finding square root of a number. You can search online for the Long Division Method and related C programs.
Comments

Advance Excel And VBA Training

#include"stdio.h" #include"conio.h" void main() { float n,sqrt,temp; clrscr(); printf("Enter a number\n"); scanf("%f",&n); temp=0; sqrt=n/2; while(sqrt!=temp) { temp=sqrt; sqrt=(n/sqrt+sqrt)/2; } printf("square root=%f",sqrt); ...
read more
#include"stdio.h" #include"conio.h" void main() { float n,sqrt,temp; clrscr(); printf("Enter a number\n"); scanf("%f",&n); temp=0; sqrt=n/2; while(sqrt!=temp) { temp=sqrt; sqrt=(n/sqrt+sqrt)/2; } printf("square root=%f",sqrt); getch(); } Say entered number is 4 i.e, n=4. Now, by line 10, sqrt=4/2=2. By line 11, compiler enters while loop. First loop.. temp=2(line 13) sqrt=(4/2 +2)/2=2 temp=sqrt Loop terminates and sqrt=2.000000 Hope this helps! :) Another Way : You can use Newton-Raphson technique of successive approximation to get the root of a number as follows: double squareroot(float n) { double k=n/2; // initial guess while((k*k-n) > 0.0000000001 || (n-k*k) > 0.0000000001) { k=(k+n/k)/2; } return k; } You have to first come up with a initial guess. I chose n/2. Then you calculate how far off the error is by squaring it and subtracting n. If it’s too far off, you modify guess slightly and try again. Repeat until error is small. Babylonian method for square root Algorithm: This method can be derived from (but predates) Newton–Raphson method. 1 Start with an arbitrary positive start value x (the closer to the root, the better). 2 Initialize y=1. 3. Do following until desired approximation is achieved. a) Get the next approximation for root using average of x and y b) Set y=n/x Implementation: /*Returns the square root of n. Note that the function */ float squareRoot(float n) { /*We are using n itself as initial approximation This can definitely be improved */ float x=n; float y=1; float e=0.000001; /* e decides the accuracy level*/ while(x - y > e) { x=(x + y)/2; y=n/x; } return x; } /* Driver program to test above function*/ int main() { int n=50; printf ("Square root of %d is %f", n, squareRoot(n)); getchar(); } Example: n=4 /*n itself is used for initial approximation*/ Initialize x=4, y=1 Next Approximation x=(x + y)/2 (= 2.500000), y=n/x (=1.600000) Next Approximation x=2.050000, y=1.951220 Next Approximation x=2.000610, y=1.999390 Next Approximation x=2.000000, y=2.000000 Terminate as (x - y) > e now. If we are sure that n is a perfect square, then we can use following method. The method can go in infinite loop for non-perfect-square numbers. For example, for 3 the below while loop will never terminate. /*Returns the square root of n. Note that the function will not work for numbers which are not perfect squares*/ unsigned int squareRoot(int n) { int x=n; int y=1; while(x > y) { x = (x + y)/2; y = n/x; } return x; } /* Driver program to test above function*/ int main() { int n = 49; printf (" root of %d is %d", n, squareRoot(n)); getchar(); } read less
Comments

View 1 more Answers

Related Questions

hi guyz! i am dng my 2nd yr in engg n i wanna strengthen my C n C++ prgmmng skills........which s d best way for it?? can u suggest me sum gud books to refer??
you can study the online websites and also solve the output,error oriented program in LET US C book by yaswant
Logeshwari
What are the control statements in C language?
They are broadly classified into three types: decision-making, iteration, and jump statements. Decision-making statements, like if, else if, and switch, determine the flow of execution based on conditions....
Mukund
0 0
5
Should variables be stored in local blocks?
Variables in local blocks are auto variables, meaning created and removed from memory, automatically.
Sayed
How do you list a file's date and time?
int main(void) { struct tm str_time; time_t time_of_day; str_time.tm_year = 2012-1900; str_time.tm_mon = 6; str_time.tm_mday = 5; str_time.tm_hour = 10; str_time.tm_min = 3; str_time.tm_sec...
Neelima
0 0
6
How do you override a defined macro?
We can use the #ifdef and #undef directive to undefine a previously defined macro.
Sharmistha

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Some Tricky Questions in C
Can you write two functions in which one executes before main function and other executes after the main function? #pragma startup #pragma exit Where, priority is optional integer value.For user priority...

C Program-Error Handling
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main function void main(){ int dividend=10; int divisor=0; int quotient; //Function for clearing screen clrscr(); ...
S

Is It Fine To Write “void main()” Or “main()” In C/C++?
The definition: void main() { /* ... */ } Is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1 or the ISO C standard 5.1.2.2.1. A conforming...

Datatypes in C Language
Data types in C Language Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that...

C PROGRAM FOR GENERATING SOUND
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { sound(3000); // sound function have single parameter , this parameter we put integer value its generating...

V. Muthu Ganeshan

0 0
0

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more