help solving addition problem in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Sosman

    #16
    [OT] Re: help solving addition problem in C

    Keith Thompson wrote:
    "@$|-|. DUBEY" <dubey.ashwini@ gmail.comwrites :
    >i have a interger
    >val = 99999;
    >and i want to add the content of val i.e., 9+9+9+9+9 = 45;
    >>
    >how to do that....
    >need urgent help
    >
    It's difficult to imagine how adding the digits of the decimal
    representation of an integer could be "urgent". [...]
    The O.P. has been interviewing for jobs in Frankfurt, and has
    met with nothing but rejections. He's trying to overcome them by
    casting out neins.

    --
    Eric.Sosman@sun .com

    Comment

    • Ben Bacarisse

      #17
      Re: help solving addition problem in C

      Hyuga <hyugaricdeau@g mail.comwrites:
      On Oct 14, 7:03 am, ssksakthi -image processing <ssksak...@gmai l.com>
      wrote:
      >On Oct 14, 11:30 am, "@$|-|. DUBEY" <dubey.ashw...@ gmail.comwrote:
      >>
      i have a interger
      val = 99999;
      and i want to add the content of val i.e., 9+9+9+9+9 = 45;
      >>
      how to do that....
      need urgent help
      >>
      >main()
      >{
      >int a,b,sum=0;
      >printf("ente r the number\n");
      >scanf("%d", &a);
      >while(a>0)
      >{
      >>
      >a=a%10;
      >sum=sum+a;
      >a=a/10;}
      >>
      >printf("%d", sum);
      >getch();
      >>
      >}
      >
      Doesn't compile on my system:
      <snip>
      Which I suppose is fine, because the OP can hardly learn anything if
      you do his homework for him. Or maybe that was your point?
      There are so many way this program is wrong (not least that the
      algorithm is all wrong) that I'd like to believe it was all intended.

      --
      Ben.

      Comment

      • Nate Eldredge

        #18
        Re: [OT] Re: help solving addition problem in C

        Eric Sosman <Eric.Sosman@su n.comwrites:
        Keith Thompson wrote:
        >"@$|-|. DUBEY" <dubey.ashwini@ gmail.comwrites :
        >>i have a interger
        >>val = 99999;
        >>and i want to add the content of val i.e., 9+9+9+9+9 = 45;
        >>>
        >>how to do that....
        >>need urgent help
        >>
        >It's difficult to imagine how adding the digits of the decimal
        >representati on of an integer could be "urgent". [...]
        >
        The O.P. has been interviewing for jobs in Frankfurt, and has
        met with nothing but rejections. He's trying to overcome them by
        casting out neins.
        You, sir, are a very bad person.

        Comment

        • CBFalconer

          #19
          Re: help solving addition problem in C

          Keith Thompson wrote:
          "@$|-|. DUBEY" <dubey.ashwini@ gmail.comwrites :
          >
          >i have a interger
          >val = 99999;
          >and i want to add the content of val i.e., 9+9+9+9+9 = 45;
          >>
          >how to do that.... need urgent help
          >
          It's difficult to imagine how adding the digits of the decimal
          representation of an integer could be "urgent". Unless, of
          course, you're asking us to do your homework for you.
          Another possibility - he is trying to develop a check-digit (or a
          check-digit checking) system. Not likely, but feasible.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.

          Comment

          • user923005

            #20
            Re: help solving addition problem in C

            On Oct 14, 3:17 pm, CBFalconer <cbfalco...@yah oo.comwrote:
            Keith Thompson wrote:
            "@$|-|. DUBEY" <dubey.ashw...@ gmail.comwrites :
            >
            i have a interger
            val = 99999;
            and i want to add the content of val i.e., 9+9+9+9+9 = 45;
            >
            how to do that.... need urgent help
            >
            It's difficult to imagine how adding the digits of the decimal
            representation of an integer could be "urgent".  Unless, of
            course, you're asking us to do your homework for you.
            >
            Another possibility - he is trying to develop a check-digit (or a
            check-digit checking) system.  Not likely, but feasible.
            Or even pure-math research:
            The distribution of the sum-of-digits function (1998)
            by Michael Drmota, Johannes Gajdosik
            J. Theor. Nombres Bordx

            Comment

            • user923005

              #21
              Re: help solving addition problem in C

              On Oct 14, 1:14 am, "lovecreatesbea ...@gmail.c0m"
              <lovecreatesbea ...@gmail.comwr ote:
              On Oct 14, 2:30 pm, "@$|-|. DUBEY" <dubey.ashw...@ gmail.comwrote:
              >
              i have a interger
              val = 99999;
              and i want to add the content of val i.e., 9+9+9+9+9 = 45;
              >
              how to do that....
              need urgent help
              >
              Hope it doesn't invoke any UB :)
              >
              /* given eg 54321, return 1 + 2 + 3 + 4 + 5. */
              int my_sum(int n)
              {
                      int sum = 0;
              >
                      for (; n; n /= 10)
                              sum += n % 10;
                      return sum;
              >
              }
              Since you have so neatly destroyed any hope of education for the O.P.,
              let's complete the job:

              #include <stdio.h>
              #include <stdlib.h>
              #include <string.h>
              #include <ctype.h>

              char *revstr(char *s)
              {
              const char *start = s;
              char *end = s;
              char c;

              while (*s++) {;
              }
              s -= 2;
              while (end < s) {
              c = *end;
              *end++ = *s;
              *s-- = c;
              }

              return (start);
              }
              // The digit sum of N can be defined as the sum of the digits of N
              // See: http://mathworld.wolfram.com/DigitSum.html
              unsigned long digit_sum(unsig ned long long n)
              {
              unsigned long sum;
              for (sum = 0; n; n /= 10)
              sum += n % 10;
              return sum;
              }

              // The digit root of N is the digit sum repeated until we have a
              single digit
              unsigned long digit_root(unsi gned long long n)
              {
              while (n 9) {
              n = (unsigned long long) digit_sum(n);
              }
              return (unsigned long) n;
              }

              // What strange magic is this?
              unsigned long dr(unsigned long long n)
              {
              if (n)
              return 1 + (n - 1) % 9;
              return 0;
              }

              // What is so special about these?
              unsigned long long magic_numbers[] =
              {1, 81, 1458, 1729};

              int main(void)
              {
              const size_t s = sizeof magic_numbers / sizeof
              magic_numbers[0];
              size_t index;
              for (index = 0; index < s; index++) {
              const unsigned long l = digit_sum(magic _numbers[index]);
              unsigned long lrev;
              char revnum[12];
              sprintf(revnum, "%lu", l);
              lrev = (unsigned long) atol(revstr(rev num));
              printf("%lu * %lu = %lu\n", l, lrev, l * lrev);
              }
              for (index = 9999999; index <= 10000099; index++) {
              printf("digit root %lu is %lu\n", (unsigned long) index,
              digit_root(inde x));
              printf("dr %lu is %lu\n", (unsigned long) index,
              dr(index));
              }
              return 0;
              }

              Comment

              Working...