Thread: Integer power

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    Integer power

    What i gotta do:

    Write a function called "integerPower(base, exponent)" that calculates and then returns the value of base to the power exponent. For example, if you invoke the function in the following manner:

    integerPower(3,4);

    the function should return the value 81 (3 * 3 * 3 * 3 = 81). Assume that "exponent" is a positive nonzero integer and that "base" is an integer. You should use type "long" to represent the exponent and base. The function should return a "long" value as well. Do NOT use any math library functions as part of your solution.

    Write a main() function to use (and test) your integerPower() function. Your main() function should request a base and exponent value from the user and then invoke the function with the values provided by the user.

    As a general rule, it is prefered that you NOT input or output (scanf or printf) from inside a function. By having the calling program (main() in our case) do all the input and output, your function remains more general purpose.



    What i got .... cant seem to get the function workin properly
    Code:
    #include <stdio.h>
    
    long integerpower( long base, long exponent );
    
    int main()
    {
    	long base;
    	long exponent;
    
    	printf("\nEnter the base value: ");
    	scanf("%d", &base);
    
    	do{
    		printf("Enter the exponent value(must be a positive integer): " );
    		scanf("%d", &exponent);
    		if(exponent<0){
    		printf("\nInvalid data entered, try again!\n" );		
    		}
    	}	while(exponent<0);
    
    	printf("%d raised to the power %d is: %d\n\n", base, exponent , integerpower);
    	return 0; 
    } 
    
    long integerpower(long base, long exponent)
    {
      if (exponent == 1)
        return base;
      else
        return(base * integerpower(base, exponent -1));
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       printf("%ld raised to the power %ld is: %ld\n\n", base, exponent,
              integerpower(base,exponent));
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Easy as pie sir.
    Code:
    #include <stdio.h>
    
    int Power(int base, int pow){
    //Special Cases:
        if (pow == 0) {return 1;}
        if (pow < 0)   {return 0;}  //No negative power is an integer
    
        int result=1;
             for(int i=0;i<pow;i++){
                   result *= base;
             }
        return result;
    }
                
    int main(){
        printf("%d\n",Power(5,3));
        getchar();
        return 0;
    }
    Last edited by Krak; 06-10-2005 at 03:47 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    thx bro....... haha wut a simple mistake ..... awell thx for the correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM