Thursday, April 6, 2023

Reverse a Number - C++ Program

Reverse a Number - C++ Program Write a C++ Program to Reverse a Number.

#include <iostream>
using namespace std;

int main() {
    int num, reversedNum = 0, remainder;
    cout << "Enter an integer: ";
    cin >> num;

    while (num != 0) {
        remainder = num % 10; // extract the last digit of the number
        reversedNum = reversedNum * 10 + remainder;// append the digit to the reversed number
        num /= 10; // remove the last digit from the original number
    }

    cout << "The reversed number is: " << reversedNum << endl;
    return 0;
}

//===========
OUTPUT:
Enter an integer: 12345
The reversed number is: 54321

Enter an integer: 987654321
The reversed number is: 123456789

Enter an integer: 100
The reversed number is: 1

Explaining : The program starts by declaring three integer variables: num, reversedNum, and remainder. num is the original number input by the user, reversedNum is the number that will be produced by reversing the digits of num, and remainder is used to extract the last digit of num in each iteration of the while loop.

The program then prompts the user to enter an integer using the cout and cin statements.

The while loop starts by checking whether num is not equal to 0. If num is not 0, the remainder variable is set to the last digit of num using the modulo operator %. The reversedNum variable is then updated by appending the remainder digit to the end of the current value of reversedNum. This is achieved by multiplying reversedNum by 10 and adding the remainder.

Finally, the num variable is updated by dividing it by 10, which effectively removes the last digit of num. The loop then repeats until num becomes 0.

Once the while loop completes, the program prints the reversedNum variable using the cout statement. Notes:

  • * The program is written in C++, which is a high-level programming language commonly used for developing applications, games, and system software.
  • * The program uses the iostream library, which provides input and output capabilities. This allows the program to interact with the user by prompting for input and displaying output.
  • * The program declares three integer variables: num, reversedNum, and remainder. Variables are used to store data in memory and can be manipulated using various operations.
  • * The program uses the cout statement to display the prompt message "Enter an integer: " to the user. The cin statement is then used to read the user's input and store it in the num variable.
  • * The program uses a while loop to extract each digit of num and append it to reversedNum. The loop uses the modulo operator % to extract the last digit of num, and then divides num by 10 to remove the last digit. The loop continues until num becomes 0.
  • * The program updates the reversedNum variable by multiplying it by 10 and adding the remainder digit in each iteration of the while loop.
  • * The program uses the cout statement to display the reversed number to the user.
  • * The program uses the return statement to indicate that the program has successfully completed execution.
  • * It is important to note that the program assumes that the user will input a valid integer, and does not handle cases where the user enters non-integer values or values that are too large to be stored in an int variable.
  • * The program demonstrates basic programming concepts such as variables, data types, input/output, loops, and basic arithmetic operations.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...