Q:

C program to reverse a number

0

C program to reverse a number

 

C program to reverse a number and to print it on the screen. For example, if the input is 123, the output will be 321. In the program, we use the modulus operator (%) to obtain digits of the number. To invert the number write its digits from right to left.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

  printf("Enter a number to reverse\n");
  scanf("%d", &n);

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

  printf("Reverse of the number = %d\n", r);

  return 0;
}

output:

Enter a number to reverse

12345

Reverse of the number = 54321

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now