Reverse a Number in C++ Last Updated : 21 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 Comment More infoAdvertise with us Next Article Reverse a Number in C++ kartik Follow Improve Article Tags : C++ Programs C++ C++ Basic Programs Practice Tags : CPP Similar Reads How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Reverse a Stack in C++? In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can re 2 min read How to Reverse a Deque in C++? In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Rever 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read Reverse Middle X Characters Given a string str and an integer X. The task is to reverse the middle X characters of the given string and then print the modified string. Note that len(str) - X is always even.Examples: Input: str = "geeksforgeeks", X = 3 Output: geeksrofgeeks Middle three character are "geeksforgeeks" Hence the r 9 min read Program to print numbers from N to 1 in reverse order Given a number N, the task is to print the numbers from N to 1.Examples: Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 7 Output: 7 6 5 4 3 2 1 Approach 1: Run a loop from N to 1 and print the value of N for each iteration. Decrement the value of N by 1 after each iteration.Below is the implem 5 min read Increment a number without using ++ or + The task is to Increment a number without using ++ and + operators.Examples: Input : 3 Output : 4 Input : 9 Output : 10 The idea is based on the fact that the negative numbers are stored using 2's complement form. 2's complement form is obtained by inverting bits and then adding one. So if we invert 6 min read Program to delete Nth digit of a Number Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.Examples: Input: num = 1234, n = 3 Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134Input: num = 4516312, n = 2 Output: num_after_deleting_from_starti 15+ min read Like