C program to reverse the number entered by the user, then displays the reversed number on the screen.
For example, if the user enters 678 as input, 876 is displayed as output. We use the mod(%) operator in the program to get the digits of an integer.
C Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> int main() { int nreversed = 0, nbr; printf("Enter an integer number: "); scanf("%d", &nbr); while (nbr != 0) { nreversed = nreversed * 10; nreversed = nreversed + nbr%10; nbr = nbr/10; } printf("The number in reversed is = %d\n", nreversed); return 0; } |
Output:
1 2 3 4 | Enter an integer number: 12345 The number in reversed is = 54321 |