I have a programming assignment for CSCI180. The assignment is to create a recursive function power(base, exponent) that when invoked returns base ^ exponent. Everything compiles correctly and when i run the program i enter the base and exponent like prompted. But then the program stops working and i cannot determine why.
Here is the code i have. help would be greatly appreciated.

#include <stdio.h>


int power(int base,int exponent);

int main( void )
{
	int choice;
	int base;
	int exponent;
		
	do{
		printf("enter a number and an exponent.\n");
		scanf("%d,%d", &base, &exponent);
		printf("%d raised to the %d is %d.\n", base, exponent, power(base,exponent));

		printf("would you like to enter another power?\n");
		printf("please type (1 = yes, 2 = no)\n");
		scanf("%d", &choice);

	}while ( choice != 2 );

	return 0;
}


	int power( int base, int exponent)
	{
		if (exponent==1){
			return base;
		}
		else {
			return base * power(base, exponent-1);
		}
               if (exponent < 0)
                        return 1/base * power(base, exponent-1);
		
	}

Recommended Answers

All 2 Replies

line 14 is wrong -- in the format string replace the comma with a space like this: scanf("%d %d", &base, &exponent);

Thanks that was my problem. i hate getting all aggravated because of stupid little errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.