Introduction – Reverse a Number
Reverse a number involves flipping its digits such that the digits appear in the opposite order. For Example, reversing the number “12345” results in “54321”. Implementing a program to reverse a number in C++ requires understanding a basic arithmetic operations and loops.
Project Codes
Here is the Project Codes,
#include <iostream> int reverseNumber(int num) { int reversedNumber = 0; while (num != 0) { int lastDigit = num % 10; reversedNumber = reversedNumber * 10 + lastDigit; num /= 10; } return reversedNumber; } int main() { int number; std::cout << "Enter a number to reverse: "; std::cin >> number; int reversed = reverseNumber(number); std::cout << "Reversed number: " << reversed << std::endl; return 0; }
Output
Input: If the user enters 12345.
Output: The program calculates and outputs 54321.