Velocity Reviews > not a homework question

# not a homework question

Three Headed Monkey
Guest
Posts: n/a

 03-12-2008

write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

I tried

#include <stdio.h>

int pow(int n)
{
int i,power;
power=n;
for(i=0;i<n;i=i+1)
power=power*power;
return power;
}

void main()
{
int result;
char ignore;
result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1) )))))));
printf("\nresult is %d", result);
printf("\nPress ENTER");
gets(&ignore);
}

but it does not work.

how to do that in "C" standard language?

I am using lcc-win32 compiler & windows 98.

help!

Ian Collins
Guest
Posts: n/a

 03-12-2008
Three Headed Monkey wrote:
> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
>

I don't think you can in standard C, the result will be huge...

--
Ian Collins.

Brice Rebsamen
Guest
Posts: n/a

 03-12-2008
On Mar 12, 12:57 pm, Three Headed Monkey
<(E-Mail Removed)> wrote:
> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))
>
> I tried
>
> #include <stdio.h>
>
> int pow(int n)
> {
> int i,power;
> power=n;
> for(i=0;i<n;i=i+1)
> power=power*power;
> return power;
>
> }
>
> void main()
> {
> int result;
> char ignore;
> result= pow(9,pow(8,pow(7,pow(6,pow(5,pow(4,pow(3,pow(2,1) )))))));
> printf("\nresult is %d", result);
> printf("\nPress ENTER");
> gets(&ignore);
>
> }
>
> but it does not work.
>
> how to do that in "C" standard language?
>
> I am using lcc-win32 compiler & windows 98.
>
> help!

First, you may want to have the pow function take 2 arguments: int
pow(int a, int n) { ... }
Second, the pow function defined in math.h does the job for you,
except that it deals with doubles: float pow(double a, double n)
returns a to the power of n as a float. So if you want to deal with
integers you have to convert the result.
Finally you may want to use a loop to do this. It'd look like this:

#include <math.h>
#include <stdio.h>
int main(){
double res=1;
int n;
for( n=2; n<=9; n++ )
res = pow((double)n,res);
printf("res=%f\n",res);
return 0;
}

Don't forget to link with the math library when compiling (-lm)

However this might overflow, resulting in res reaching inf. You can
try using long double and powl... Or more complicated stuff. Any idea
of what the resulting number might be?

CBFalconer
Guest
Posts: n/a

 03-12-2008
Three Headed Monkey wrote:
>
> write a program in "C" language that computes
> 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

Since 2 xor 1 is identically zero, that value suffices.

x = 0;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Peter Nilsson
Guest
Posts: n/a

 03-12-2008
Three Headed Monkey <(E-Mail Removed)> wrote:
> Subject: not a homework question

Your subject should reflect the nature of your problem,
not merely that you have one.

It's not homework questions we mind, rather it's homework
questions that are quoted verbatim without so much as an
attempt.

> write a program in "C" language that computes
> 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

#include <stdio.h>

int main(void)
{
printf("%d\n", 9^(8^(7^(6^(5^(4^(3^(2^1))))))) );
return 0;
}

If you mean exponentiation, then realise it's pretty big!
Just 5^(4^(3^(2^1))) alone yields a 183000+ digit number.
Raise 6 to the power of that and you have... big!

> I am using lcc-win32 compiler & windows 98.

Ah well... the non-standard extension qfloat should knock
that over easily.

--
Peter

user923005
Guest
Posts: n/a

 03-12-2008
On Mar 11, 9:57*pm, Three Headed Monkey <(E-Mail Removed)>
wrote:
> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

I guess that even LCC's qfloat data type will be too small.
Do you have any idea how many digits there are in that number?

user923005
Guest
Posts: n/a

 03-12-2008
On Mar 11, 10:35*pm, Peter Nilsson <(E-Mail Removed)> wrote:
> Three Headed Monkey <(E-Mail Removed)> wrote:
>
> > Subject: not a homework question

>
> Your subject should reflect the nature of your problem,
> not merely that you have one.
>
> It's not homework questions we mind, rather it's homework
> questions that are quoted verbatim without so much as an
> attempt.
>
> > write a program in "C" language that computes
> > 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

>
> * #include <stdio.h>
>
> * int main(void)
> * {
> * * printf("%d\n", 9^(8^(7^(6^(5^(4^(3^(2^1))))))) );
> * * return 0;
> * }
>
> If you mean exponentiation, then realise it's pretty big!
> Just 5^(4^(3^(2^1))) alone yields a 183000+ digit number.
> Raise 6 to the power of that and you have... big!
>
> > I am using lcc-win32 compiler & windows 98.

>
> Ah well... the non-standard extension qfloat should knock
> that over easily.

I doubt it.
Maple with precision set to 10,000,000 digits overflowed:
> Digits=10000000; y := 9.^(8.^(7.^(6.^(5.^(4.^(3.^(2.^1.)))))));
>

10 = 10000000

Error, (in evalf/power) argument too large
>

Richard Heathfield
Guest
Posts: n/a

 03-12-2008
Three Headed Monkey said:

>
> write a program in "C" language that computes
> 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

#include <stdio.h>

int main(void)
{
printf("%d\n",
9^(8^(7^(6^(5^(4^(3^(2^1))))))));
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Robert Gamble
Guest
Posts: n/a

 03-12-2008
On Mar 12, 12:57 am, Three Headed Monkey
<(E-Mail Removed)> wrote:
> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

Assuming that the caret represents exponentiation (which based on the
context of the rest of your post it is obvious that it does, except
perhaps to Mr. Heathfield ), this isn't a programming problem, it
is a math comprehension problem. Even if it were possible to
calculate the result of this expression you wouldn't be able to store
it. There are more digits in the result than there are particles in
the universe by an unimaginable factor.

--
Robert Gamble

Micah Cowan
Guest
Posts: n/a

 03-12-2008
Three Headed Monkey <(E-Mail Removed)> writes:

> write a program in "C" language that computes 9^(8^(7^(6^(5^(4^(3^(2^1)))))))

No one's mentioned this, so perhaps I'd better:

Are you certain that the expression above (assuming your subject may
just be a slight fib) is intended to represent exponentiation? The ^
is a real operator in C, and means something rather different (**
would be a better choice to represent exponentiation, as it doesn't
have another, real, meaning in C).

Otherwise, you may be best-suited using the standard library's own
pow() function along with a floating point type (double would make
sense, given that's what pow() deals in). OTOH, if you happen to have
an implementation with <tgmath.h> (and don't care to be portable to
them wot don't), you might opt for long double.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Trip Technician Python 7 04-21-2009 08:31 PM joebenjamin C Programming 31 10-11-2007 05:32 PM tmp123 C Programming 36 03-03-2006 02:01 AM FML Computer Support 6 06-25-2005 05:25 PM Charles C++ 5 07-28-2003 10:29 PM

Advertisments