Thread: How can i count sum of digits in odd/even places?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    15

    How can i count sum of digits in odd/even places?

    Hello Everyone!
    I want to count the sum of digits in odd places and in even places , and after that their difference
    we know that first digit it's from the right side

    How can I do it with switch?
    For example :

    A number 23543 :

    sum of digits in even places:7 (3+4=7 even places)
    sum of digits in odd places:10 ( 2+5+3=10 odd places)
    difference: -3 (7-10)

    Thank's!!!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What have you tried so far? Post some code and were you are stuck so we can help

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    unsigned int n,m,oddSum=0,evenSum=0;
    char ch
    printf("Please insert the number for the program:");
    scanf("%d",&n);
    printf("Press 1 for qu. 1:\n");
    printf("Press E or e to exit:\n");
    
    ch=getchar();
    
    
    
    switch ( ch )
    {
        case '1':
    		while (n!=0) {
        
           oddSum += n % 10;
    	   n /= 10;
    	    evenSum += n % 10;
    	   n /=10;
        
    
    		}
        printf("Sum of digits in even  places:\n",evenSum);
        printf("Sum of digits in odd  places:\n",oddSum);
        printf("Difference:\n",evenSum-oddSum);
    	
    	
    	break;
    	default:				
    			puts("Wrong choice!");
    			
    
                   
        
    
    
    }
    
    
    }

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Your logic is correct. Just a few mistakes.
    1)
    Code:
    char ch;
    forgot the ;
    Code:
    int main()
    dont use void on main

    2) When you scanf() you leave the '\n' on the buffer. Thus, when you call getchar() you get the newline and don't let the user enter something. You want

    Code:
    printf("Please insert the number for the program:");
    scanf("%d",&n);
    c=getchar(); //clear buffer
    printf("Press 1 for qu. 1:\n");
    printf("Press E or e to exit:\n");
    ch=getchar(); //read choice
    3) You don't use the "Enter E or e" to quit. The program quits anyhow. You might want to put a while(ch != 'e' || ch != 'E') in your program or something else

    Other than that is seems fine!

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    It's still doesn't work

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    We're pretty good here, but we're not clairvoyant.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Here the link of picture cmd

    C - MyPicx.com

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your printf() calls need to have a format pattern in the message text.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Thank U very much!!!

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cowa View Post
    It's still doesn't work
    That's a sentence we should never hear here. Learn how to report problems properly:
    How to Report Bugs Effectively
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count digits
    By Mariana in forum C++ Programming
    Replies: 7
    Last Post: 03-03-2004, 07:56 PM
  2. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  3. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM
  4. Sum of steps
    By Melon in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 06:52 PM
  5. Replies: 3
    Last Post: 11-17-2002, 04:51 PM