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

Can a function defined inside another function?
No, you can call other function(s) inside a function but you cannot define a new function in it.
Kamal
0 0
8
What is the most efficient way to store flag values?
A flag is a value used to make a decision between two or more options in the execution of a program. Efficiency in this case is a tradeoff between size and speed. The most memory-space efficient way to...
Deepak
0 0
6
Can include files be nested?
one line can only include one directory. No nesting is applicable.
Himangi
0 0
7
What is a void main() in the C programming language?
Void main () is the entry point for execution in C program. The void is a keyword that represents function will not return anything but a void value. Main is the name of the function and () represents...
Ambar
0 0
5
What exactly is \r in the C language?
In the C language, "\r" represents the carriage return character. When encountered in a string, it instructs the output device to return the cursor to the beginning of the current line. This character...
Sajini
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

What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Find out the Output of the following with reason and get C Language Training fess less by 10%
1. void main() { clrscr(); printf(5+"Beautifull"); getch(); } 2. void main() { int a=50; clrscr(); ...

What Are IT Industries Performance Metrics?
1. Outstanding Expectation: Eligible to get Promotion easily and good salary hike. Always preferrable to go abroad. 2. Exceed Expectation: Can get Promotion as per schedule of company with good salary...

C Program Sample Application
//Standard Library Functions(Header Files used) #include<stdio.h> #include <conio.h> //Main method int main() { // function for clearing screen clrscr(); // function to print the output...

Operators in C
Operators in C Operator: An operator is a symbol that tells the compiler to perform certain mathematical or logical calculations. Operators are used in programs to manipulate data and variables. The...

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