Find the Factorial of a Number in C – Piece of Piss, Yeah

find the factorial of a number in c – piece of piss, yeah

intro

Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna knock up a lil program to work out the factorial of a number. It’s dead easy but proper cool—like when I was tryna count how many ways I could scoff a pack of biscuits (turns out, a lot). Great for getting your head round loops in C without chucking a wobbly. Let’s have a bash!

code bit

Here’s the code—nowt fancy, just a quick job I slung together. Shoved it in a box so it don’t look like a right state on your blog. have a butcher’s:

#include

int main() { // nearly muffed it with void, ha!
int num,i; // i’s for the loop, innit
long fact=1; // big numbers, mate

printf("gimme a number: ");
scanf("%d",&num);

if(num<0) { // no negatives, ya muppet
printf("oi, factorials don’t work with negatives!\n");
}
else {
for(i=1;i<=num;i++) { // loopity loop!
fact=fact*i; // pile it on
}
printf("factorial of %d is %ld\n",num,fact); // forgot ; once, ugh!
}

return 0;
}

what ya get

Here’s what it spits out when ya run it. I chucked in a few numbers—worked bang on, mate!

gimme a number: 5
factorial of 5 is 120

gimme a number: -3
oi, factorials don’t work with negatives!

how it works, kinda

Right, let’s have a natter bout this like we’re parked up at the chippy with a greasy bag—or maybe a brew, coz I’m parched. I’ll spill it like my tatty old notepad from when I was faffing with C. It’s a doddle once ya clock it, honest!

1. that `#include ` gubbins
This is me nabbing my toolkit before we start. ``—posh way of saying “standard input-output”—lets me use `printf` and `scanf`. Forgot it once, and my code just sat there like a plonker. Proper numpty moment!

2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the front door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sweet as, guv!”), and them curly `{}` bits keep it all snug. Nearly wrote `void main()`—old me was a right tit!

3. boxes: `int num,i; long fact=1;`
These are my lil buckets. `int num` is for the number ya give me, `i` is for looping, and `long fact=1` is where the factorial grows—used `long` coz it gets big quick, like 120 for 5! Started at 1 coz ya can’t start at 0, that’d muck it up!

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme a number: ")` is me hollering, “Oi, number, sharpish!” Then `scanf("%d",&num)` nabs what ya type and chucks it in `num`. `%d` is for integers—no faffy decimals—and `&` is like a post-it going “stick it here, ya twit!”

5. checking for negatives
Factorials don’t play nice with negatives—maths goes wonky—so `if(num<0)` catches that. If ya give me a minus, it’s like, “Oi, no chance!” and bails. Keeps it safe, innit!

6. the loop malarky: `for(i=1;i<=num;i++)`
If it’s not negative, we get to the fun bit! `for(i=1;i<=num;i++)` is a loop—starts `i` at 1, keeps going til it hits `num`, adding 1 each time. Inside, `fact=fact*i` multiplies `fact` by `i` every go—like 1×2×3×4×5 for 5. It’s like piling up biscuits til ya can’t count no more!

7. showing off: `printf`
Then `printf("factorial of %d is %ld\n",num,fact)` blurts the answer. `%d` is for `num`, `%ld` is for `fact` coz it’s a `long`, and `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming for ages!

8. `return 0` to bugger off
That `return 0;` is me scarpering, like, “Cheers, we’re done!” It’s a C thing—means it all went smooth as a greased pig.

And that’s yer lot! A smashing lil program to find a factorial. Have a faff with it—chuck in daft numbers, tweak it to say “big ol’ fact” if ya fancy. First time I ran this, I did 10 and it went massive—nearly broke my brain! You’re a star already, mate—keep smashing it! 😊