Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a C program for guessing the number game.
A number guessing game is a classic programming exercise where the computer has a secret number and the user must guess it. The program provides hints after each guess, telling whether the guess is too high or too low, until the correct number is found.
Syntax
do {
if (guess == secretNumber) {
// Correct guess - exit loop
} else if (guess < secretNumber) {
// Give "too low" hint
} else {
// Give "too high" hint
}
} while (condition);
Algorithm
The logic used to implement the number guessing game is as follows −
- Initialize a secret number and a flag variable
- Prompt user for their first guess
- Compare the guess with the secret number
- Provide appropriate hint (too high/too low)
- Keep track of the number of attempts
- Repeat until the correct number is guessed
Example
Following is the complete C program for the number guessing game −
#include <stdio.h>
int main() {
int num = 64, flag = 1, guess, count = 0;
printf("Welcome to the Number Guessing Game!<br>");
printf("I have a number between 1 and 100. Can you guess it?<br>");
printf("Enter your guess: ");
scanf("%d", &guess);
do {
if (num == guess) {
flag = 0;
} else if (guess < num) {
flag = 1;
printf("Your guess is lower than the number<br>");
count++;
} else {
flag = 1;
printf("Your guess is greater than the number<br>");
count++;
}
if (flag == 1) {
printf("Try again! Enter your next guess: ");
scanf("%d", &guess);
}
} while (flag);
printf("Congratulations! You guessed the correct number %d<br>", num);
printf("Total number of attempts: %d<br>", count);
return 0;
}
Output
When the above program is executed, it produces the following output −
Welcome to the Number Guessing Game! I have a number between 1 and 100. Can you guess it? Enter your guess: 45 Your guess is lower than the number Try again! Enter your next guess: 60 Your guess is lower than the number Try again! Enter your next guess: 70 Your guess is greater than the number Try again! Enter your next guess: 65 Your guess is greater than the number Try again! Enter your next guess: 62 Your guess is lower than the number Try again! Enter your next guess: 64 Congratulations! You guessed the correct number 64 Total number of attempts: 5
Key Points
- The do-while loop ensures the game continues until the correct number is guessed
- A flag variable controls the loop execution (1 for continue, 0 for stop)
- The count variable tracks the number of incorrect attempts
- Conditional statements provide appropriate feedback to guide the user
Conclusion
The number guessing game demonstrates fundamental programming concepts like loops, conditionals, and user input handling. It provides an interactive way to practice C programming while creating an engaging user experience.
Advertisements
