- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Guess Number Higher or Lower in C++

Suppose we are playing the Guess Game. The properties of this game is as follows −

Player 1 will pick a number from 1 to n. player2 have to guess which number I picked. Every time player2 guess wrong, player1 will tell player2 whether the number is higher or lower.

We can use the function guess(num) which will return 3 possible results as follows −

-1 − Player1's number is lower

1 − Player1's number is higher

0 − Number is matched

So, if the input is like n = 10, pick = 5, then the output will be 5.

To solve this, we will follow these steps −

l := 1,r := n

while l −= r, do −

m := l+(r - l)/2

if guess(m) is the same as 0, then −

return m

if guess(m) is the same as -1, then −

r := m - 1

Otherwise

l := m + 1

return 0

## Example

Let us see the following implementation to get a better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { private: int number; int guess(int num){ if(number > num) return 1; if(number < num) return -1; return 0; } public: Solution(int n){ number = n; } int guessNumber(int n) { int l=1,r=n,m; while(l<=r){ m=l+(r-l)/2; if(guess(m)==0) return m; if(guess(m)==-1) r=m-1; else l=m+1; } return 0; } }; main(){ Solution ob(5); //pick = 5 cout << (ob.guessNumber(10)); }

## Input

5,10

## Output

5

- Related Articles
- Guess Number Higher or Lower II in C++
- What has high frequency sound: higher pitch or lower pitch?
- Number of moves required to guess a permutation in C++
- How Can We Guess That Whether The Number Is Rational Or Irrational ?
- 8085 program to show masking of lower and higher nibbles of 8-bit number
- How to convert a higher unit into a lower unit?
- How to convert a lower unit into a higher unit?
- Next higher number with same number of set bits in C++
- Melting point of most non-metals is (higher/lower) than metals.
- Convert vowels from upper to lower or lower to upper using C program
- Next higher number using atmost one swap operation in C++
- Explain how normal forms can be transformed from lower to higher(DBMS)
- Lower bound in C++
- Which temperature is higher $-$7oC or $+$7oC?
- Following are a few definitions of osmosis. Read carefully and select the correct definition.(a) Movement of water molecules from a region of higher concentration to a region of lower concentration through a semi-permeable membrane(b) Movement of solvent molecules from its higher concentration to lower concentration(c) Movement of solvent molecules from higher concentration to lower concentration of solution through a permeable membrane(d) Movement of solute molecules from lower concentration to higher concentration of solution through a semi-permeable membrane.