- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Found 7347 Articles for C++
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
494 Views
Here we will see, whether a number is power of another number or not. Suppose a number 125, and another number 5 is given. So it will return true when it finds that 125 is power of 5. In this case it is true. 125 = 53.AlgorithmisRepresentPower(x, y): Begin if x = 1, then if y = 1, return true, otherwise false pow := 1 while pow < y, do pow := pow * x done if pow = y, then return true return false ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
529 Views
Here we will see how to check whether a number is Mystery number or not. A Mystery number is a number that can be represented by sum of two numbers, and the numbers re reverse of each other. Let us see the code to get better idea. We have to check all pairs and find the decision.Example Live Demo#include using namespace std; int revNum(int str) { string s = to_string(str); reverse(s.begin(), s.end()); stringstream ss(s); int rev = 0; ss >> rev; return rev; } bool isMysteryNumber(int n) { for (int i=1; i
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
450 Views
Here we will see, if a number has adjacent set bits in its binary representation. Suppose the number 12 has two consecutive 1s (12 = 1100).To check this type of number, the idea is very simple. We will shift the number 1 bit, then do bitwise AND. If the bitwise AND result is non-zero, then there must be some consecutive 1s.Example Live Demo#include using namespace std; bool hasConsecutiveOnes(int n) { if((n & (n >> 1)) == 1){ return true; }else{ return false; } } int main() { int num = 67; //1000011 if(hasConsecutiveOnes(num)){ cout
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
184 Views
In this section we will check whether a number has same number of set bits and unset bits or not. Suppose the number 12 is there. Its binary representation is 1100. This has same number of 0s and 1s.The approach is simple. We will check each bit of the number, and if that is 1, then increase set_bit_count, and if it is 0, then increase unset_bit_count. Finally, if they are same, then return true, otherwise false.Example Live Demo#include using namespace std; bool hasSameSetUnset(int n) { int set_count = 0, unset_count = 0; while(n){ if((n & ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
122 Views
In this section we will see, whether a number can be represented as tree consecutive numbers or not. Suppose a number is 27. This can be represented as 8 + 9 + 10.This can be solved in two different approach. The first approach is Naïve approach. In that approach, we have to check i + (i + 1) + (i + 2) is same as number or not. Another efficient approach is by checking whether the number is divisible by 3 or not. Suppose a number x can be represented by three consecutive 1s, then x = (y - 1) ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
218 Views
Here we will see, if we can represent a number as sum of two non-zero powers of 2. So we will check the given number N can be represented as (2x + 2y) where x, y > 0. Suppose a number is 10, this can be represented as 23 + 21.The approach is simple. There are two cases. If the number n is even, it can be represented as 2x. Where x > 0. Another case is that is N is odd, it can never be represented as sum of powers of 2. We cannot use power as 0, so ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
111 Views
In this section, we will see if we can express one number as the sum of two triangular numbers or not. The triangular numbers are like below −From the example, we can see that 1, 3, 6, 10 are some triangular numbers. We need to express a number N (say 16) as sum of two triangular numbers (6, 10).The approach is very simple. We have to get all triangular numbers less than N. Form a set from these values. Now we have to take a number say X from the set, and check whether N – X is present in ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
146 Views
Here we will check whether we can represent a number as power, like xy or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin if num = 1, then return true for i := 2, i2
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
134 Views
Here we will check whether we can represent a number as power, like ab or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin if num = 1, then return true for i := 2, i2
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
278 Views
Here we will check whether we can represent a number like ab or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin if num = 1, then return true for i := 2, i2