LCM of Two Numbers in C – Dead Nifty, Innit

lcm of two numbers in c – dead nifty, 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 the least common multiple—LCM—of two numbers, that smallest number they both divide into nice and neat. It’s dead simple but proper useful—like when I was tryna work out how many pints me and my mate could split evenly (took ages, too smashed). Great for getting yer head round loops and maths 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 yer blog. take a gander:

#include

int gcd(int a, int b) { // lil helper for GCD
while(b!=0) { // keep chopping
int temp=b;
b=a%b; // mod it
a=temp;
}
return a;
}

int lcm(int a, int b) { // LCM magic
return (a*b)/gcd(a,b); // mash em with GCD
}

int main() { // nearly did void, ha!
int num1,num2; // buckets for numbers

printf("gimme first number: ");
scanf("%d",&num1);
printf("gimme second number: ");
scanf("%d",&num2);

if(num1<=0||num2<=0) { // no daft stuff, ya muppet
printf("oi, gimme numbers above 0!\n");
}
else {
int result=lcm(num1,num2); // crunch it
printf("LCM of %d and %d is %d\n",num1,num2,result); // forgot ; once, ugh!
}

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I tested it with a couple numbers—worked bang on, mate!

gimme first number: 6
gimme second number: 8
LCM of 6 and 8 is 24

gimme first number: 15
gimme second number: 25
LCM of 15 and 25 is 75

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 doddle once ya clock it, swear down!

1. that `#include ` gubbins
This is me nicking my toolkit. ``—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: `int num1,num2;`
These are my lil buckets—`num1` and `num2` hold the two numbers ya chuck in. `int` coz we’re keeping it whole and simple, no faffy decimals!

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme first number: ")` is me barking, “Oi, first number, sharpish!” Then `scanf("%d",&num1)` grabs it and shoves it in `num1`. Same for `num2`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”

5. `gcd` funtion
`gcd(int a, int b)` is a lil helper—finds the greatest common divisor using Euclid’s trick. `while(b!=0)` loops: stash `b` in `temp`, `b=a%b` mods it, `a=temp` swaps em. When `b` hits 0, `a`’s the GCD. Like chopping away til ya find the common bit!

6. `lcm` funtion
Here’s the guts! `lcm(int a, int b)` does `(a*b)/gcd(a,b)`—multiply the numbers, divide by their GCD, bam, that’s the LCM. Sneaky maths trick—keeps it smallest and tidy!

7. check the daft ones: `if(num1<=0||num2<=0)`
If ya chuck in 0 or less, `if(num1<=0||num2<=0)` spots it and yells “oi, above 0 only!”—coz LCM don’t play nice with negatives or zeros!

8. chucking it out: `printf` and `lcm`
`int result=lcm(num1,num2)` crunches it, then `printf("LCM of %d and %d is %d\n",num1,num2,result)` shows it off. `%d` slots the numbers, `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

9. `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 the LCM of two numbers. Faff with it—chuck in daft numbers, tweak it to say “common mash” if ya fancy. First time I ran this, I did 4 and 6 and got 12—bang on, eh! You’re a star already, mate—keep smashing it! 😊