help solving addition problem in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • @$|-|. DUBEY

    #1

    help solving addition problem in C

    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


  • Pranav

    #2
    Re: help solving addition problem in C

    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
    take a temporary variable to store sum and initialize it to zero,
    Now by recursively taking modulus and dividing the number you can each
    digit separated and you can go on adding the digit..,

    Comment

    • @$|-|. DUBEY

      #3
      Re: help solving addition problem in C

      On Oct 14, 11:39 am, Pranav <pranav...@gmai l.comwrote:
      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
      >
      take a temporary variable to store sum and initialize it to zero,
      Now by recursively taking modulus and dividing the number you can each
      digit separated and you can go on adding the digit..,


      can u explain with code...
      i didn't understand.....

      Comment

      • Nate Eldredge

        #4
        Re: help solving addition problem in C

        "@$|-|. 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
        Hint:

        val % 10 is the rightmost digit of val.
        val / 10 shifts all digits to the right.

        Comment

        • Nick Keighley

          #5
          Re: help solving addition problem in C

          On 14 Oct, 07:48, "@$|-|. DUBEY" <dubey.ashw...@ gmail.comwrote:
          On Oct 14, 11:39 am, Pranav <pranav...@gmai l.comwrote:
          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
          right...

          take a temporary variable to store sum and initialize it to zero,
          Now by recursively taking modulus and dividing the number you can each
          digit separated and you can go on adding the digit..,
          >
          can u explain with code...
          i didn't understand.....
          not only do you want help with your homework but you want the code as
          well!
          Another approach would be to use sprintf()

          --
          Nick Keighley
          ****Can anyone find the error
          Yes:
          Error on line 0: Lazy programmer.
          (comp.lang.c++)

          Comment

          • @$|-|. DUBEY

            #6
            Re: help solving addition problem in C

            On Oct 14, 11:55 am, Nate Eldredge <n...@vulcan.la nwrote:
            "@$|-|. 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
            >
            Hint:
            >
            val % 10 is the rightmost digit of val.
            val / 10 shifts all digits to the right.
            i got it you saved my day........

            this group rocks.!!!!!!!!! !!!!!!1

            Comment

            • Pranav

              #7
              Re: help solving addition problem in C

              On Oct 14, 12:05 pm, "@$|-|. DUBEY" <dubey.ashw...@ gmail.comwrote:
              On Oct 14, 11:55 am, Nate Eldredge <n...@vulcan.la nwrote:
              >
              "@$|-|. 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
              >
              Hint:
              >
              val % 10 is the rightmost digit of val.
              val / 10 shifts all digits to the right.
              >
              i got it you saved my day........
              >
              this group rocks.!!!!!!!!! !!!!!!1
              Thats good Always try to solve on your own or by following an
              algorithm never expect a valid code.., It will help you a lot,

              Comment

              • Keith Thompson

                #8
                Re: help solving addition problem in C

                "@$|-|. 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.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • Nick Keighley

                  #9
                  Re: help solving addition problem in C

                  On 14 Oct, 08:33, Keith Thompson <ks...@mib.orgw rote:
                  "@$|-|. 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".  
                  perhaps he's a number theorist or something. Tho' a 5
                  digit number seems kind of small. Ah! perhaps "interger"
                  isn't meant to be "integer" but indicates some other
                  mathematical object with more complex properties
                  (tho' Plain Old Integers a pretty complicated).
                  We need more info about these inergers of which you
                  speak.

                  Unless, of course,
                  you're asking us to do your homework for you.
                  good grief!

                  --
                  Nick Keighley

                  Many astrologers think that this concentration on [the sun-sign
                  column] has
                  done untold damage to serious astrology.
                  The Independent

                  Comment

                  • lovecreatesbeauty@gmail.c0m

                    #10
                    Re: help solving addition problem in C

                    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;
                    }

                    Comment

                    • Richard Heathfield

                      #11
                      Re: help solving addition problem in C

                      Keith Thompson said:
                      "@$|-|. 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".
                      <entire Star Trek episode snipped before posting, with extreme prejudice>

                      The above seemed like a good idea at the time, but didn't turn out all that
                      well (I haven't had much practice writing ST scripts!), and I consigned it
                      to the bit bucket for the benefit of humanity. Suffice to say that they're
                      hiding in a ventilation shaft in an extremely expensive set^W^W alien city
                      on an SG-4 planet[1] and, for excellent reasons which never become readily
                      apparent, if they don't find the digital root of this astoundingly
                      enormous number *really* quickly, the Klingons will get the victory, the
                      universe, and the girl. The hero of the hour is a young programmer
                      named... well, that would be giving too much away, but anyway, although he
                      doesn't know how to calculate digital roots (this knowledge having long
                      since been lost to the human race), he *does* know how to open a vortex in
                      space-time and hack into 21st century Usenet.

                      [1] A planet's SG-rating is like closures. If you know, you know. If you
                      don't know, you don't want to.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Tony Mc

                        #12
                        Re: help solving addition problem in C

                        On Mon, 13 Oct 2008 23:30:50 -0700 (PDT), "@$|-|. DUBEY"
                        <dubey.ashwini@ 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
                        >
                        I think that if you are adding the digits you need an intrager rather
                        than an interger.

                        Tony

                        Comment

                        • Doug Miller

                          #13
                          Re: help solving addition problem in C

                          In article <lnk5cbfx67.fsf @nuthaus.mib.or g>, Keith Thompson <kst-u@mib.orgwrote:
                          >"@$|-|. 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". Unless, of course,
                          >you're asking us to do your homework for you.
                          >
                          ... and it's due today.

                          Comment

                          • ssksakthi -image processing

                            #14
                            Re: help solving addition problem in C

                            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("enter the number\n");
                            scanf("%d", &a);
                            while(a>0)
                            {

                            a=a%10;
                            sum=sum+a;
                            a=a/10;
                            }
                            printf("%d", sum);
                            getch();
                            }


                            Comment

                            • Hyuga

                              #15
                              Re: help solving addition problem in C

                              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("enter 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:

                              ~$ gcc -Wall sumdigits.c -o sumdigits
                              sumdigits.c:2: warning: return type defaults to ‘int’
                              sumdigits.c: In function ‘main’:
                              sumdigits.c:4: warning: implicit declaration of function ‘printf’
                              sumdigits.c:4: warning: incompatible implicit declaration of built-in
                              function ‘printf’
                              sumdigits.c:5: warning: implicit declaration of function ‘scanf’
                              sumdigits.c:5: warning: incompatible implicit declaration of built-in
                              function ‘scanf’
                              sumdigits.c:15: warning: implicit declaration of function ‘getch’
                              sumdigits.c:3: warning: unused variable ‘b’
                              sumdigits.c:16: warning: control reaches end of non-void function
                              /tmp/ccalL9CH.o: In function `main':
                              sumdigits.c:(.t ext+0xb1): undefined reference to `getch'
                              collect2: ld returned 1 exit status

                              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?

                              Hyuga

                              Comment

                              Working...