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 is a pragma?
In computer programming, a directive pragma is a language construct that specifies how a compiler should process its input. The ' #pragma ' directive is the method specified by the C standard for providing...
Anil
Can an ECE graduate with some knowledge of C language learn SQL?
Yes ECE graduate student learn SQL languuage bcuz SQL is. A structured query language. it all query depends upon the logic. In which table is used Nd database is maitained
Anand
0 0
5
What is the difference between #include< > and #include" "
first one is used for predefined header files.. and second is used for user defined header files
Priya
1 0
6
What is the fee for learning C Language?
In C lang course ,there is not only basics it also having whole package to complete the task and course.cost may be it's depends upon the institute n teaching level
Sk.

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

Ask a Question

Related Lessons

C Program-Prime Number
/*WAP to print a number entered by the user is prime or not*/ //Header files #include<stdio.h>#include<conio.h> //Main function void main(){ int num,i; //Function for clearing screen clrscr();...
S

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

C Program-Infinite Loop[For] Demo
//Header Files #include<stdio.h>#include<conio.h> //Main function void main(){ //Function for clearing screen clrscr(); //Infinite for loop for(;;) { printf("Hello!"); } //Function for...
S

C Program-Error Handling[Program Exit Status]
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main Function void main(){ int dividend=20; int divisor=5; int quotient; //Function for clearing screen clrscr();...
S

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

Recommended Articles

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 >

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 >

Applications engineering is a hot trend in the current IT market.  An applications engineer is responsible for designing and application of technology products relating to various aspects of computing. To accomplish this, he/she has to work collaboratively with the company’s manufacturing, marketing, sales, and customer...

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