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

• 11-16-2009
cowa
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!!!
• 11-16-2009
C_ntua
What have you tried so far? Post some code and were you are stuck so we can help :)
• 11-16-2009
cowa
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!");                                           } }```
• 11-16-2009
C_ntua
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!
• 11-16-2009
cowa
It's still doesn't work :(
• 11-16-2009
Dino
We're pretty good here, but we're not clairvoyant.
• 11-16-2009
cowa
Here the link of picture cmd

C - MyPicx.com
• 11-16-2009
Dino
Your printf() calls need to have a format pattern in the message text.
• 11-16-2009
cowa
Thank U very much!!!
• 11-16-2009
iMalc
Quote:

Originally Posted by cowa
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