# to the nth power

Donn Pedro donn at brat.UUCP
Mon Nov 5 03:42:26 AEST 1990


In article <90305.005050CJH101 at psuvm.psu.edu>, CJH101 at psuvm.psu.edu (Carl J. Hixon) writes:
> I appologize for bothering you computer wizards with such an elementary
> question but, I'm floundering.  Why am I unable to find an opperator which
> raises a number to a power. (The fortran equivalent of x**n)  Is there such
> an opporator or do I need to write my own function?  It seems like a terrible
> oversite to me that such a common operation was overlooked.
> 
> Any help would be greatly appreciated!  I'm not sure I could even write the
> function, I'm currently writing my first C program ever...
> 
> Thankyou,
> 
> Carl

Straight from K&R.

Page 25.


/*power: raise base to n-th power; n>= 0 */

int power(int base, int n)
{
	int i, p;

	p = 1;
	for (i = 1; i <= n; ++i)
		p = p * base;
	return p;
}


This is written as a function.  On page 24 you will find the same 
stuff in a main().

      Donn F Pedro ....................a.k.a. uunet!nwnexus!mcgp1!brat!donn
         else:  {the known world}!uunet!nwnexus!mcgp1!brat!donn 



More information about the Comp.lang.c mailing list