Write a C++ program to reverse a number

Write a C++ Program to Reverse a Number

Reversing a number is a common and important program for beginners in C++. It helps students understand loops, arithmetic operations, and number manipulation. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to reverse a given number.

This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.

1. Introduction of Reversing a Number

Reversing a number means changing the order of its digits from left to right.

For example:

Original number: 1234
Reversed number: 4321

Original number: 567
Reversed number: 765

This concept is commonly used in problems related to palindromes and number processing.

2. Logic of the Program

To reverse a number in C++, follow these steps:

  1. Start the program.
  2. Declare variables for the number, remainder, and reversed number.
  3. Take input from the user.
  4. Use a loop until the number becomes 0.
  5. Extract the last digit using % 10.
  6. Add the digit to the reversed number.
  7. Remove the last digit from the original number using / 10.
  8. Display the reversed number.

Example:
If the user enters 123

Step 1: remainder = 123 % 10 = 3
Reversed = 0 × 10 + 3 = 3
Number becomes 123 / 10 = 12

Step 2: remainder = 12 % 10 = 2
Reversed = 3 × 10 + 2 = 32
Number becomes 12 / 10 = 1

Step 3: remainder = 1 % 10 = 1
Reversed = 32 × 10 + 1 = 321
Number becomes 0

Final reversed number = 321

3. C++ Source Code (Easy Student Friendly Code)

#include <iostream>
using namespace std;

int main()
{
    int num, remainder, reverse = 0;

    cout << "Enter a number: ";
    cin >> num;

    while(num != 0)
    {
        remainder = num % 10;
        reverse = reverse * 10 + remainder;
        num = num / 10;
    }

    cout << "Reversed number is: " << reverse;

    return 0;
}

Explanation:
num % 10 extracts the last digit.
reverse = reverse * 10 + remainder builds the reversed number.
num = num / 10 removes the last digit.
The while loop runs until the number becomes 0.

4. Sample Output

Example 1:
Enter a number: 1234
Reversed number is: 4321

Example 2:
Enter a number: 908
Reversed number is: 809

5. Viva Questions with Answers

Q1: What does % 10 do in this program?
Answer: It extracts the last digit of the number.

Q2: Why do we multiply the reversed number by 10?
Answer: To shift digits to the left before adding the new digit.

Q3: Which loop is used in this program?
Answer: A while loop is used.

Q4: What happens when the number becomes 0?
Answer: The loop stops executing.

Q5: Can this program work for negative numbers?
Answer: With slight modification, it can also handle negative numbers.

This program helps beginners understand loops and number manipulation in C++. Practice more basic programs on cslearning.org to strengthen your programming skills.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top