How to reverse a number in C?

Sep 1st, 2023

How to reverse a number in C?


To reverse a number in C programming we will be using do…while loops.

How do you reverse a number in C?

#include <stdio.h>

int main() {

  int n, reverse = 0, remainder;

  printf("Enter an number: ");
  scanf("%d", &n);

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

  printf("Reversed number is %d", reverse);

  return 0;


Here we are using a while loop to reverse the integer:

  • while (n != 0) This line suggests that the loop will continue as long as the value of n is not equal to 0.
  • Inside the loop:
    • remainder = n % 10 this line calculates the last digit of the integer n.
    • reverse = reverse * 10 + remainder this line updates the reverse variable by appending the remainder to the end of the current reverse value.
    • n /= 10 this line removes the last digit from n by dividing it by 10. This makes n ready for the next iteration of the loop.
  • All these will return you the reversed value of the integer in C programming language using while loops.

In conclusion, using do…while loops is the most effective method to reverse a number in C programming language.

Thanks for reading! Have a great day!