A Program to Reverse an Integer

In this example, we are giving a program to reverse an integer given by user.


Program to reverse an integer:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n, reversedNumber = 0, remainder;
  5. printf("Enter an integer: ");
  6. scanf("%d", &n);
  7. while(n != 0)
  8. {
  9. remainder = n%10;
  10. reversedNumber = reversedNumber*10 + remainder;
  11. n /= 10;
  12. }
  13. printf("Reversed Number = %d", reversedNumber);
  14. return 0;
  15. }

Output:

Enter an integer: 1234
Reversed Number = 4321