#include <stdio.h>

Reverse Number In C Program
int main()
{
int n, reverse = 0;

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

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

   printf("Reverse of entered number is = %d\n", reverse);

   return 0;
}

What is your question, your code seems to do what is intended?

please explain me about step 6 and 7.

I hope the following explanation is helpful. Unfortunately, I can't find any preview option, so I'm not sure how the post will appear

I've iterated line by line thru the while loop in your posted coded as indicated below.

Assume n (input) is 1234

Since reverse is initially zero

 reverse = reverse * 10;

will result in reverse still being equal to zero

Since n is equal to 1234, using the modulus operator will produce 123.4 and the modulus operator will return 4 which is entered into the reverse variable
Remember the reverse is initially set to zero. So, zero plus 4 equals 4

reverse = reverse + n%10;

The moudulus operator (percent sign) computes the remainder that results from performing integer division.

At this point reverse now contains 4

We now divide n by 10. Integer division will only return the whole excluding the remainder. So, 1234/10 equals 123. n now contains 123.

n = n/10;

We now loop back up to...

reverse = reverse * 10;

Since reverse contains 4, multiplying it by 10 will result in reverse now containing 40

Now we again use the modulus operator on n. Remember that n now contains 123. Using the modulus operator will return a remainder of 3 (123/10 gives 12 with a remainder of 3). To reiterate, the modulus operator will ONLY return the remainder value of 3. Thus, the following statement is adding 3 to the value of reverse which is 40, making reverse now equal to 43

reverse = reverse + n%10;

The next statement divides n which contains 123 by 10 resulting in n containing the whole number 12. The remainder 3 is discarded,

 n = n/10;

We again loop back up to..

reverse = reverse * 10;

reverse contains 43 prior to executing the above statement. Thus, multiplying reverse by 10 will make reverse equal 430

Keep in mind prior to executing the next statement that n is equal to 12 and reverse is equal to 430. So, using the modulus operator on n will result in it generating a remainder of 2 which is added to reverse, making reverse equal to 432

reverse = reverse + n%10;

The current value of n before executing the next statement is 12. After executing the next statement, n will contain 1 size integer division will return whole numbers exclusive of remainders (12/10 = 1)

n = n/10;

Finally, we loop back one last time to

reverse = reverse * 10;

reverse contains 432 prior to executing the previous statement. So, when we multiple reverse by 10, reverse will now contain 4320

At this point n contains 1. using the modulus operator will generate a remainder of 1 which is added to reverse, making reverse equal to 4321

 reverse = reverse + n%10;

Executing the next statement returns an integer value of ZERO which will cause the while loop to break since n is equal to ZERO

n = n/10;

The end result is having reverse contain 4321.

Cool but probably slow to execute due to all the math. Seems like handling it as an array of characters and using xor swap would be faster.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.