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:
- If the user’s pick matches the lottery number in the exact order, the prize at stake is P8,000.
- If all digits in the user’s pick match all digits in the lottery number, the prize at stake is P5,000.
- 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!");
}
CodePudding user response:
It was simpler just to break it into ones and tens digits rather than try and put it back together. You'll need to do that for checking if their is any matches at all so why not use it for the entire thing.
#include<stdio.h>
#include<stdlib.h>
int main(){
srand(time(NULL));
int guess=0;
while(1){
int lotto = rand()