finding the max of 3 numbers in c – piece of cake, yo
intro
Oi, you lot! New to C? Wicked, coz today we’re smashing out a quick lil program to find the biggest outta three numbers. It’s dead simple, yeah, but mega useful—like that time I was tryna figure out who nabbed the most chips at movie night (me again, oops). Perfect for getting your head round C without crying. Let’s crack on!
the code bit
Here’s the code—nowt posh, just summat I chucked together. Shoved it in a box so it dont look like rubbish on your blog. have a gander:
int main() { // nearly put void here, d’oh!
float num1,num2,num3,max; // oops no spaces, whatever
printf("gimme first number: ");
scanf("%f",&num1);
printf("second number, quick: ");
scanf("%f" ,&num2); // extra space coz why not
printf("third number now: ");
scanf("%f",&num3);
max=num1; // num1’s the boss for now
if(num2>max) { // num2 wants a go
max=num2;
}
if(num3>max) {
max=num3; // num3’s like “step aside, lads”
}
printf("biggest one’s %.2f\n",max); // forgot ; once, pain!
return 0;
}
what it spits out
Here’s what you see when ya run it. I chucked some numbers in—worked ace, mate!
second number, quick: 8.2
third number now: 6.7
biggest one’s 8.20
how it works, sorta
Right, let’s have a natter bout this like we’re sat on the sofa with a brew—or a bag of crisps, coz I’m starving right now. I’ll ramble it out like my old notes when I was knackered learning C. It’s easy once ya clock it, pinky swear!
1. that `#include
This is me nabbing my tools before we kick off. The `
2. `int main()` funtion
Gotta have a “main” to start—think of it like the door to my flat. `int` means I’m chucking a number out at the end (that `0` is me saying “sweet as!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a twit!
3. boxes: `float num1,num2,num3,max;`
These are my lil buckets for numbers. Went with `float` coz I fancied decimals—like 3.5 or 8.2—not dull whole ones. `max` is where the winner lives. Forgot spaces between ‘em—meh, still works!
4. yapping at ya: `printf` and `scanf`
So yeah, `printf("gimme first number: ")` is me shouting, “Oi, number, now!” Then `scanf("%f",&num1)` grabs whatever you chuck in and stuffs it in `num1`. The `%f` is all “float me up,” and `&` is like a post-it going “here, ya numpty!” Do that three times—job done.
5. digging out the max
Here’s the fun bit! I kick off with `max=num1`—first one’s the king til proven otherwise. Then `if(num2>max)` checks if num2’s got more guts—if it does, it’s the new max. Same game with `if(num3>max)`. It’s like a scrap in the pub—who’s toughest? `max` wins!
6. showing it off: `printf`
Then `printf("biggest one’s %.2f\n",max)` blurts it out. That `%.2f` keeps it neat—two decimals, so 8.20 not some daft 8.199999 mess. Forgot a `;` once—CODE WOULDN’T BUDGE, and I was fuming for ages!
7. `return 0` at the end
That `return 0;` is me legging it outta the program, like, “Ta-ra, we’re sorted!” It’s a C habit—means it all went smooth as a pint.
And that’s yer lot! A cracking lil program to nab the max of three. Fiddle with it—chuck in daft numbers, call it “biggest thingy” if ya feel funky. First time I ran it, I was shoving in -10 and stuff just to test it (still worked, ha!). You’re a legend already, mate—keep at it! 😊