Question

Reverse a Number using do while in C language

Updated on May 31, 2025 | By Learnzy Admin | 👁️ Views: 200 students

Solution
✔ Verified

This C program reverses the digits of a given number using a do-while loop. The process involves repeatedly extracting the last digit of the number and constructing the reversed number by appending this digit to a new number.

How it works:

  1. The program takes an integer input from the user.
  2. A do-while loop is used to reverse the number:
  3. In each iteration, the last digit of the number is extracted using the modulus operator (number % 10).
  4. This digit is added to the reversed number by multiplying the current reversed number by 10 (shifting its digits to the left) and then adding the extracted digit.
  5. The number is then divided by 10 (removing the last digit) using integer division (number / 10).
  6. The loop continues until the number becomes zero.
  7. After the loop finishes, the reversed number is printed.
#include <stdio.h>

int main() {
    int num, rev = 0;

    printf("Enter a number: ");
    scanf("%d", &num);

    do {
        rev = rev * 10 + num % 10;
        num = num / 10;
    } while (num != 0);

    printf("Reversed number = %d\n", rev);

    return 0;
}

Output - 

Enter a number: 123
Reversed number = 321
Was this solution helpful? 25
Click here to download practice questions on do while in C

More Questions on do while in C

Question 1

Sum of Digits of a Number using do while in C language


View solution
Question 2

Print Numbers from 1 to 10 using do while loop


View solution