gen the fibonacci series in c – dead fun, innit
intro
Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna sling together a lil program to spit out the Fibonacci series—ya know, that mad sequence where each number’s the sum of the two before it. It’s dead simple but proper cool—like when I was tryna count how many pints me and my mate could sink before we lost track (answer: too many). Great for getting your head round loops in C without a right faff. Let’s have a go!
code bit
Here’s the code—nowt posh, just a quick job I knocked up. Shoved it in a box so it don’t look like a total shambles on your blog. have a butcher’s:
int main() { // nearly did void again, doh!
int n,i,first=0,second=1,next; // first two are set, innit
printf("gimme how many terms ya want: ");
scanf("%d",&n);
if(n<=0) { // no negatives or zero, ya muppet
printf("oi, gimme a proper number above 0!\n");
}
else {
printf("fibonacci series for %d terms: ",n);
for(i=0;i
printf("%d ",first);
continue; // skip to next go
}
if(i==1) { // second one too
printf("%d ",second);
continue;
}
next=first+second; // add the last two
printf("%d ",next); // chuck it out
first=second; // shuffle em up
second=next; // keep it rolling
}
printf("\n"); // new line, forgot once, ugh!
}
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in a few terms—worked ace, mate!
fibonacci series for 7 terms: 0 1 1 2 3 5 8
how it works, sorta
Right, let’s have a natter bout this like we’re slumped at the pub 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, honest!
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 front door to my local. `int` means I’m chucking a number out at the end (that `0` says “sweet as, guv!”), and them curly `{}` lads keep it all cosy. Nearly wrote `void main()`—old me was a right plonker!
3. boxes: `int n,i,first=0,second=1,next;`
These are my lil buckets. `n` is how many terms ya want, `i` is for looping, `first=0` and `second=1` are the starters—Fibonacci always kicks off with 0 and 1—and `next` is where we stash the new number each go.
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme how many terms ya want: ")` is me hollering, “Oi, how many, quick!” Then `scanf("%d",&n)` grabs what ya chuck in and shoves it in `n`. `%d` is for integers—no faffy decimals—and `&` is like a post-it going “bung it here, ya twit!”
5. check the daft ones: `if(n<=0)`
If ya give me 0 or less, it’s no good—`if(n<=0)` catches that and tells ya “oi, proper number!” coz ya can’t have a series with nowt. Keeps it sensible, innit!
6. the loop malarky: `for(i=0;i 7. showing off: `printf` 8. `return 0` to leg it And that’s yer lot! A cracking lil program to gen a Fibonacci series. Faff with it—chuck in mad numbers, tweak it to say “fibby list” if ya fancy. First time I ran this, I did 10 terms and lost count—mental, eh! You’re a star already, mate—keep smashing it! 😊
Here’s the guts! `for(i=0;i
`printf("%d ",next)` spits each number with a space, and that final `printf("\n")` tidies it up with a new line. Forgot the `\n` once—IT ALL SQUISHED TOGETHER, and I was proper gutted!
That `return 0;` is me scarpering, like, “Ta-ra, we’re done!” It’s a C thing—means it all went smooth as muck.