Reversing The Number Of The Digit
ALGORITHM:
- Get the input “n” from the user.
- Initialize a variable “rev_number” to 0.
- Repeat the following until “n” is greater than 0:
- a) Find the remainder of “n” when divided by 10, and store it in a variable called “remainder”.
- b) Multiply the current value of “rev_number” by 10, and add the value of “remainder” to it.
- c) Divide “n” by 10 and store the result back in “n”.
4.Print out the value of “rev_number”.
DETAILED EXPLANATION:
1.This algorithm takes an input number “n” and reverses its digits.
2.It does this by repeatedly finding the last digit of “n” using the modulo operator (%), adding it to a variable “rev_number” after shifting the current value of “rev_number” one place to the left (i.e., multiplying it by 10), and then removing the last digit of “n” by dividing it by 10.
3. When “n” becomes 0, the reversed number is stored in “rev_number”, which is then printed out.
FLOWCHART: