Typical run of the program: Give a positive number between 10 and 20 8 Give a po
ID: 3578296 • Letter: T
Question
Typical run of the program: Give a positive number between 10 and 20 8 Give a positive number between 10 and 20 23 Give a positive number between 10 and 20 15 The content of your array is: 31 79 113 81 139 83 123 55 34 191 27 15 172 56 39 Odd numbers are: 31 79 113 81 139 83 123 55 191 27 15 39 The maximum is 191 The minimum is 15 The sum is 1238.0 The average is 82.53333333333333 Write a java program that allows to: Generate a random integer number between 20 and 100. Ask the user to find the number by giving propositions (or guessing). The program gives feedback to the user if the proposed number is greater or less than the generated number. The user continues to guess the number until he finds the generated number or he enters the letter Q to exit the program. The program informs the user about the number of trials, the randomly generated number, and his status of success or failure to guess the number. NB: you have to run your program then join a screenshot of the execution to your answer Typical run of the program: Give your proposition: a number between 20 and 100 Q to quit: 50 Try a smaller number Give your proposition: a number between 20 and 100 Q to quit: 40 Try a bigger number Give your proposition: a number between 20 and 100 Q to quit: 45 Try a smaller number Give your proposition: a number between 20 and 100 Q to quit: 42 !!! Good you find the exact number !!! The number of trials is 4 The correct number was 42Explanation / Answer
Solution for First Image Output :-
#include<stdio.h>
int main()
{
int sum=0,temp,j,i,a,array[50]={31,79,113,81,139,83,123,55,34,191,27,15,172,56,39};
for(i=0;i<3;i++)
{
printf("Give a positive number between 10 and 20 ");
scanf("%d",&a);
}
printf("The contents of your array is : ");
for(i=0;i<15;i++)
{
printf("%d ",array[i]);
sum=sum+array[i];
}
printf(" Odd numbers are : ");
for(i=0;i<15;i++)
{
if(array[i]%2!=0)
printf("%d ",array[i]);
}
for(i=0;i<15;i++)
{
for(j=0;j<15-i-1;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
printf(" The maximum is %d",array[14]);
printf(" The minimum is %d ",array[0]);
printf(" The sum is %f",(float)sum);
printf(" The aaverage is %f",(float)sum/(float)15);
return 0;
}
Output :-
Give a positive number between 10 and 20
8
Give a positive number between 10 and 20
23
Give a positive number between 10 and 20
13
The contents of your array is :
31 79 113 81 139 83 123 55 34 191 27 15 172 56 39
Odd numbers are :
31 79 113 81 139 83 123 55 191 27 15 39
The maximum is 191
The minimum is 15
The sum is 1238.000000
The aaverage is 82.533333
Solution for Q4 :-
#include<stdio.h>
int main()
{
int number=42,num,i=0;
while(1)
{
i++;
printf("Give your position : a number between 20 and 100 Q to quit: ");
scanf("%d",&num);
if(num>number)
printf("Try a smaller number ");
else if(num<number)
printf("Try a bigger number ");
else if(num==number)
{
printf("!!! Good you find the exact number !!! ");
printf("The number of trails is %d ",i);
printf("The correct number was %d",number);
break;
}
}
return 0;
}
Output :-
Give your position : a number between 20 and 100
Q to quit:
50
Try a smaller number
Give your position : a number between 20 and 100
Q to quit:
40
Try a bigger number
Give your position : a number between 20 and 100
Q to quit:
45
Try a smaller number
Give your position : a number between 20 and 100
Q to quit:
42
!!! Good you find the exact number !!!
The number of trails is 4
The correct number was 42
Note :- At the time of input we read a integer number because the numariv value can not comare with charecter values like Q( for quit) if quit a program if entered input other then a number then use if(isdigit(num)) else quit(1). These two predefined functions worked with the help of #include<ctype.h> and #include<stdlib.h> header files.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.