check if a number’s prime in c – dead easy, mate
intro
Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna bash out a lil program to see if a number’s prime—ya know, one of them numbers that only divides by 1 and itself. It’s dead simple but proper clever—like when I was tryna figure out if 7 was special (spoiler: it is). Great for getting your head round loops and stuff in C without losing yer marbles. 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 right pig’s ear on your blog. take a peek:
int main() { // nearly did void, ha!
int num,i,flag=0; // flag’s like a red flag, innit
printf("gimme a number: ");
scanf("%d",&num);
if(num<=1) { // 1 and below ain’t prime, mate
printf("%d ain’t prime, ya numpty!\n",num);
}
else {
for(i=2;i<=num/2;i++) { // check half the number, clever eh?
if(num%i==0) { // if it divides, we’re knackered
flag=1; // wave the flag!
break; // leg it outta the loop
}
}
if(flag==0) {
printf("%d’s prime, nice one!\n",num);
}
else {
printf("%d ain’t prime, sorry mate!\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 bang on, mate!
7’s prime, nice one!
gimme a number: 8
8 ain’t prime, sorry mate!
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. I’ll ramble it like my scruffy old notes from when I was faffing with C. It’s a piece of cake once ya suss it, pinky swear!
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 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 tit!
3. boxes: `int num,i,flag=0;`
These are my lil buckets. `num` is the number ya give me, `i` is for looping, and `flag=0` is my signal—starts at 0 for “prime” til we prove otherwise. Like a red flag in a race, innit!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme a number: ")` is me barking, “Oi, number, now!” Then `scanf("%d",&num)` grabs what ya chuck in and shoves it in `num`. `%d` is for integers—no faffy decimals—and `&` is like a post-it going “bung it here, ya twit!”
5. check the small fry: `if(num<=1)`
Prime numbers gotta be bigger than 1—0 and 1 don’t cut it. So `if(num<=1)` catches that and tells ya “ain’t prime” if ya try summat daft. Keeps the riff-raff out!
6. the loop malarky: `for(i=2;i<=num/2;i++)`
If it’s over 1, we loop from 2 to half the number—`for(i=2;i<=num/2;i++)`—coz if summat divides it, it’ll show up by then. `if(num%i==0)` checks if `i` divides `num` with no leftover—if it does, `flag=1` goes up and we `break` outta there. No point checking more!
7. prime or not: `if(flag==0)`
After the loop, `if(flag==0)` means nowt divided it—it’s prime! Else, `flag=1` means it’s not. Then `printf` blurts it out—`%d` slots the number in, and `\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 ritual—means it all went smooth as a greased pig.
And that’s yer lot! A smashing lil program to check if a number’s prime. Fiddle with it—chuck in daft numbers, tweak it to say “prime as pie” if ya fancy. First time I ran this, I kept testing 9 coz I wasn’t sure (it ain’t, duh!). You’re a legend already, mate—keep at it! 😊