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

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

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

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

View 1 more Answers

Related Questions

What does %d mean in the C programming language?
To specify the integer values in printf and scanf.
Vikas
0 0
6
Which should I learn first, C or UNIX?
As you know C is a programming language and UNIX is Operating System if you want to grow your career as Programmer/Developer than go for C/C++ programming as it is very basic programming languages and...
Tushar
0 0
6
Can you suggest me some books for learning C?
"letus c" for c programming yashwant kanetkar
Yamun
What are important topics in C language?
Important topics in the C programming language include: 1. Variables and data types 2. Control flow (loops and conditional statements) 3. Functions and recursion 4. Arrays and strings 5. Pointers...
Everest
0 0
5

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

Ask a Question

Related Lessons

Pointers Concept
Every variable has a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following...

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0


Features Of C Language
Features of C language It is a robust language with rich set of built-in functions and operators that can be used to write any complex program. The C compiler combines the capabilities of an assembly...

Understanding Computer Science Concepts with Images and Videos..
All Computer science concepts relating to programming and software development are only virtual. It cannot be practically shown as a hardware parts of a computer. But for better understanding it should...

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