Home / Programs / Calculate Square Root without using Math.h sqrt Function
🚀 Programming Example

Calculate Square Root without using Math.h sqrt Function

👁 804 Views
💻 Practical Program
📘 Step Learning
Write a C program which will calculate the square root of a number without using math.h sqrt() function and print that sqrt to the STDOUT as floating point number with exactly 2 decimal precision.

💻 Program Code

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
    if(argc==1)
    {
        printf("No arguments");
     return 0;
    }
    else
    {
        int n;
        n=atoi(argv[1]);
        float i=0.00;
        
        while(i*i<=n)
        {
            i=i+0.001;
        }
        
        i=i-0.001;
        printf("%.2f",i);
    }
}
                        

🖥 Program Output

37

6.08
                            

📘 Explanation

Nope
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.