Write a program that reads an integer between 0 – 999 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digit is 14. Hint: Use the % operator to extract digits and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.
Answer:
The sum of integer digits is the sum of the remainder when the integer is repeatedly modulus’ed by 10 and then divided by 10 until the integer becomes 0. For repetition we can use the while loop. The following is an algorithm for this program using a flow chart.