vendredi 24 décembre 2021

How to determine if one digit in the user's lottery pick matches a digit in the lottery number?

Problem: Write a c program that will allow the user to play Lottery for as long as he/she wanted to. The program randomly generates a lottery of a two-digit number, prompts the user to enter a pick of two-digit number, and determines whether the user wins according to the following rules:

  1. If the user’s pick matches the lottery number in the exact order, the prize at stake is P8,000.
  2. If all digits in the user’s pick match all digits in the lottery number, the prize at stake is P5,000.
  3. If one digit in the user’s pick matches a digit in the lottery number, the prize at stake is P2,000.

Required knowledge: Generating random numbers, comparing digits, using Boolean operators, selection and looping structures.

I'm stuck on the third requirement — can you help?

This is my code so far...

#include <stdio.h>
#include <stdlib.h>
int main()
{
    srand((unsigned)time(0));
    int guess, guess2, lottoNum2, lottoNum3, lottoNum4, ones;
    int lottoNum = rand() % 100;

    printf("The lottery number is %d\n", lottoNum);

    printf("Enter your lottery pick (two digits): ");
    scanf("%d", &guess);

    if (guess == lottoNum)
        printf("\nExact match: you win P8,000");

    while (lottoNum != 0)
    {
        ones = lottoNum % 10;
        lottoNum2 = lottoNum2 * 10 + ones;
        lottoNum /= 10;
    }
    if (guess == lottoNum2)
        printf("\nMatch all digits: you win P5000\n");

    while (lottoNum != 0)
    {
       

    if (guess != lottoNum)
    printf("\nSorry, there is no digit match!");
}



Aucun commentaire:

Enregistrer un commentaire