Search

Wednesday, February 27, 2019

C Program to Reverse a Number

Posted By Manisha Gupta
Example to reverse an integer entered by the user. This problem is solved using while loop in this example.
Reverse and integer

To understand this example, you should have the knowledge of following C programming topics:

Example: Reverse an Integer

#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;

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

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

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

    return 0;
}
Output
Enter an integer: 12345
Reversed Number = 54321
This program takes an integer input from the user. Then the while loop is used until n != 0is false.

Follow on Facebook

ManishaTech . 2017 Copyright. All rights reserved. Designed by Blogger Template | Manisha Gupta