Reverse a Number in C++ Last Updated : 23 Jul, 2025 Suggest changes Share 3 Likes Like Report In this article, we will learn to write a C++ program to reverse a number. Reversing the digits of a number means changing the order of the digits of the number so that the last digit becomes the first digit, the second last digit becomes the second digit, and so on. The number upon reversing read the same as reading the original number backward. For example, if the number num is 12548, the reverse of the number num is 84521. Algorithm to Reverse Digits of a Number Let us assume the number to be reversed is num and the reversed number will be stored in rev_num. Initialize rev_num = 0.Run a loop till num > 0.The rightmost digit of num can be obtained by performing modulo by 10 (num % 10).Now, the rightmost digit obtained is added to the reversed number by shifting its digits one position to the left.rev_num = rev_num*10 + num%10;Remove the last digit from num by dividing it by 10 (num = num / 10).After the loop, return rev_num which holds the reverse of digits of the number num.C++ Program to Reverse a Number C++ // C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Iterative function to // reverse digits of num int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } // Driver code int main() { int num = 4562; cout << "Reverse of num is " << reverseDigits(num); getchar(); return 0; } OutputReverse of num is 2654Complexity AnalysisTime Complexity: O(log(n)), where n is the input number. Auxiliary Space: O(1) Flow of Program To Reverse a Number The below image illustrates the flow of the program to reverse the digits of a number. Related ArticlesWrite a program to reverse digits of a number K kartik Follow 3 Article Tags : C++ Programs C++ C++ Basic Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like