Found 7197 Articles for C++

C++ program to find unique pairs such that each element is less than or equal to N

Ayush Gupta
Updated on 03-Oct-2019 11:17:31

153 Views

In this article, we will be discussing a program to find unique pairs of numbers having elements less than or equal to N and following some certain conditions −The square of difference between the two numbers must be equal to the LCM of those two numbers.The HCF of those two numbers can be represented as a product of any two consecutive numbers.The best approach to solve this problem would be to take two consecutive numbers (starting from 1) and finding the multiples of product of those numbers. Then among the multiples, to specify to one pair of numbers we need ... Read More

C++ program to find union and intersection of two unsorted arrays

Ayush Gupta
Updated on 03-Oct-2019 11:14:47

2K+ Views

In this article, we will be discussing a program to find the union and intersection of two given unsorted arrays.Let us denote the two arrays with ‘A’ and ‘B’. Then union of those arrays is denoted by A ∪ B which is basically an array of all the elements in both the given arrays; provided that each element repeats only once.To find this, we will create a separate array and copy down all the elements from the first array. Then we will traverse through the elements of the second array and check if it is already present in the union ... Read More

Check if a number N starts with 1 in b-base in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:49:33

192 Views

We have a number N and a base b. In this program we have to check whether the number is starting with 1 in base b or not. Suppose a number 6 is given. In binary this is 110, so it starts with 1, in base 4 also it will be 124. Here also it starts with 1.As we know, if a number N is represented in base b, b gets converted to m+1 bit sequence bm bm-1 … b0. This implies bm bm + bm-1 * bm-1 + … + b0*b0 = N. The largest number will be 2*bm ... Read More

Check if a number is Quartan Prime or not in C++

Akansha Kumari
Updated on 23-Jul-2025 18:48:18

391 Views

The Quartan primes are prime numbers that can be represented as x^4 + y^4. Where x, y > 0. Some Quartan prime numbers are {2, 17, 97, …}.In this article, we will learn how to check whether a number is Quartan Prime or not in C++. Consider the following input and output scenarios to understand the concept better: Scenario 1 Input: 17 Output: The number is Quartan Prime Explanation Here, we can represent the given number in the form of x^4 + y^4; (1)^4 + (2)^4 => 1 + 16 = 17. Scenario 2 Input: 12 Output: The number ... Read More

Check if a number is Full Prime in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:37:39

346 Views

Here we will see, how to check, whether a number is full prime or not. A number is said to be a full prime, if it is prime, and all of its digits are also prime. Suppose a number is 37, this is full prime. But 97 is not full prime as 9 is not a prime number.One efficient approach is that; first we have to check whether any digit is present that is not prime. Digits must be in 0 to 9. In that range 2, 3, 5 and 7 are prime, others are not prime. If all are ... Read More

Check if a number is a Pythagorean Prime or not in C++

Akansha Kumari
Updated on 24-Jul-2025 18:17:32

981 Views

The Pythagorean primes are prime numbers that can be represented in the form 4n + 1, where n is a non-negative integer. These primes have a special property as they can be expressed as the sum of two square numbers. In other words, a prime number p is called a Pythagorean prime if: p is prime, and p ≡ 1 (mod 4), i.e., when divided by 4, it leaves a remainder of 1. For example: 5 = 4x1 + 1 = 22 + ... Read More

Check if a number is a power of another number in C++

Arnab Chakraborty
Updated on 27-Sep-2019 09:13:37

607 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

Check if a number is a Mystery Numbers in C++

Arnab Chakraborty
Updated on 27-Sep-2019 09:07:29

747 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

Check if a number has two adjacent set bits in C++

Arnab Chakraborty
Updated on 27-Sep-2019 09:04:49

537 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

Check if a number has same number of set and unset bits in C++

Arnab Chakraborty
Updated on 27-Sep-2019 09:00:47

267 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

Advertisements