Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Find the square root of a number using Babylonian method

C Programming Mathematics: Exercise-19 with Solution

Write a C program to find the square root of a number using Babylonian method.

Example 1:
Input: n = 50
Output: 7.071068
Example 2:
Input: n = 17
Output: 4.123106

Sample Solution:

C Code:

#include <stdio.h>

float square_Root(float n) 
{ 

   float a = n; 
        float b = 1; 
        double e = 0.000001; 
        while (a - b > e) { 
            a = (a + b) / 2; 
            b = n / a; 
        } 
   return a; 
} 
  
int main(void)
{ 
    int n = 50; 
    printf("Square root of %d is %f", n, square_Root(n)); 
    n = 17; 
    printf("\nSquare root of %d is %f", n, square_Root(n)); 
    return 0;    
}

Sample Output:

Square root of 50 is 7.071068
Square root of 17 is 4.123106

Flowchart:

Flowchart: Find the square root of a number using Babylonian method.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C programming to find the total number of full staircase rows that can be formed from given number of dices.
Next: Write a C program to multiply two integers without using multiplication, division, bitwise operators, and loops.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP