Thread: Reversing Digits

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question 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. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    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.
    Last edited by Swarvy; 10-07-2010 at 09:58 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    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. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Posted that last post without refreshing the page, didn't see your post there Swarvy.

    Quote Originally Posted by Swarvy View Post
    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. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    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 subscribe to a feed

Similar Threads

  1. Reversing digits in C
    By Digital Thought in forum C Programming
    Replies: 22
    Last Post: 10-15-2010, 06:34 AM
  2. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  3. Reversing Digits Project (Help Please)
    By fenixataris182 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2005, 01:08 PM
  4. Reversing the order of the digits?
    By Mak in forum C Programming
    Replies: 15
    Last Post: 10-09-2003, 09:17 PM
  5. reversing digits
    By lizardking3 in forum C++ Programming
    Replies: 15
    Last Post: 03-28-2003, 12:30 PM