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);
		
	}

Dani AI

Generated

Short summary and root cause
The crash came from an input-format mismatch: the program's input pattern required a specific character between the two numbers, so when you typed them differently the second value was never read and the exponent variable stayed uninitialized. That leads to undefined behavior and can make the program "stop working." spotted the input issue and confirmed it fixed the immediate problem. Always check the number of items scanf actually reads so you can detect malformed input.

Recursion, base cases, and negative exponents
The recursive power routine needs a proper base case for exponent == 0 (return 1). Any handling for negative exponents must happen before returning or recursing, because code placed after a return is unreachable. Also, if you intend to support negative exponents, use a floating return type (for example double) — integer arithmetic will make 1/base evaluate to zero for most bases. Deep recursion can blow the stack for large exponents; use exponentiation-by-squaring to halve recursion depth and improve performance. Example of a compact, correct recursive approach:

double power(double base, int exp) {
    if (exp == 0) return 1.0;
    if (exp < 0) return 1.0 / power(base, -exp);
    if ((exp & 1) == 0) {
        double half = power(base, exp/2);
        return half * half;
    }
    return base * power(base, exp - 1);
}

Practical robustness tips

  • Validate input: check scanf's return value or use fgets + strtol for safer parsing and to clear leftover characters.
  • Validate choice read and guard loops against bad input.
  • Be aware of integer overflow for large bases/exponents; use double or the standard pow function for production code.
  • Test edge cases: exponent 0, 1, negative values, and large exponents to confirm behavior.

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.