write the program in C language. and please follow the instructions given on the
ID: 3850500 • Letter: W
Question
write the program in C language.
and please follow the instructions given on the question.
and could you please also add and sample output just to make sure it's working properly.
thank you
Explanation / Answer
Here is the code and output for the question. Please don't forget to rate the answer if it helped. Thank you very much.
roulett.c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void showInstructions();
double getBetAmount();
void makeBet(int *, int *);
int spinWheel();
double figureWinnings(int, int, double, int);
int main()
{
double bet_amount = 0, current_winnings;
int main_choice, sub_choice;
int number;
char ch;
/*
main_choice will be
1 - for bet on specific number,
2 - odd/even ,
3 - dozen
sub_choice will be
-the specific number when main_choice selected is 1
-1 for option odd and 2 for option even , when main_choice selected is 2
-1 for first 1-12 dozen, 2 for 13-24 dozen , 3 for 25-36 dozen, when main_choice is 3 */
srand(time(NULL));
showInstructions();
while(1)
{
makeBet(&main_choice, &sub_choice);
bet_amount = getBetAmount();
number = spinWheel();
printf(" The number on the Roulette wheel is : %d", number);
current_winnings = figureWinnings(main_choice, sub_choice, bet_amount, number);
if(current_winnings > 0)
printf(" You win $%.2lf", current_winnings);
else
printf(" You lost the bet !");
printf(" Do you wish to continue? y/n: ");
getchar(); //flush the newline
scanf("%c", &ch);
if(ch!='y' && ch != 'Y')
break;
}
printf(" ********* Thank you for playing Roulette Game ********** ");
}
void showInstructions()
{
printf(" ****** Welcome to Roulette Game ******");
printf(" Rules of the game - ");
printf(" 1. You can bet on a specific number. In this case, payout is 36 times the bet amount.");
printf(" 2. You can bet on odd or even number. In this case, payout is 2 times the bet amount.");
printf(" 3. You can bet on a dozen, first 1-12, second 13-24, third 25 - 36. In this case, ");
printf(" payout is 3 times the bet amount.");
printf(" 4. The number zero does not count for odd or even or dozen, but can count as a number bet");
printf(" Press <enter> to continue ... ");
getchar();
}
void makeBet(int *main_choice, int *sub_choice)
{
int m, s;
while(1)
{
printf(" 1. Bet on a number");
printf(" 2. Bet on odd / even ");
printf(" 3. Bet on a dozen");
printf(" Enter your choice: ");
scanf("%d", &m);
if(m < 1 || m > 3)
printf(" Invalid menu choice !");
else
break;
}
switch(m)
{
case 1:
while(1)
{
printf(" ------Betting on a number -------");
printf(" Enter the number you want to bet on (0 - 36): ");
scanf("%d", &s);
if(s < 0 || s > 36)
printf(" Invalid number entered ! Try again...");
else
break;
}
break;
case 2:
while(1)
{
printf(" ------Betting on odd / even -------");
printf(" 1. Bet on odd");
printf(" 2. Bet on even");
printf(" Enter your choice (1-2): ");
scanf("%d", &s);
if(s < 1 || s > 2)
printf(" Invalid choice entered ! Try again...");
else
break;
}
break;
case 3:
while(1)
{
printf(" ------Betting on a dozen -------");
printf(" 1. Bet on dozen 1 - 12");
printf(" 2. Bet on dozen 13 - 24");
printf(" 2. Bet on dozen 25 - 36");
printf(" Enter your choice (1-3): ");
scanf("%d", &s);
if(s < 1 || s > 3)
printf(" Invalid choice entered ! Try again...");
else
break;
}
break;
}
*main_choice = m;
*sub_choice = s;
}
double getBetAmount()
{
double amt;
while(1)
{
printf(" Enter bet amount: ");
scanf("%lf", &amt);
if(amt < 1)
printf(" Invalid bet amount ! Try again...");
else
break;
}
return amt;
}
int spinWheel()
{
return ( rand() % 37); //return a number from 0-36
}
double figureWinnings(int main_choice, int sub_choice, double bet_amount, int number)
{
double winnings = 0;
if(main_choice == 1) //bet on a number
{
if(number == sub_choice)
winnings = 36 * bet_amount;
}
else if(main_choice == 2) //bet on odd/even
{
if((sub_choice == 1 && number % 2 == 1) || (sub_choice == 2 && number % 2 == 0) )
winnings = 2 * bet_amount;
}
else //bet on dozen
{
if((sub_choice == 1 && number >=1 && number <= 12) ||
(sub_choice == 2 && number >=13 && number <= 24) ||
(sub_choice == 3 && number >=25 && number <= 36) )
winnings = 3 * bet_amount;
}
return winnings;
}
output
****** Welcome to Roulette Game ******
Rules of the game -
1. You can bet on a specific number. In this case, payout is 36 times the bet amount.
2. You can bet on odd or even number. In this case, payout is 2 times the bet amount.
3. You can bet on a dozen, first 1-12, second 13-24, third 25 - 36. In this case,
payout is 3 times the bet amount.
4. The number zero does not count for odd or even or dozen, but can count as a number bet
Press <enter> to continue ...
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 1
------Betting on a number -------
Enter the number you want to bet on (0 - 36): 19
Enter bet amount: 10
The number on the Roulette wheel is : 7
You lost the bet !
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 2
------Betting on odd / even -------
1. Bet on odd
2. Bet on even
Enter your choice (1-2): 1
Enter bet amount: 10
The number on the Roulette wheel is : 36
You lost the bet !
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 2
------Betting on odd / even -------
1. Bet on odd
2. Bet on even
Enter your choice (1-2): 2
Enter bet amount: 10
The number on the Roulette wheel is : 17
You lost the bet !
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 2
------Betting on odd / even -------
1. Bet on odd
2. Bet on even
Enter your choice (1-2): 1
Enter bet amount: 10
The number on the Roulette wheel is : 19
You win $20.00
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 3
------Betting on a dozen -------
1. Bet on dozen 1 - 12
2. Bet on dozen 13 - 24
2. Bet on dozen 25 - 36
Enter your choice (1-3): 1
Enter bet amount: 10
The number on the Roulette wheel is : 12
You win $30.00
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 3
------Betting on a dozen -------
1. Bet on dozen 1 - 12
2. Bet on dozen 13 - 24
2. Bet on dozen 25 - 36
Enter your choice (1-3): 2
Enter bet amount: 10
The number on the Roulette wheel is : 2
You lost the bet !
Do you wish to continue? y/n: y
1. Bet on a number
2. Bet on odd / even
3. Bet on a dozen
Enter your choice: 3
------Betting on a dozen -------
1. Bet on dozen 1 - 12
2. Bet on dozen 13 - 24
2. Bet on dozen 25 - 36
Enter your choice (1-3): 3
Enter bet amount: 10
The number on the Roulette wheel is : 15
You lost the bet !
Do you wish to continue? y/n: n
********* Thank you for playing Roulette Game **********
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.