Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Reversing Digits function using C programming

Write a function that takes an integer value and returns the number with
its digits reversed. For example, given the number 7631, the function should return 1367.

Answer:



#include <stdio.h>

int reverseDigits( int n );

int main()
{ 
   int number; // input number 

   printf( "%s", "Enter a number between 1 and 9999: " );
   scanf( "%d", &number );

   // find number with digits reversed 
   printf( "The number with its digits reversed is: %d\n", 
          reverseDigits( number ) );
} // end main 

// reverseDigits returns number obtained by 
// reversing digits of n 
int reverseDigits( int n )
{ 
   int reverse = 0; // reversed number 
   int part = 0; // temp variable for individual digits 

   while ( n > 1 ) {
      part = n % 10;
      reverse = reverse * 10 + part;
      n /= 10;
   }

   return reverse; // return reversed number 
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!