find the sum of digits in c – dead simple, innit
intro
Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna sling together a lil program to tot up the digits of a number—ya know, like adding 1+2+3 from 123. It’s dead easy but proper handy—like when I was tryna count the coins in my pocket after a sesh (lost count, too smashed). Great for getting your head round numbers 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 pig’s ear on your blog. take a gander:
int main() { // nearly did void, ha!
int num,sum=0,digit; // buckets, innit
printf("gimme a number: ");
scanf("%d",&num);
if(num<0) { // no negatives, ya muppet
num=-num; // flip it positive
}
while(num>0) { // keep going til it’s nowt
digit=num%10; // nab the last digit
sum=sum+digit; // chuck it in the pot
num=num/10; // chop off the end
}
printf("sum of digits is %d\n",sum); // forgot ; once, ugh!
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in a number—worked bang on, mate!
sum of digits is 6
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 crisps, 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, swear down!
1. that `#include
This is me nicking my toolkit before we start. `
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 num,sum=0,digit;`
These are my lil buckets. `num` is the number ya give me, `sum=0` starts the tally, `digit` is for nabbing each digit one by one. All `int` coz we’re keeping it simple, no faffy decimals!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme a number: ")` is me barking, “Oi, number, sharpish!” Then `scanf("%d",&num)` grabs what ya chuck in and shoves it in `num`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”
5. fix the negatives: `if(num<0)`
If ya give me a negative, `if(num<0)` spots it and `num=-num` flips it positive—coz minus signs muck up the maths. Keeps it clean, innit!
6. chop and add: `while(num>0)`
Here’s the guts! `while(num>0)` loops til `num`’s nowt. `digit=num%10` nabs the last digit (like 3 from 123), `sum=sum+digit` adds it to the pot, `num=num/10` chops the last digit off (123 becomes 12). Keeps going til it’s all done—like peeling an onion!
7. showing off: `printf`
Then `printf("sum of digits is %d\n",sum)` blurts the total. `%d` slots the sum, `\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 tot up a number’s digits. Faff with it—chuck in daft numbers, tweak it to say “digit pile” if ya fancy. First time I ran this, I tried 99 and got 18—mental, eh! You’re a star already, mate—keep smashing it! 😊