Genarate a code for reversing the number (while loop)

0
Output


Explanation of the Reverse Number Program

This program is designed to reverse a number entered by the user. It repeatedly extracts the last digit of the number and builds the reversed number step-by-step using a while loop.

The program uses the modulus operator (%) to separate digits and integer division to remove the last digit. Each digit is appended to the reversed number using the expression rev = rev * 10 + rem. However, the variable rev is not initialized, which can lead to incorrect output.

What the Program Does

  • Asks the user to enter any positive number.
  • Uses % 10 to extract the last digit of the number.
  • Uses / 10 to remove the extracted digit.
  • Builds a reversed number digit-by-digit.
  • Repeats this process until the number becomes 0.
  • Prints the reversed number as output.
  • Uses getch() to pause the screen in Turbo C.

Example Input

Enter a number: 1 2 3 4

Example Output

The reverse is: 4 3 2 1

Summary: This program reverses a number by repeatedly extracting digits using the modulus operator and rebuilding them in reverse order. To work correctly, the variable rev must be initialized to 0.

Tags

Post a Comment

0Comments

Post a Comment (0)