473,837 Members | 1,553 Online

# Trouble calculating 5th root of a given number using C

Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Regards
Kishor
Nov 14 '05 #1
13 12760

"Kishor" <na***********@ rediffmail.com> wrote in message
news:84******** *************** **@posting.goog le.com...
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number. Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Regards
Kishor

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0
Allan
Nov 14 '05 #2
Allan Bruce wrote:

"Kishor" <na***********@ rediffmail.com> wrote in message
news:84******** *************** **@posting.goog le.com...
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Strengths.

I ran some of the code there through the DS9K, and now there are nasal
demons all over the place :-)

Regards
Kishor

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0

Fortunately, it gives me 1.0 as expected :-) Perhaps you meant this?:

double result = pow(32, 1/5.);

-nrk.
Allan

--
Remove devnull for email
Nov 14 '05 #3
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:bv******** **@news.freedom 2surf.net...
"Kishor" <na***********@ rediffmail.com> wrote in message
news:84******** *************** **@posting.goog le.com...
Hi Friends

given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

Strengths.

Regards
Kishor

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0

Or 1.0 at the very least. Try 1.0/5.0.

This approach is good enough for a homework assignment,
but not industrial strength.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #4
P.J. Plauger writes:
Or 1.0 at the very least. Try 1.0/5.0.

This approach is good enough for a homework assignment,
but not industrial strength.

I take it for granted that you are right. But what would you recommend for
someone who does not wish to make this a life's work?
Nov 14 '05 #5
Kishor wrote:
Hi Friends

Please help me to write a C program to find the 5th (fifth) root of a given number.
Ex:(1) Input : 32
Output : 5th root of 32 is 2

Ex:(1) Input : 243
Output : 5th root of 243 is 3

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <float.h>

inline double root(double x)
{
return pow(x, 0.2);
}
inline double original(double x)
{
return pow(x, 5.);
}

int main(void)
{
size_t pass;
const size_t maxpass = 10;
double x;
const double upperlimit = 1000.;
srand((unsigned ) time(0));
for (pass = 0; pass < maxpass; pass++) {
x = upperlimit * rand() / (1. + RAND_MAX);
printf("number: %.*g\n", DBL_DIG, x);
printf("5th root: %.*g\n", DBL_DIG, x = root(x));
printf("root to 5th power: %.*g\n\n", DBL_DIG, original(x));
}
return 0;
}

number: 349.12674268707 6
5th root: 3.2254968589367 2
root to 5th power: 349.12674268707 6

number: 933.51386301219 5
5th root: 3.9266677391732 4
root to 5th power: 933.51386301219 5

number: 337.09432277828 5
5th root: 3.2029509480995 5
root to 5th power: 337.09432277828 5

number: 921.04862164706
5th root: 3.9161246903652 7
root to 5th power: 921.04862164706

number: 193.95603332668 5
5th root: 2.8677458418489 5
root to 5th power: 193.95603332668 5

number: 536.52641875669 4
5th root: 3.5149424791371 3
root to 5th power: 536.52641875669 4

number: 769.72117181867 4
5th root: 3.7780411723776 9
root to 5th power: 769.72117181867 4

number: 245.49040617421 3
5th root: 3.0061240969129 7
root to 5th power: 245.49040617421 3

number: 818.88967892155 1
5th root: 3.8251201345917 3
root to 5th power: 818.88967892155 1

number: 422.26747702807 2
5th root: 3.3505609816076 8
root to 5th power: 422.26747702807 2

--
Martin Ambuhl
Nov 14 '05 #6
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0

Bullshit. It yields 1.0

--
Martin Ambuhl
Nov 14 '05 #7
On Mon, 02 Feb 2004 18:45:09 GMT, Martin Ambuhl <ma*****@earthl ink.net> wrote:
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0

Bullshit. It yields 1.0

Forgive me if I misunderstand, but could someone explain to me what value 32^0
is? If I read the statement
double result = pow(32, 1/5);

correctly, that's what the statement is computing.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 14 '05 #8
Lew Pitcher <Le*********@td .com> spoke thus:
Forgive me if I misunderstand, but could someone explain to me what value 32^0
is? If I read the statement
double result = pow(32, 1/5);

correctly, that's what the statement is computing.

You're reading it correctly. 32 (or any number other than 0) raised
to the zero power gives 1.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9

"Martin Ambuhl" <ma*****@earthl ink.net> wrote in message
Allan Bruce wrote:

Is one allowed to use the standard math.h ?
If so, then
double result = pow(32, 1/5);
should give you 2.0

Bullshit. It yields 1.0