Homework Help! Probably very easy...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Homework Help! Probably very easy...

  1. #1
    Join Date
    Sep 2009
    Posts
    1

    Question Homework Help! Probably very easy...

    I have been doing this homework since the day it was assigned and I have 3 problems left out of 11 with absolutely no clue on how to do them.

    Please help me its due tomorrow!

    Here's one of them..

    It wants me to write a program that prompts the user to input an integer and then output both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of -2345 as 2 3 4 5.

    It seems like it would be very easy but I just can't seem to come up with what to do and my professor won't pick up his phone! LOL =[

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Homework Help! Probably very easy...

    There are two ways to approach this sort of problem.
    1) Use modular math to identify each individual digit sequentially, eg, n % 10 is the ones digit of n.
    2) Convert the integer to a string (multiple approaches---stringstream, itoa, sprintf, etc) and then just process the resulting string. Be careful to recognize the difference between the character '1' and the number 1 when adding the digits, though!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center