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

The object file is in binary code, and the machine understands the binary language. So why object files are not executed?
Object files are sources compiled into binary machine language, but they do not have library files, so they are not executed.
Dhruvil
0 0
9
If one leaves a lot of space in a C language code, will it require more space in a hard disk and RAM?
Head files tells system, how to deal with different functions with program such as printf(), scanf() , getche() etc. so you must have reference to it.
Abhishek
0 0
5
How would you round off a value from 1.66 to 2.0?
By adding 1.0 (float) to the integer part (type casted) of 1.66.
Dhanya
0 0
6
What is a local block in C programming?
the statements within the braces { } is known as local block
Rakhi
0 0
6
Is C/C++ language is still important in 2019?
Of course, C language is only the heart of any processor or controller.Because of complexity in C peoples are looking to work on High-level languages like JAVA etc.
Tejas
0 0
7

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

Ask a Question

Related Lessons

Tress And Its Traversal
Depth First Traversals:(a) Inorder (Left, Root, Right) : 4 2 5 1 3(b) Preorder (Root, Left, Right) : 1 2 4 5 3(c) Postorder (Left, Right, Root) : 4 5 2 3 1 Trees are one of the data structures like...

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

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...

Why we need to learn Programming languages?
Language is medium for communication. If two parties like to communicate or exchange the thoughts they must know a language. Language should be understandable by both the Parties. For example A wants to...

Java and C trainer
Always think any conspect with real-time example like Object -- object must have states and behaviour then only we will call that thing is Object like fan is Object (rotating,color)

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