Sum of the Digits and Reverse of a Number
This program calculates the sum of the digits and the reverse of a given number. Let's explore the code and understand its workings.
Code Breakdown
Algorithm
- Start
- Input Number: Read the value of the number.
- Initialize Variables: Initialize
sum
andreverse
to 0. - Calculate Sum and Reverse:
- Extract each digit using modulo operation.
- Add the digit to
sum
. - Construct the reverse by shifting and adding the digit.
- Display Results:
- Print the sum of the digits and the reversed number.
- End
Code Explanation
Example Flowchart
Start
|
V
Input number
|
V
Initialize sum, reverse to 0
|
V
Is number not equal to 0?
/ \
/ \
Yes No
| |
V V
+--------------------------------------+ End
| Get last digit (remainder) |
| sum = sum + remainder |
| reverse = reverse * 10 + remainder |
| number = number / 10 |
+--------------------------------------+
|
V
Display sum and reverse
|
V
End