# Reversing the digits

• March 9th, 2013, 12:16 AM
Tanushreeagr
Reversing the digits
//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>
void main()
{

int num, i = 0;

char rev[5];

printf("Enter any 5 digit number\n");

scanf("%d", &num);
i = 0;
while(num)
{

rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????

i++;

num = num/10;
}

printf("%s", rev);

}

What's wrong in the above program.

WHILE DISPLAYING i am not getting correct output. whats wrong in rev[] array.

• March 9th, 2013, 01:34 AM
nuzzle
Re: Reversing the digits
Quote:

Originally Posted by Tanushreeagr
rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????

You're extracting the rightmost digit from the integer held in num. It will be an integer between 0 and 9. What you want to do now is to convert it to a char between '0' and '9' before assigning it to the array. Since those characters lie next to each other in the ASCII encoding this will work (although it isn't that elegant)

rev[i] = '0' + num % 10;

http://www.asciitable.com/

The ASCII char '0' is represented by 48 which is added to the digit between 0 and 9 turning it into an ASCII char between '0' and '9'.
• March 9th, 2013, 05:39 AM
2kaud
Re: Reversing the digits
If you just want to reverse an integer, see this thread

http://forums.codeguru.com/showthrea...ighlight=emirp

Code:

```//Reverse an integer int ReverseInteger(int no) { int rnum = 0;         while (no) {                 rnum = rnum * 10 + (no % 10);                 no /= 10;         }         return rnum; }```
• March 9th, 2013, 05:41 AM
2kaud
Re: Reversing the digits
PS. When you post code, please use CODE tags for readability. Go Advanced, select code then click '#'
• March 9th, 2013, 08:49 AM
Eri523
Re: Reversing the digits
Quote:

Originally Posted by 2kaud
If you just want to reverse an integer, see this thread

http://forums.codeguru.com/showthrea...ighlight=emirp

Code:

```//Reverse an integer int ReverseInteger(int no) { int rnum = 0;         while (no) {                 rnum = rnum * 10 + (no % 10);                 no /= 10;         }         return rnum; }```

I considered that approach as well, but whether it actually is valid depends on the exact requirements set up by the assignment: It behaves different from the char[]-based approach when the subject number ends with one or more zeroes. These would become insignificant in processing and thus omitted in the output. That was irrelevant in the emirp context since there are no primes ending with 0 anyway.
• March 9th, 2013, 11:21 PM
Coder Dave
Re: Reversing the digits
Here are some fixes to your code. Study it and my comments. :)

Quote:

Originally Posted by Tanushreeagr
Code:

```//PROGRAM TO REVERSE THE 5 DIGIT NUMBER #include <stdio.h> void main() {                 int num,  i;  // Initializing i here is not needed since it is done later.         char rev[6]; // You need to allocate space for the null character to terminate the string.                     // This is required by the printf() function.         printf("Enter any 5 digit number\n");         scanf("%d", &num);         i  =  0;         while(num & (i < 5)) // It is very important that you check that the number is not too long.                             // Without this check data could be written outside of the rev array causing memory corruption.         {                 rev[i]  =  '0' + (num % 10);                 i++;                 num  =  num/10;         }         rev[i] = '\0';  // Make sure you terminate the string with a null character.         printf("%s\n", rev);  // This prints a new line after the result. }```

• March 10th, 2013, 07:08 AM
2kaud
Re: Reversing the digits

To make the revised program work, the while condition test should be the logical and not the bitwise and.

Code:

`while(num && (i < 5))`
Also it would be good practice to declare and initialise i as close as possible to where it is first used (or at least initialise it where it is defined).

main() should return an integer by the ANSI standard.

Code:

```//PROGRAM TO REVERSE THE 5 DIGIT NUMBER #include <stdio.h> int main() { char        rev[6];                // You should allocate space for a null character to terminate the string. int        num;         printf("Enter any 5 digit number: ");         scanf("%d", &num); int        i = 0;         while (num && (i < 5))        // It is very important that you check that the number is not too long.                                 // Without this check data could be written outside of the rev array causing memory corruption.         {                 rev[i++]  =  '0' + (num % 10);                 num  /=  10;         }         rev[i] = '\0';                // Make sure you terminate the string with a null character.         printf("%s\n", rev);        // This prints a new line after the result.         return (0); }```
• March 11th, 2013, 11:07 PM
Coder Dave
Re: Reversing the digits
Quote:

Originally Posted by 2kaud
To make the revised program work, the while condition test should be the logical and not the bitwise and.

Doh! :blush: