C Program: Create a program that will prompt the user for the number of die (bet
ID: 3886989 • Letter: C
Question
C Program: Create a program that will prompt the user for the number of die (between 0 and 3) that the user would like the computer to roll. Based on that input, and using if-else statements output the appropriate number of die values (between 1 and 12, we have fancy dice), and sum up the values appearing on the die.For example, here are a few executions: Please enter the number of die to be rolled: 2 The two die have values of 7 and 12, for a total of 19.
Please enter the number of die to be rolled: 5 We can only roll at most 3 dice.
Please enter the number of die to be rolled: -2 Number of die to be rolled must be greater than 0.
There are three test cases just to get you started, the remaining 7 points will consist of: formatting 2pts, comments 2pts, variable names 1pt, random numbers working properly (should get different die values everytime program is run) 2 points. For a total of 10 points C Program: Create a program that will prompt the user for the number of die (between 0 and 3) that the user would like the computer to roll. Based on that input, and using if-else statements output the appropriate number of die values (between 1 and 12, we have fancy dice), and sum up the values appearing on the die.
For example, here are a few executions: Please enter the number of die to be rolled: 2 The two die have values of 7 and 12, for a total of 19.
Please enter the number of die to be rolled: 5 We can only roll at most 3 dice.
Please enter the number of die to be rolled: -2 Number of die to be rolled must be greater than 0.
There are three test cases just to get you started, the remaining 7 points will consist of: formatting 2pts, comments 2pts, variable names 1pt, random numbers working properly (should get different die values everytime program is run) 2 points. For a total of 10 points
For example, here are a few executions: Please enter the number of die to be rolled: 2 The two die have values of 7 and 12, for a total of 19.
Please enter the number of die to be rolled: 5 We can only roll at most 3 dice.
Please enter the number of die to be rolled: -2 Number of die to be rolled must be greater than 0.
There are three test cases just to get you started, the remaining 7 points will consist of: formatting 2pts, comments 2pts, variable names 1pt, random numbers working properly (should get different die values everytime program is run) 2 points. For a total of 10 points
Explanation / Answer
Here is your c rolling dice program
C Dice Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
void main()
{
int TotalDice,Sum=0,n,i;
clrscr();
printf(" How many dice you want to roll ");
scanf("%d",&TotalDice); //For taking user's input
if(TotalDice>3) //checking condition for more than 3 dices
{
printf(" We can only roll at most 3 dice");
}
else if(TotalDice<=0) //checking condition for total dices below zero
{
printf(" Number of dice to be rolled must be greater than 0");
}
else
{
srand(time(NULL)); //seeding with time, so we get random value every time
printf(" The %d dice have values of",TotalDice);
for(i=0;i<TotalDice;i++)
{
n=rand()%12+1; // n for storing value of current value of rolled dice
printf(" %d and ",n); //for printing value of dices
Sum+=n; //for storing sum of all dices
}
printf(",for a total of %d",Sum); //for printing total sum of all dice values
}
getch();
}
I hope this solves your problem
Comment if you have any problem in above code
And please give it a thumbs up if it solved your problem :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.