calc power of a number with recursion in c – dead mad, innit
intro
Oi, you lot! New to C and fancy summat a bit bonkers? We’re gonna sling together a lil program to work out a number raised to a power—like 2 to the 3 is 8—using recursion, where the code calls itself like some mad loop. It’s dead simple but proper mental—like when I was tryna count how many pints I’d had by doubling the last tally (lost it after 4). Great for getting your head round recursion in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on your blog. take a peek:
int power(int base, int exp); // lil helper funtion, innit
int main() { // nearly did void, ha!
int base,exp; // buckets, mate
printf("gimme the base number: ");
scanf("%d",&base);
printf("gimme the power: ");
scanf("%d",&exp);
if(exp<0) { // no negatives, ya muppet
printf("oi, gimme a power 0 or up!\n");
}
else {
printf("%d to the power %d is %d\n",base,exp,power(base,exp)); // forgot ; once, ugh!
}
return 0;
}
int power(int base, int exp) { // the recursive bit!
if(exp==0) { // base case, innit
return 1; // anything to 0 is 1
}
return base*power(base,exp-1); // call itself, mad eh!
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in a couple numbers—worked bang on, mate!
gimme the power: 3
2 to the power 3 is 8
gimme the base number: 5
gimme the power: 0
5 to the power 0 is 1
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of crisps, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, pinky swear!
1. that `#include
This is me nicking my toolkit before we start. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `int base,exp;`
These are my lil buckets in `main`. `base` is the number ya wanna power up, `exp` is how many times ya multiply it. Kept it `int` coz we’re doing whole numbers, no faffy decimals!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme the base number: ")` is me barking, “Oi, base, now!” Then `scanf("%d",&base)` grabs it. Same for `printf("gimme the power: ")` and `scanf("%d",&exp)`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”
5. check the daft ones: `if(exp<0)`
If ya give me a negative power, `if(exp<0)` spots it and yells “oi, 0 or up only!”—coz negatives get messy with recursion and I ain’t faffing with that!
6. the recursive malarky: `int power(int base, int exp)`
Here’s the guts! `int power(int base, int exp)` is a lil helper funtion. If `exp==0`, it returns 1—coz anything to the 0 is 1, innit. Else, it does `base*power(base,exp-1)`—multiplies `base` by itself with `exp` dropped by 1, calling itself til it hits 0. Like 2³ = 2×2×2—mental, eh!
7. showing off: `printf`
Then `printf("%d to the power %d is %d\n",base,exp,power(base,exp))` blurts the answer—`power(base,exp)` does the heavy lifting. `%d` slots the numbers, `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
8. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.
And that’s yer lot! A cracking lil program to calc a power with recursion. Faff with it—chuck in daft numbers, tweak it to say “power whack” if ya fancy. First time I ran this, I did 3 to the 4 and got 81—blew my tiny mind! You’re a star already, mate—keep smashing it! 😊