Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   not a homework question (http://www.velocityreviews.com/forums/t597964-not-a-homework-question.html)

Three Headed Monkey 03-12-2008 04:57 AM

not a homework question
 

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 03-12-2008 05:19 AM

Re: not a homework question
 
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 03-12-2008 05:25 AM

Re: not a homework question
 
On Mar 12, 12:57 pm, Three Headed Monkey
<four_headed_mon...@yahoo.com> 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 03-12-2008 05:33 AM

Re: not a homework question
 
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 03-12-2008 05:35 AM

Re: not a homework question
 
Three Headed Monkey <four_headed_mon...@yahoo.com> 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 03-12-2008 05:41 AM

Re: not a homework question
 
On Mar 11, 9:57*pm, Three Headed Monkey <four_headed_mon...@yahoo.com>
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 03-12-2008 05:48 AM

Re: not a homework question
 
On Mar 11, 10:35*pm, Peter Nilsson <ai...@acay.com.au> wrote:
> Three Headed Monkey <four_headed_mon...@yahoo.com> 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 03-12-2008 05:51 AM

Re: not a homework question
 
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 03-12-2008 05:59 AM

Re: not a homework question
 
On Mar 12, 12:57 am, Three Headed Monkey
<four_headed_mon...@yahoo.com> 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 03-12-2008 06:17 AM

Re: not a homework question
 
Three Headed Monkey <four_headed_monkey@yahoo.com> 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/


All times are GMT. The time now is 12:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57