Write a C program to Calculate the Square root of a number.

Function Main
    Declare Integer Num
    Declare Real X
    Output "Enter a number: "
    Input Num
    If Num > 1
        Assign X = Num
    Else
        Assign X = 1
    End
    While X * X > Num
        Assign X = (1 / 2) * ( X + ( Num / X ) )
    End
    Output "The square root of the number " & Num & " is: " & X
End

#include <stdio.h>

int main() {
    int Num;
    double X;
    printf("Enter a number: \n");
    scanf("%d", &Num);
    if (Num > 1) {
        X = Num;
    } else {
        X = 1;
    }
    while (X * X > Num) {
        X = (double) 1 / 2 * (X + Num / X);
    }
    printf("The square root of the number %d is: %f\n", Num, X);
    return 0;
}

Enter a number: 
10
The square root of the number 10 is: 3.162278

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.