Calc the Area of a Rectangle in C – Easy Peasy, Innit

calc the area of a rectangle in c – easy peasy, innit

intro

Oi, you lot! New to C and wanna do summat dead simple? We’re gonna bash out a lil program to work out the area of a rectangle. It’s nowt fancy but proper handy—like when I was tryna figure out if my rug’d fit in the lounge (it didn’t, gutted). Ace for getting your head round C without a faff. Let’s crack on!

code bit

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

#include

int main() { // nearly did void, doh!
float length,width,area; // float coz decimals, yeah

printf("gimme the length: ");
scanf("%f",&length);

printf("gimme the width: ");
scanf("%f" ,&width); // extra space coz I’m a numpty

area=length*width; // just multiply, innit

printf("area of rectangle is %.2f\n",area); // forgot ; once, ugh!

return 0;
}

what ya see

Here’s what it chucks out when ya run it. I lobbed in some numbers—worked a treat, mate!

gimme the length: 4.5
gimme the width: 3.2
area of rectangle is 14.40

how it works, sorta

Right, let’s have a natter bout this like we’re sat at the caff with a bacon sarnie—or a cuppa, coz I’m gasping. I’ll spill it like my scruffy notes from when I was faffing with C. It’s a doddle once ya twig it, swear down!

1. that `#include ` malarky
This is me nicking my toolkit before we kick off. ``—fancy way of saying “standard input-output”—lets me use `printf` and `scanf`. Forgot it once, and my code just sat there like a lump. Total plonker move!

2. `int main()` funtion
Gotta have a “main” to start—it’s like the front door to my gaff. `int` means I’m chucking a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all tight. Nearly wrote `void main()`—old me was a right muppet!

3. boxes: `float length,width,area;`
These are my lil buckets. Went with `float` coz rectangles can have decimals—like 4.5 or 3.2—not just boring whole numbers. `length` and `width` for the sides, `area` for the answer. Simples!

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme the length: ")` is me barking, “Oi, length, now!” Then `scanf("%f",&length)` grabs what ya chuck in and shoves it in `length`. `%f` is for floats—none of that integer nonsense—and `&` is like a post-it going “bung it here, ya twit!” Do that again for width.

5. doing the maths: `area=length*width;`
Here’s the guts of it! `area=length*width` just multiplies the two—rectangle area’s always length times width, innit? Like when I measured my rug—4.5 by 3.2 equals 14.4. No faffing about!

6. showing off: `printf`
Then `printf("area of rectangle is %.2f\n",area)` blurts it out. `%2f` keeps it to two decimals—nice and tidy, like 14.40 not some daft 14.39999 mess. Forgot a `;` once—CODE WOULDN’T BUDGE, and I was proper raging!

7. `return 0` to leg it
That `return 0;` is me scarpering, like, “Ta-ra, 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 rectangle’s area. Mess with it—chuck in goofy numbers, call it “space” instead of “area” if ya fancy. First time I did this, I put in 0 by mistake and got nowt—duh, me! You’re smashing it already, mate—keep at it! 😊