Coding Practice

Write a C program to enter a number and print its reverse

Enter a number and print its reverse
Sample Output
Enter a number and print its reverse.

Number: 12345

Reverse Number: 54321
Source Code
#include<stdio.h>

int main()
{
    int digit, number, revNumber = 0;

    printf("Enter a number and print its reverse.\n\nNumber: ");
    scanf("%d", &number);

    /** Reverse Number */
    while(number != 0)
    {
        digit = (number % 10);
        number = (number / 10);
        revNumber = (10 * revNumber) + digit;
    }
    /** Reverse number end  */
    printf("\nReverse Number: %d\n\n", revNumber);
    return 0;
}
Sample Output
Enter a number and print its reverse.

Number: 7634

Reverse Number: 4367

No comments:

Post a Comment

Change Theme
X