In: Computer Science
Part 2
Write a C code to create 5 random numbers between 90 and 100 (included) and print the values on the screen.
Part 3
Create a function that will mimic throwing the dice twice. The function will do the following when you call it.
The function will display “hard luck” otherwise
part2:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
int upper=100,lower=90; //Taking upper limit as 100 and lower limit as 90.
int i,num;
printf("Five random number between 90 and 100:\n\n");
srand(time(0));//by this we generate new batch of random numbers every time we run the programm.
for(i=0;i<5;i++)
{
//use rand function to generate random numbers
num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
printf("%d\t",num);//show output on screen
}
return 0;
}
part 3:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void throwdice()
{
int upper=6,lower=1; //Taking upper limit as 6 and lower limit as 1.
int firstnumber,secondnumber;
int num,i;
//use rand function to generate random numbers
for(i=0;i<2;i++)
{
//use rand function to generate random numbers
num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
if(i==0)
{
firstnumber=num;//store first random number when i==0
}
if(i==1)
{
secondnumber=num;//store second random number when i==1
}
}
printf("Two random number are: %d %d\n",firstnumber,secondnumber);//show two random numbers
if(firstnumber+secondnumber==7 || firstnumber+secondnumber==11)//checking condition as given in question
{
printf("\nWinner\n");
}
else
{
printf("\nHard luck\n");
}
}
int main()
{
srand(time(0));//by this we can generate new random numbers every time when we run the program.
throwdice(); //calling function
return 0;
}
I also merge two program if u required:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void throwdice()
{
int upper=6,lower=1; //Taking upper limit as 6 and lower limit as 1.
int firstnumber,secondnumber;
int num,i;
//use rand function to generate random numbers
for(i=0;i<2;i++)
{
//use rand function to generate random numbers
num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
if(i==0)
{
firstnumber=num;//store first random number when i==0
}
if(i==1)
{
secondnumber=num;//store second random number when i==1
}
}
printf("Two random number are: %d %d\n",firstnumber,secondnumber);//show two random numbers
if(firstnumber+secondnumber==7 || firstnumber+secondnumber==11)//checking condition as given in question
{
printf("\nWinner\n");
}
else
{
printf("\nHard luck\n");
}
}
int main()
{
int upper=100,lower=90; //Taking upper limit as 100 and lower limit as 90.
int i,num;
printf("Five random number between 90 and 100:\n\n");
srand(time(0));
for(i=0;i<5;i++)
{
//use rand function to generate random numbers
num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
printf("%d\t",num);//show output on screen
}
printf("\n\n");
throwdice();
return 0;
}
plss like if u like my answer.