# Thread: To get the sum of digits of a five digit number

1. ## To get the sum of digits of a five digit number

Code:
#include<conio.h>
#include<stdio.h>
void main(void)
{
int d1,d2,d3,d4,d5,sum;
long num;
printf("Enter a five digit number: ");
scanf("%ld",&num);
d1=(num%10);
d2=(num%100)/10;
d3=(num%1000)/100;
d4=(num%10000)/1000;
d5=(num/1000);
sum=d1+d2+d3+d4+d5;
printf("\n The sum of the digis is: %d",sum);
}

2. Conceited programmers are a pain... especially when they are crap.

3. Try typing 50000&#37;10 on calc, it equals 0. Same for the rest, you will always get zero. Also you forgot to specify what the problem is. Experiment your logic using a calculator or pen&paper first, its easier.

4. ...especially when they are wrong?
Code:
\Documents\Code\cs_ex>gcc -o fail.exe fail.c
fail.c: In function `main':
fail.c:4: warning: return type of 'main' is not `int'

\Documents\Code\cs_ex>fail
Enter a five digit number: 20000

The sum of the digis is: 20
\Documents\Code\cs_ex>fail
Enter a five digit number: 12345

The sum of the digis is: 26
So... yeah. Line 1 is a good place to start:
Code:
#include<conio.h>
A dated header, and since you don't use it, you don't need it. I'm surprised it exists on my system.
Line 3:
Code:
void main(void)
Wrong, and my compile spits out warning for it, should be:
Code:
int main(void)
Code:
d5=(num/1000);
You need to recheck that one... add a zero... should be 10000
Finally, since you've changed the return type of main(), add a return 0;

And... you might want to check for negatives:
Code:
\Documents\Code\cs_ex>fail
Enter a five digit number: -12300

The sum of the digis is: -6
Try typing 50000&#37;10 on calc, it equals 0.
As it should? As I read that line in the code, it is correct, and is getting the ones place.

6. Originally Posted by hum_tum2005007
Can't or don't want to?

7. Apart from the obvious mentioned bugs, complete disregard of using arrays, and lack of input validation, you spelt "digits" wrong.

8. lmao; a very cr@ppy version: (sorry cpp not c, just hitme lol)

Code:
stringstream tstream;
string tstring, ta;
int ia = 123, ib=0, ic=0;  //ia = input
char tc;

tstream << ia;
tstring = tstream.str();
cout << tstring << "\n\n";
tstream.str("");

for (int i=0; i<tstring.size(); ++i)
{
cout << tstring[i] << " : ";
string t = tstring.substr(i, 1);
cout << atoi(&t[0]) << "\n";
ic += atoi(&t[0]);
}
cout << "\n" << ic;  //output
/drunk

9. Why use a long for num? It is only a 5digit number. Besides the rest are int... Not that this is a big deal
Also, most of the compilers indeed need main to return an int. Some don't care if it's void. Most don't care if you don't have return. Anyway, depends how much you care about warnings (you should though)...

Here is a bit elegant code to do your job (for any number of digits):
Code:
#include <stdlib.h>
#include <stdio.h>

#define DIGITS 5

int main(void)
{
int sum = 0, num, i;
printf("Enter a five digit number: ");
scanf("&#37;d", &num);
for (i=0; i < DIGITS; ++i) {
sum += num % 10;
num = num / 10;
}
printf("The sum of the digis is: %d\n ",sum);
return 0;
}
Haven't tested it ofcourse. It should work I believe though. It should be ok for negatives or for entering fewer than DIGITS number.
If you test it and it works let me know

EDIT: *hits* gltiich for using CPP. Nothing wrong with the language. But as a basic rule: hit every language-user you are not very familiar with