# Number guessing game in java

In this article, we will implement Number guessing game in java.

Table of Contents

The number guessing game is based on a concept where player `guesses` a number between a `range`. If player guesses the exact number then player wins else player looses the game. Since this game provides limited attempts, so, player must guess the number with the limited attempts, else will lose the game.

Let’s understand the game and its rules.

## Number guessing game Rules

1. You must enter only valid integer within the specified range.
2. You will be provided limited attempts to guess the number.
3. You cannot leave the game, once started.

If the entered number is less than or greater than the required number, then player gets the message (hint) to proceed further either in up or down range.

## Algorithm for Number guessing game

`Step 1:` Create three variables `attempt`, `userGuessNumber`, `secretNumber` and initialize them.
`Step 2:` Generate a random number and assigned to `secretNumber`.
`Step 3:` Start a loop and take user input.
`Step 4:` Validate user input and match with the `secretNumber.`
`Step 5:` If `userGuessNumber` is greater than or less than the `secretNumber` then print a message to user.
`Step 6:` If `userGuessNumber` is equal to the `secretNumber`, then user wins and exit the game.
`Step 7:` If number of attempts exceeds the `limit`, exit the game.
`Step 8:`Repeat from Step 3.

Output

Welcome to Guess Number Game
You Will Be Asked To Guess A Number To Win The Game
You have Maximum 5 Attemp Limit
Enter a guess number between 1 to 100
75
Your Guess Number is Greater.
Enter a guess number between 1 to 100
45
Your Guess Number is Smaller.
Enter a guess number between 1 to 100
58
Your Guess Number is Smaller.
Enter a guess number between 1 to 100
67
Your Guess Number is Greater.
Enter a guess number between 1 to 100
62
OOhhOO!, Your Number is Correct. You Win the Game!

That’s all about Number guessing game in java.

import_contacts

## Related Posts

• 05 April

### Count occurrences of Character in String in Java

Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 Features In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given String is : […]

• 24 December

### How to capitalize first letter in java

Learn about how to capitalize first letter in java.

• 09 October

### Java program to find first and last digit of a number

Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() method In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and log() methods of […]

• 03 October

### Happy Number program in Java

Table of ContentsWhat is a Happy Number?Using HashsetAlgorithmExampleUsing slow and fast pointersAlgorithmExample In this article, we are going to learn to find Happy Number using Java. Let’s first understand, what is Happy Number? What is a Happy Number? A number which leaves 1 as a result after a sequence of steps and in each step […]

• 30 September

### Find Perfect Number in Java

Table of ContentsIterative approachRecursive approach In this article, we are going to find whether a number is perfect or not using Java. A number is called a perfect number if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches to find the […]

• 28 September

### How to find Magic Number in Java

Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic Number In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a result after a […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.