Simple Grade Calc in C – Dead Easy, Innit

simple grade calc in c – dead easy, innit

intro

Oi, you lot! New to C and fancy summat a bit handy? We’re gonna sling together a lil program to figure out a grade from a mark—ya know, like turning 85 into an “A” or summat. It’s dead simple but proper useful—like when I was tryna work out if I’d passed my pub quiz score (barely scraped it). Great for getting your head round if-else 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 gander:

#include

int main() { // nearly did void, ha!
float mark; // float coz marks can be decimals, innit

printf("gimme yer mark (0-100): ");
scanf("%f",&mark);

if(mark<0||mark>100) { // keep it sensible, ya muppet
printf("oi, gimme a mark between 0 and 100!\n");
}
else if(mark>=90) { // top notch!
printf("grade’s A, smashing it!\n");
}
else if(mark>=80) { // still ace
printf("grade’s B, nice one!\n");
}
else if(mark>=70) { // decent
printf("grade’s C, fair play!\n");
}
else if(mark>=60) { // just about
printf("grade’s D, scrape by!\n");
}
else { // oof, mate
printf("grade’s F, better luck next time!\n"); // forgot ; once, ugh!
}

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I lobbed in a couple marks—worked bang on, mate!

gimme yer mark (0-100): 85
grade’s B, nice one!

gimme yer mark (0-100): 45
grade’s F, better luck next time!

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 pork scratchings, 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 piece of piss once ya clock it, honest!

1. that `#include ` gubbins
This is me nicking my toolkit before we start. ``—fancy pants “standard input-output”—lets me use `printf` and `scanf`. Forgot it once, and my code just sat there like a plonker. Total mug move!

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: `float mark;`
This is my lil bucket. `mark` is the score ya give me—went with `float` coz marks can be decimals, like 85.5, not just boring whole numbers. Keeps it real!

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme yer mark (0-100): ")` is me barking, “Oi, mark, sharpish!” Then `scanf("%f",&mark)` grabs what ya chuck in and shoves it in `mark`. `%f` for floats, `&` like a post-it saying “bung it here, ya twit!”

5. check the daft ones: `if(mark<0||mark>100)`
If ya give me summat daft like -5 or 150, `if(mark<0||mark>100)` catches it and yells “oi, 0 to 100 only!” Keeps the riff-raff out, innit!

6. grade it: `else if(mark>=90)`
Here’s the guts! `else if(mark>=90)` checks if it’s 90 or up—bam, “A”. `else if(mark>=80)` for 80-89, “B”, and so on—70-79 “C”, 60-69 “D”, anything less is “F”. Like a pub quiz leaderboard, but with less shouting!

7. showing off: `printf`
Then `printf("grade’s A, smashing it!\n")` or whatever blurts the grade. `\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 grade. Faff with it—chuck in daft marks, tweak it to say “top lad” for an A if ya fancy. First time I ran this, I chucked in 75 and got a C—fair play, eh! You’re a star already, mate—keep smashing it! 😊