To get the sum of digits of a five digit number

This is a discussion on To get the sum of digits of a five digit number within the C Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    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. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Conceited programmers are a pain... especially when they are crap.

  3. #3
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    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.
    Last edited by P4R4N01D; 06-14-2008 at 11:27 PM. Reason: Mayaswell make the number 5didgits
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    ...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.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    i have a problem i cant read your replies.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by hum_tum2005007 View Post
    i have a problem i cant read your replies.
    Can't or don't want to?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Apart from the obvious mentioned bugs, complete disregard of using arrays, and lack of input validation, you spelt "digits" wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    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
    Last edited by gltiich; 06-15-2008 at 02:52 PM.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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
    Last edited by C_ntua; 06-15-2008 at 04:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21