PROGRAM TO CREATE A GAME IN WHICH A BETTING GAME IS DEVELOPED BY ROLLING A DICE:
STATEMENT:
In a betting game involving the roll of a dice, Rupesh gains Rs.X if an odd number turns up and he loses Rs.Y is an even number turns up. The numbers shown on the face of the dice in a certain number of games is passed as input. The values of X and Y are also passed as input. The program must print the net gain or loss as the output.
PROGRAM:
#include <stdio.h>
int main()
{
char input[1000];
int gainAmount,lossAmount;
fgets(input,1000,stdin);
scanf("%d%d",&gainAmount,&lossAmount);
char* ptr=input;
int offset=0;
int currentDiceNumber;
int finalAmount=0;
while(sscanf(ptr,"%d%n",¤tDiceNumber,&offset) == 1)
if(currentDiceNumber%2 == 1){
finalAmount+=gainAmount;
}else{
finalAmount-=lossAmount;
}
ptr+=offset;
}
printf("%d",finalAmount);
}
Input:
1 4 3
10
30
Output:
-10
Explanation:
He gains 20 rupees for 1 and 3 and loses 30 rupees for 4. Hence there is a net loss of 20-30 = -10
No comments:
Post a Comment