Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program to find the integer square root of a given number. That is th

ID: 3802392 • Letter: 1

Question

1. Write a program to find the integer square root of a given number. That is the largest integer whose square is less than or equal to the given number.

2. Apply the Babylonian Algorithm to compute the square root of 2. This algorithm (so called because it was used by the ancient Babylonians) computes square root of 2 by repeatedly replacing one estimate of x with the closer estimate of x = (x + (2/x))/2. Note that this is simply the average of x and 2/x.

3. Implement a Pyramid class with base and height as data members and surface area() and volume() calculations as member functions.

4. implement a Stack class for stacks of interreges. Include functions push() pop() isEmpty() and isFull(). Limit the stack size to 5.

Explanation / Answer

1)C programm to find the squareroot:

#include <stdio.h>

Void main()

{

Printf(“enter the number”);

Scanf(“%d”,&x);

Int y= int floorSqrt(x);

Printf(“the squareroot is %d“, y);

)

int floorSqrt(int x)

{   

        if (x == 0 || x == 1)

       return x;

       int start = 1, end = x, ans;  

    while (start <= end)

    {       

        int mid = (start + end) / 2;

         // If x is a perfect square

        if (mid*mid == x)

            return mid;

         // Since we need floor, we update answer when mid*mid is

        // smaller than x, and move closer to sqrt(x)

        if (mid*mid < x)

        {

            start = mid + 1;

            ans = mid;

        }

        else // If mid*mid is greater than x

          end = mid - 1;       

    }

    return ans;

}

2) Babylonian Algorithm to compute the square root of 2:

#include <stdio.h>

Void main()

{

Printf(“enter the number”);

Scanf(“%d”,&x);

float i= float sqrt(x);

Printf(“the squareroot is %f “, i);

)

float sqrt(int n)

{

float x = n;

float y = 1;

float e = 0.000001;

while(x - y > e)

{

    x = (x + y)/2;

    y = n/x;

}

return x;

}