In: Computer Science
With C language
A slot machine has three windows. A random fruit is picked for each window from cherry, apple, lemon, and orange. If all three windows match, the user wins 8 times the bet amount. If the first two windows only are cherries, the user wins 3 times the bet amount. If the first window is a cherry and the second window is not a cherry, the user wins the bet amount. Otherwise, the player loses their bet. Write a C program that will first allow the user to enter a bet amount. Next, the program should pick 3 random fruits for the 3 windows and print the 3 fruits picked. Lastly, the amount of money won or lost by the user should be displayed.
Here we map the fruit with the numbers.
We map cherry with number 0
We map apple with number 1
We map lemon with number 2
We map orange with number 3
We map cherry with number 0
and then generates random number from 0 to 3 for all the three windows.
Here is the C-program
#include<stdio.h>
#include<stdlib.h> //for rand() function
#include<time.h> //for time() function
int main()
{
        int betAmount, window1, window2, window3, wonAmount;
        //INPUT BET AMOUNT FROM USER
        printf("Enter the bet amount ");
        scanf("%d", &betAmount);
        //GENERATING RANDOM NUMBER FROM 0 TO 3 FOR THREE WINDOWS
        srand(time(0));
        window1 = rand() % 4;
        window2 = rand() % 4;
        window3 = rand() % 4;
        //PRINTING THE FRUIT PICKED FOR FIRST WINDOW
        if(window1 == 0)
                printf("\n\nFruit picked for window 1 - CHERRY\n");
        else if(window1 == 1)
                printf("\n\nFruit picked for window 1 - APPLE\n");
        else if(window1 == 2)
                printf("\n\nFruit picked for window 1 - LEMON\n");
        else if(window1 == 3)
                printf("\n\nFruit picked for window 1 - ORANGE\n");
        //PRINTING THE FRUIT PICKED FOR SECOND WINDOW
        if(window2 == 0)
                printf("Fruit picked for window 2 - CHERRY\n");
        else if(window2 == 1)
                printf("Fruit picked for window 2 - APPLE\n");
        else if(window2 == 2)
                printf("Fruit picked for window 2 - LEMON\n");
        else if(window2 == 3)
                printf("Fruit picked for window 2 - ORANGE\n");
        //PRINTING THE FRUIT PICKED FOR THIRD WINDOW
        if(window3 == 0)
                printf("Fruit picked for window 3 - CHERRY\n");
        else if(window3 == 1)
                printf("Fruit picked for window 3 - APPLE\n");
        else if(window3 == 2)
                printf("Fruit picked for window 3 - LEMON\n");
        else if(window3 == 3)
                printf("Fruit picked for window 3 - ORANGE\n");
        //ALL THREE WINDOWS ARE MATCHED
        if(window1 == window2 && window2 == window3)
        {
                wonAmount = 8*betAmount;
                printf("\n\nPlayer wins 8 times the bet amount\n");
                printf("Amount of money won by user = %d\n", wonAmount);
        }
        //FIRST TWO WINDOWS ARE CHERRY
        else if(window1 == 0 && window2 == 0)
        {
                wonAmount = 3*betAmount;
                printf("\n\nPlayer wins 3 times the bet amount\n");
                printf("Amount of money won by user = %d\n", wonAmount);
        }
        //FIRST WINDOW IS CHERRY AND SECOND IS NOT CHERRY
        else if(window1 == 0 && window2 != 0)
        {
                wonAmount = betAmount;
                printf("\n\nPlayer wins the bet amount\n");
                printf("Amount of money won by user = %d\n", wonAmount);
        }
        else
        {
                printf("\n\nPlayer loses the bet\n");
                printf("Amount of money loses by user = %d\n", betAmount);
        }
}
PLEASE UPVOTE IF YOU LIKED THE ANSWER