Write a program that prompts the user to input an integer and then outputs 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 sum = 18 •8030 as 8 0 3 0 sum = 11 •2345526 as 2 3 5 6 6 2 6 sum = 30 •-2345 as 2 3 4 5 sum = -14 To solve this problem consider the polynomial expansion of the integer, for example: 3456 = 3 * 10^3+ 4 * 10^2+ 5 * 10^1+ 6 * 10^0 To extract the digits from the right to left use: digit = number % 10 // update sum // output digit number = number / 10 // (use integer division) To extract the digits from left to right, use a similar strategy but you will need to know the highest power represented. This can be done by using two loops, the first loop extracts digits from right to left, keeping track of the number of times the loop executes and accumulating the sum. The second loop can use the value of the counter to extract digits from left to right for the final display. Make sure your program produces the output shown above.Include comments in the code which describe the program and include your name (and the name of your programming partner if you have one). C++
Write a program that prompts the user to input an integer and then outputs 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 sum = 18
•8030 as 8 0 3 0 sum = 11
•2345526 as 2 3 5 6 6 2 6 sum = 30
•-2345 as 2 3 4 5 sum = -14
To solve this problem consider the polynomial expansion of the integer, for example:
3456 = 3 * 10^3+ 4 * 10^2+ 5 * 10^1+ 6 * 10^0
To extract the digits from the right to left use:
digit = number % 10
// update sum
// output digit
number = number / 10 // (use integer division)
To extract the digits from left to right, use a similar strategy but you will need to know the highest power represented. This can be done by using two loops, the first loop extracts digits from right to left, keeping track of the number of times the loop executes and accumulating the sum. The second loop can use the value of the counter to extract digits from left to right for the final display.
Make sure your program produces the output shown above.Include comments in the code which describe the program and include your name (and the name of your
C++
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images