check if a number’s even or odd in c – easy as pie
intro
Hiya, mate! New to C and wanna do something dead simple? We’re gonna knock up a lil program to see if a number’s even or odd. It’s proper basic but ace—like when I was tryna split sweets with my brother and figure out who got more (he cheated, the git). Great for getting your head round C without a meltdown. Let’s go!
code bit
Here’s the code—nowt special, just summat I slung together. Chuckeda it in a box so it don’t look like a dog’s dinner on your blog. have a butcher’s:
int main() { // nearly did void again, doh!
int num; // just one number, innit
printf("gimme a number: ");
scanf("%d",&num); // %d coz it’s an int, yeah
if(num%2==0) { // that % thing’s modulo, mate
printf("%d’s even, nice one!\n",num);
}
else {
printf("%d’s odd, fair enough!\n",num); // forgot ; once, ugh
}
return 0;
}
what ya get
Here’s what it spits out when ya run it. I chucked in a couple numbers—worked a treat!
6’s even, nice one!
gimme a number: 7
7’s odd, fair enough!
how it works, kinda
Right, let’s natter bout this like we’re slouched on the couch with a bag of crisps—or maybe a sarnie, coz I’m peckish. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya twig 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—think of it like the front gate to my gaff. `int` means I’m lobbing a number out at the end (that `0` says “all good, guv!”), and them curly `{}` bits keep it all cosy. Nearly put `void main()`—old me was a right muppet!
3. one lil box: `int num;`
Variables are just buckets, yeah? I went with `int` this time coz we’re just doing whole numbers—like 6 or 7—no faffy decimals. Just one bucket, `num`, to shove our number in. Sorted!
4. chatting at ya: `printf` and `scanf`
So, `printf("gimme a number: ")` is me hollering, “Oi, number, now!” Then `scanf("%d",&num)` nabs what ya type and chucks it in `num`. That `%d` is for integers—none of that float nonsense—and `&` is like a post-it going “bung it here, mate!” Dead easy.
5. even or odd check
Here’s the good stuff! `if(num%2==0)`—that `%` is modulo, fancy word for “what’s left after dividing.” If `num` divided by 2 has no leftover (equals 0), it’s even—boom, we print that. If not, the `else` kicks in and says it’s odd. Like flipping a coin, but nerdier!
6. showing it: `printf`
Then `printf("%d’s even, nice one!\n",num)` or the odd version blurts it out. `%d` slots the number in, and `\n` gives ya a new line so it ain’t all squished. Forgot a `;` once—CODE DIED, and I was proper gutted for ages!
7. `return 0` to finish
That `return 0;` is me legging it, like, “Cheers, we’re done!” It’s a C thing—means it all went smooth as muck.
And there ya go! A cracking lil program to check even or odd. Have a mess with it—chuck in mad numbers, tweak the words to “evensies” or whatever tickles ya. First time I did this, I kept testing 0 coz I wasn’t sure (it’s even, by the way!). You’re a champ already, mate—keep smashing it! 😊