Latest From My Blog

Showing posts with label Square root. Show all posts
Showing posts with label Square root. Show all posts

Programming: Find 'n'th root of K number

Hello friends,

Today I am going to share a program to find 'n'th root of a given 'k' number. So here is the program.

#include<stdio.h>

#include <math.h>

double root1(int,int);

int main()
{
    int n;
    int num1;
    double root;
    printf("Enter a number greater then 1: ");
    scanf("%d",&num1);
    if(num1>1)
    {
        printf("Enter the value for 'n'(the root to be calculated): ");
        scanf("%d",&n);
        root = root1(num1,n);
        printf("%d th Root of %d is %f\n\n", n,num1,root);
    }
    else
        printf("Wrong entry");
    return 0;
}

double root1(int a, int b)
{
    int j;
    double i,k=1;
    double incre = 0.01;

    for(i=1; i<=a; i = i+incre)
    {
        for(j=0;j<b;j++)
        {
            k=k*i;
        }
        if(a<k)
        {
            return(i-incre);
            break;
        }
        else
            k=1;
    }
}
Hope it helps... .... Later.... :)

C++: Program to Find the Square Root of a Number Without using Inbuilt sqrt() Function

Hey folks, hope you people are doing well.

Today I am going to share a program of finding the square-root of a number without using any inbuilt library functions.

So, the basic algorithm goes like this,

Suppose that you want to find square root of the number 49. You know that the square root lies between 0 to 49. So how do you check it.

Let say you take the lower bound as 0 and upper bound as 49. now you take the exact middle value and check whether the square of that middle value is anywhere near to the number.


temp = (lower_bound + upper_bound)/2


You will get temp = 24.5. and temp * temp = 600.25
That way to larger than the number, so we now know that the square root lies between 0 to 24.5. We will continue this procedure till we find the exact square root of the given number.

Hope you got the algorithm. Now the program. Here is the program I've created to test the algorithm, and guess what, it works pretty well..


#include <iostream>
using namespace std;

double sqrtnum(double num) //Our main function to find the square root.
{
double lb=0; //lower bound initialization
double ub=num, temp=0;

/*upper bound initialization and taking 
a temporary variable to save partial results.*/
 

                 int itr = 40;
/*We take iteration variable to make sure 

we perform the loop enough time so that 
we get closer to the exact value of the square root. 
Remember the more the iteration, 
the closer you are to the answer.*/

while(itr != 0)
{
temp=(lb+ub)/2;
if(temp*temp==num) 

                  //Checking whether we reached an exact number.
{
return temp;
}
else if(temp*temp > num)
                   /*If not, re- initialize the 

                    upper or lower bound according to the conditions.*/
{
ub=temp;
}
else
{
lb=temp;
}
itr--;
}
return temp;
}

int main()
{
double num, result;
cout<<"Enter the number\n";
cin>>num;

if(num < 0)
{
cout<<"Can not process the squareroot of negetive number\n";
return 0;
}

cout<<"The square root of the number is: "<<sqrtnum(num);
return 0;
}



Points to remember:
1. The more you keep the value of the iteration in the function, the closer you will get to the answer.
2. Taking all the variables as double is to make sure we can handle larger numbers and the floating points.

Please analyse and let me know what do you think about it. If there is any improvement required please leave a comment below so that I can also learn more.

..... Later folks... :)
+