

C++ Program to Reverse a Number
source link: https://www.geeksforgeeks.org/videos/c-program-to-reverse-a-number/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++ Program to Reverse a Number
- 1480 Views
- 24/05/2022
In this video, we will see a C++ program to reverse digits of a number by using the Iterative method and user-defined function. Basically, reversible numbers, or more specifically pairs of reversible numbers, are whole numbers in which the digits of one number are the reverse of the digits in another number.
For examples: 1) Input : num = 12345 Output: 54321
2) Input : num = 876 Output: 678
ALGORITHM:
Input: num
Step 1: Initialize rev_num = 0
Step 2: Loop while num > 0 Step 2.1: Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; Step 2.2 Divide num by 10
Step 3: Return rev_num Suppose there is a number 4562 and we want to reverse number of this: => num = 4562 => rev_num = 0 => rev_num = rev_num *10 + num%10 = 2 => num = num/10 = 456 => rev_num = rev_num *10 + num%10 = 20 + 6 = 26 => num = num/10 = 45 => rev_num = rev_num *10 + num%10 = 260 + 5 = 265 => num = num/10 = 4 => rev_num = rev_num *10 + num%10 = 2650 + 4 = 2654 => num = num/10 = 0
Program to Reverse Digits of a Number: https://www.geeksforgeeks.org/write-a-program-to-reverse-digits-of-a-number/
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK