plz tell me how to do the below program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jia123
    New Member
    • Oct 2017
    • 1

    plz tell me how to do the below program

    ask the user to enter a number
    a)get separate digits of this number
    b)find the sum of the digits
  • yamex5
    New Member
    • Apr 2010
    • 3

    #2
    This is a homework question!
    Last edited by yamex5; Oct 30 '17, 11:46 PM. Reason: Homework question!

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Use the itoa() function. This will convert the integer to a string.

      Then just run a loop to display the digits.

      As you display each digit you can add its value to a total which you can display after the string has displayed.

      BTW keep in mind that displaying digits this way uses the ASCII value of the digit. You will need to subtract the offset into the ASCII table to get the numeric value of the digit.

      Comment

      Working...