• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
May 24, 2022 |49.9K Views
C++ Program to Reverse a Number
  Share  4 Likes
Description
Discussion

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/

Read More