In C++, to reverse a number means swapping the digit at the last position with the digit at the first position and vice versa.
Write a C++ program to find the reverse of the number using for loop, while loop anf function.
1. The program takes an integer number.
2. Using a while loop, the digits of the number are reversed.
3. The reverse number is printed.
4. Exit.
There are several ways to reverse a number in C++. Let’s take a detailed look at all the approaches.
- Reverse a Number in C++ using While Loop
- Reverse a Number in C++ using For Loop
- Reverse a Number in C++ using Function
In this approach, we will divide the number by 10 and add the remainder to the sum.
Here is the source code of C++ Program to Reverse a Given Number Without Using Recursion. The program output is shown below.
/* * C++ Program to Reverse a Number using While Loop */ #include<iostream> using namespace std; int main () { int num, temp, rev = 0; cout << "Enter a number : "; cin >> num; temp = num; while (temp != 0) { r = temp % 10; rev = rev * 10 + r; temp /= 10; } cout << "\nThe reverse of " << num << " is : " << rev; return 0; }
1. The user is asked to enter a number and it is stored in the variable ‘num‘.
2. num is copied to a temporary variable ‘temp‘. The variable ‘rev‘ is initialized as 0.
3. Using a while loop, temp is divided by 10 and the remainder is stored in the variable ‘r‘.
4. rev is multiplied by 10 and r is added to it.
5. The loop continues till temp becomes 0.
6. The result is then printed which is the reverse number.
Time complexity: O(log N)
The time complexity of the provided code is O(log N) because the while loop iterates for the number of digits in ‘num’.
Space complexity: O(1)
The space complexity is O(1) as the memory usage remains constant, utilizing only a few integer variables (‘num’, ‘temp’, ‘rev’, and ‘r’).
Testcase 1: In this case, we are entering the number “123” as input.
Enter a number : 123 The reverse of 123 is : 321
Testcase 2: In this case, we are entering the number “58496” as input.
Enter a number : 58496 The reverse of 58496 is : 69485
Testcase 3: In this case, we are entering the number “21” as input.
Enter a number : 21 The reverse of 21 is : 12
In this approach, we will use a for loop to reverse a number.
Here is the source code of C++ Program to Reverse a Given Number Using For Loop. The program output is shown below.
/* * C++ Program to Reverse a Number using For Loop */ #include<iostream> using namespace std; int main() { int num, rev = 0; cout << "Enter a number : "; cin >> num; for (int temp = num; temp != 0; temp /= 10) { int r = temp % 10; rev = rev * 10 + r; } cout << "\nThe reverse of " << num << " is : " << rev; return 0; }
1. The user is asked to enter a number and it is stored in the variable ‘num‘.
2. The main function is defined, and two integer variables ‘num‘ and ‘rev‘ are declared, with ‘rev‘ initialized to 0.
3. A for loop is used to reverse the number: a temporary variable ‘temp‘ is initialized with the value of ‘num‘, and the loop continues as long as ‘temp‘ is not zero.
4. In each iteration of the loop, the last digit of ‘temp‘ is extracted and stored in the variable ‘r‘.
5. The reversed number is built by multiplying ‘rev‘ by 10 and adding the value of ‘r‘ to it.
6. After the loop completes, the reversed number ‘rev‘ is displayed as the output statement.
Time complexity: O(log N)
The time complexity of the code is O(log N), where N is the number of digits in the input number ‘num’. This is because the for loop runs for the number of digits in ‘num’, and in each iteration, ‘temp’ is divided by 10.
Space complexity: O(1)
The space complexity of the code is O(1) as it uses a constant amount of extra space to store the variables. The space required does not depend on the size of the input number.
Testcase 1: In this case, we are entering “435” as input to revere a number.
Enter a number : 435 The reverse of 435 is : 534
Testcase 2: In this case, we are entering “172398” as input to revere a number.
Enter a number : 172398 The reverse of 172398 is : 893271
In this approach, we will use a separate function to calculate the reverse of the number.
Here is the source code of C++ Program to Reverse a Given Number Using Function. The program output is shown below.
/* * C++ Program to Reverse a Number using Function */ #include<iostream> using namespace std; int reverseNumber(int num) { int rev = 0; while (num != 0) { int r = num % 10; rev = rev * 10 + r; num /= 10; } return rev; } int main() { int num; cout << "Enter a number : "; cin >> num; int reversedNum = reverseNumber(num); cout << "\nThe reverse of " << num << " is : " << reversedNum; return 0; }
1. A function reverseNumber is defined to reverse the given number.
2. Inside the reverseNumber function, a variable rev is initialized as 0 to store the reversed number.
3. A while loop is used to reverse the number digit by digit.
4. The last digit of the number is extracted and added to the rev variable.
5. After the loop completes, the reversed number rev is returned.
6. In the main function, the user is prompted to enter a number.
7. The reverseNumber function is called with the entered number.
8. Finally, the original number and the reversed number are displayed as the output.
Time complexity: O(log10(num))
The time complexity of the reverseNumber function is O(log10(num)), where ‘num’ is the input number. This is because the while loop runs for the number of digits in the input number.
Space complexity: O(1)
The space complexity is O(1) as the space used remains constant regardless of the size of the input, since only a few integer variables are used in the function.
In this case, we are entering “5689243” as input to revere a number.
Enter a number : 5689243 The reverse of 5689243 is : 3429865
To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.
- Check Programming Books
- Practice Programming MCQs
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C++ Books