c
  1. c-program-to-reverse-a-number

Program to Reverse a Number - (C Programs)

Example

#include<stdio.h>

int main(){

    int number, reversedNumber = 0, remainder;

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

    while(number != 0){

        remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }

    printf("Reversed Number = %d", reversedNumber);

    return 0;
}

Output

Enter an integer: 12345
Reversed Number = 54321

Explanation

This program takes an integer input from the user and then reverses the number by using the modulus operator and while loop. It extracts the last digit of the number using the modulus operator and then multiplies the previously extracted digits by 10 and adds the new digit to it. This process is repeated until there are no remaining digits in the number. Finally, the reversed number is printed.

Use

This program can be used whenever there is a need to reverse a number in a C program.

Summary

This program takes an integer input and reverses the number using modulus, while loop and multiplication. The reversed number is then printed as output.

Published on: