# Thread: Reversing Digits

1. ## Reversing Digits

The goal of the code is to have the user enter a two digit number in a lottery, they win a different prize if they

-match the number exactly
-match the number in reverse order
-match at least one digit

I'm not sure I'm reversing the digits correctly, I know that they will only give me an integer with two digits but still having problems putting it all together. Is 25/10 = 2 if 25 is an int?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int lotto, i, revten, revone, rev, revteni, revonei;

printf("LOTTERY\n");
printf("Enter your two digit number for a chance to win: ");
scanf("%d", &lotto);
printf("\n");

revten = ((lotto%10) * 10);
revone = lotto/10;
rev = revten + revone;

srand(time(NULL));
i = rand()%(100 - 10 + 1) + 10;
printf ("Today's winning number is: %d. \n", i);

revteni = (i%10) * 10;
revonei = i/10;

if (lotto == i)
printf("Congratulations you have just won \$10,000!");
else if (rev == i)
printf("Congratulations you have just won \$3,000!");
else if ((revten || revone) == (revteni || revonei))
printf("Congratulations you have just won \$1,000!");
else printf("Sorry, your luck ran out this time.  Play again some other time!");

return 0;
}```

2. The reversing looks ok to me. But this line doesn't:
Code:
`else if ((revten || revone) == (revteni || revonei))`
Try this:
Code:
`else if ( (revten == revteni)||(revone == revonei) )`
I think this is where the problem lies.

3. Here is the updated code. It seems like it is reversing the numbers correctly but I still keep winning \$1000 each time, even when I shouldn't. Any ideas why?

Code:
```/*

My name is Jack Trocinski

*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int lotto, i, revten, revtens, revone, rev, revteni, revonei;

printf("LOTTERY\n");
printf("Enter your two digit number for a chance to win: ");
scanf("%d", &lotto);
printf("\n");

revten = lotto%10;
revtens = revten * 10;
revone = lotto/10;
rev = revtens + revone;

srand(time(NULL));
i = rand()%(100 - 10 + 1) + 10;
printf ("Today's winning number is: %d. \n", i);

revteni = (i%10);
revonei = i/10;

if (lotto == i)
printf("Congratulations you have just won \$10,000!");
else if (rev == i)
printf("Congratulations you have just won \$3,000!");
else if ((revten || revone) == (revteni || revonei))
printf("Congratulations you have just won \$1,000!");
else printf("Sorry, your luck ran out this time.  Play again some other time!");

return 0;
}```

4. Posted that last post without refreshing the page, didn't see your post there Swarvy.

Originally Posted by Swarvy
The reversing looks ok to me. But this line doesn't:
Code:
`else if ((revten || revone) == (revteni || revonei))`
Try this:
Code:
`else if ( (revten == revteni)||(revone == revonei) )`
I think this is where the problem lies.

Hmm the instructions say "If one digit in the user input matches a digit in the lottery" so I'm wondering if the way you wrote it is right, since that would only mean that the ones digit matches the random generated ones digit and same for the tens digit. But what if random generated is 25, you enter 59... I think you should still win \$1000 since there is a 5 digit in both.

5. Got it to work using some more if else statements rather than OR. Not sure why the OR wasn't working though. Here is the if else statements that are working fine. If any digit entered matches the random generated one you will at least win \$1000.

Code:
```if (lotto == i)
printf("Congratulations you have just won \$10,000!");
else if (rev == i)
printf("Congratulations you have just won \$3,000!");
else if (revten == revteni)
printf("Congratulations you have just won \$1,000!");
else if (revten == revonei)
printf("Congratulations you have just won \$1,000!");
else if (revone == revteni)
printf("Congratulations you have just won \$1,000!");
else if (revone == revonei)
printf("Congratulations you have just won \$1,000!");```

Popular pages Recent additions