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.... :)

6 comments: Leave Your Comments

  1. 3rd root of 8 is 2 but your program gives 1.9900 . How can I correct it??????

    ReplyDelete
    Replies
    1. Change the variable 'incre' to a lower value something like 0.000001 to get higher order of precision!

      Delete
  2. can you do it without using maths function?

    ReplyDelete
    Replies
    1. There is really no need of including math.h.. If you remove it from the program, it will not affect anything..

      Delete
  3. You have
    2
    2 numbers (
    N
    N ,
    M
    M) and find
    N

    M
    MN.

    Input Format:

    First line:
    T
    T i.e number of testcases

    For each test case:

    First line: Two space-separated integers
    N
    N and
    M
    M .

    Output Format:

    For every test case, if it is an integer, output
    N

    M
    MN, else

    1
    −1
    Input Constraints

    1

    T

    10
    5
    1≤T≤105
    1
    <
    N
    <
    30
    1<N<30
    1

    M

    10
    9
    1≤M≤109
    SAMPLE INPUT
    2
    2 9
    3 9
    SAMPLE OUTPUT
    3
    -1








    can u slove this and say

    ReplyDelete

+