3

Lets say I have an integer called SIN and the scanf input receives 193456787.

so SIN = 193456787;

What I want to do is add up all the other numbers after the first digit.

So 9 + 4 + 6 + 8 = 27

Can somebody please explain to a beginner how to do this?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Derrick Rose
  • 664
  • 2
  • 9
  • 21
  • So for `193456787`, the answer is `49`? – PageNotFound Aug 27 '15 at 02:52
  • If `SIN=1231234`, what the answer should be? – PageNotFound Aug 27 '15 at 02:53
  • @PageNotFound Well if I wanted to add up every other number after 1, the asnwer would be 2 + 1 + 3 = 6. – Derrick Rose Aug 27 '15 at 02:54
  • after 1 I found 93456787 but you added up only 9468 please be clear. – venki Aug 27 '15 at 02:55
  • @PageNotFound HOWEVER if I didnt want add every other number, and just wanted to add every number the answer to 1231234 would be 15. – Derrick Rose Aug 27 '15 at 02:55
  • 1
    @venki I want to specifically pull out every other number after the first digit from the int SIN, and add them, which happens to be 9 + 4 + 6 + 8 = 27. Sorry if i'm not clear enough. – Derrick Rose Aug 27 '15 at 02:58
  • @AsadMahmood: I'm not understanding anything (and thus my answer is probably off-topic). As you're a non-native English speaker, could you please give simpler explanations and sentences so they're easier to understand? – 3442 Aug 27 '15 at 03:01
  • if number is 231568974 you need result as 5 + 8 + 7 ? – venki Aug 27 '15 at 03:02
  • 1
    I've understood this so far: Find the first `'1'` in the sequence. Then sum up every two digits. That's it no? – 3442 Aug 27 '15 at 03:06
  • 1
    I just meant the 1 as an example what I really meant was the first digit, so if it was 345678, I want 4 + 6 + 8 = 18 – Derrick Rose Aug 27 '15 at 03:07
  • So the `>= 10` in my program is correct? – 3442 Aug 27 '15 at 03:07
  • Some English-speaking regions use "every other number" to mean "every second number", i.e. second, fourth, sixth, eighth, etc. – M.M Aug 27 '15 at 03:56

3 Answers3

4

Print the number and then sum every other digit

int sum_every_other_digit_after_first(unsigned long long x) {
  char buf[sizeof x * CHAR_BIT];
  sprintf(buf, "%llu", x);
  char *p = buf;
  int sum = 0;
  while (*p) {
    p++;  // Skip digit
    if (*p) {  
      sum += *p++ - '0';
    }
  }
  return sum;
}

or as inspired by @PageNotFound

int sum_every_other_digit_after_first(unsigned long long x) {
  int esum = 0;
  int osum = 0;
  while (x > 0) {
    esum += x%10;
    x /= 10;
    if (x == 0) {
      return osum;
    }
    osum += x%10;
    x /= 10;
  }
  return esum;
}

or for fun, a recursive solution

int sum_every_other_digit_after_first_r(unsigned long long x, int esum, int osum) {
  if (x >= 100) {
    int digit2 = x % 100;
    esum += digit2 % 10;
    osum += digit2 / 10
    return sum_every_other_digit_after_first_r(x / 100, esum, osum);
  }
  if (x >= 10) {
    return esum + x % 10;
  }
  return osum;
}
sum_every_other_digit_after_first_r(1234567,0,0) --> 12
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
3

Note: Please comment if this is not what you intended, as your question is a little ambigous.

#include <stdio.h>

int main() {
    unsigned number;
    scanf("%u\n", &number);

    unsigned result = 0;

    unsigned tmp = number;
    unsigned numberOfDigits = 0;
    do
        numberOfDigits++;
    while((tmp /= 10) != 0);

    if(numberOfDigits % 2 != 0)
        number /= 10;

    while(number >= 10) {
        result += number % 10;
        number /= 100;    // Skip two digits
    }

    printf("%u\n", result);
}
3442
  • 8,248
  • 2
  • 19
  • 41
  • Surely it'd be `myNumber >= 0` or you'll skip the final digit? – Tommy Aug 27 '15 at 03:00
  • your program is completely different. and OP did not asked program he just asked suggestion to start. – venki Aug 27 '15 at 03:00
  • 1
    @venki: The OP's English is not very good, and thus it's hard to understand what does he means. I explicitly stated that my answer may be completely off-topic if the OP did said something different. – 3442 Aug 27 '15 at 03:04
  • You're right. Though I now think he wants every other number. So the sum of the second, fourth, eighth, tenth, etc. – Tommy Aug 27 '15 at 03:09
  • **Edit**: Modified. I think it's now okay :). – 3442 Aug 27 '15 at 03:12
3

My solution

#include <stdio.h>

int main()
{
    int SIN = 193456787;
    int a = 0, b = 0, cnt = 0;
    while (SIN > 0) {
        if (cnt % 2) b += SIN % 10;
        else a += SIN % 10;
        cnt++;
        SIN /= 10;
    }
    printf("%d\n", cnt%2 ? b : a);
    return 0;
}
PageNotFound
  • 2,320
  • 2
  • 23
  • 34