Write a C program to input a random number in the range [1,10] and also input numbers from the user until the user entered number matches with the random number generated. In the output, print the number of guesses in which the user input matches with the entered number.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.2: While Loops
Problem 6E: (Practice) An automobile travels at an average speed of 55 mph for 4 hours. Write a C++ program that...
icon
Related questions
icon
Concept explainers
Question

4) Write a C program to input a random number in the range [1,10] and also input numbers from the user until the user entered number matches with the random number generated. In the output, print the number of guesses in which the user input matches with the entered number. 

Expert Solution
Step 1

here we write code in c:

==============================================================================

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
 int mynumber,usernumber;
 int count=1;
 srand(time(NULL));
 mynumber = (rand()%10 +1);
 printf("enter any number between 1 to 10:\n");
 while(1){
  printf("enter your guess:\n");
  scanf("%d",&usernumber);
  if(mynumber ==  usernumber){
   printf(" total  guesses count:%d",count);
   break;
  }
  else {
   count++;
   printf("try again.\n");
  }
 }
 return 0;
}

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr