C++ Articles

Page 275 of 597

Find One's Complement of an Integer in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

In this section, we will see how to find the 1’s complete of an integer. We can use the complement operator to do this task very fast, but it will make 32bit complemented value (4-bype integer). Here we want complement of n bit numbers.Suppose we have a number say 22. The binary equivalent is 10110. The complemented value is 01001 which is same as 9. Now the question comes, how to find this value? At first we have to find number of bits of the given number. Suppose the count is c (here c = 5 for 22). We have ...

Read More

Check if a binary string has a 0 between 1s or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 240 Views

Here we will see one interesting problem. We have to check whether a string has 0 in between a 1s or not. If not, then the string is valid, otherwise invalid.Suppose there are three strings −100011110100000111110001111101111From these three strings, only B is valid, because there is no 0 inside the stream of 1sTo solve this problem, we will find the index of first 1 present in the string, and also find the index of the last 1. Then we will check, is there any 0 from these two indices, if so, then return false, otherwise true (as valid)Example#include using ...

Read More

C++17 If statement with initializer

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 15K+ Views

C++17 has extended existing if statement’s syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight. Which in turn avoids variable leaking outside the scope.ExampleLet us suppose we want to check whether given number is even or odd. Before C++17 our code used to look like this −#include #include using namespace std; int main() {    srand(time(NULL));    int random_num = rand();    if (random_num % 2 == 0) {       cout

Read More

Check if a given string is made up of two alternating characters in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 511 Views

Here we will see how to check a string is made up of alternating characters or not. If a string is like XYXYXY, it is valid, if a string is like ABCD, that is invalid.The approach is simple. We will check whether all ith character and i+2 th character are same or not. if they are not same, then return false, otherwise return true.Example#include using namespace std; bool hasAlternateChars(string str){    for (int i = 0; i < str.length() - 2; i++) {       if (str[i] != str[i + 2]) {          return false; ...

Read More

Check if a large number is divisibility by 15 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 217 Views

Here we will see how to check a number is divisible by 15 or not. In this case the number is very large number. So we put the number as string.To check whether a number is divisible by 15, if the number is divisible by 5, and divisible by 3. So to check divisibility by 5, we have to see the last number is 0 or 5. To check divisibility by 3, we will see the sum of digits are divisible by 3 or not.Example#include using namespace std; bool isDiv15(string num){    int n = num.length();    if(num[n - ...

Read More

Check if a large number is divisible by 2, 3 and 5 or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 328 Views

Here we will see how to check a number is divisible by 2, 3 and 5 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 2, 3 and 5 if that number is divisible by LCM of 2, 3 and 5. So the LCM of 2, 3, 5 is 30. We have to check the number is divisible by 30 or not. A number is divisible by 30 when it is divisible by 10 (last digit is 0) and divisible by 3 (sum of all digits ...

Read More

Check if a large number is divisible by 20 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 260 Views

Here we will see how to check a number is divisible by 20 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 20, when that is divisible by 10, and after dividing 10, the remaining number is divisible by 2. So the case is simple. If the last digit is 0, then it is divisible by 10, and when it is divisible by 10, then the second last element is divisible by 2, the number is divisible by 20.Example#include using namespace std; bool isDiv20(string num){ ...

Read More

Check if a large number is divisible by 25 or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 280 Views

Here we will see how to check a number is divisible by 25 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 25, when the last two digits are 00, or they are divisible by 25.Example#include using namespace std; bool isDiv25(string num){    int n = num.length();    int last_two_digit_val = (num[n-2] - '0') * 10 + ((num[n-1] - '0'));    if(last_two_digit_val % 25 == 0)       return true;       return false; } int main() {    string num = "451851549333150";    if(isDiv25(num)){       cout

Read More

Check if a large number is divisible by 3 or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 529 Views

Here we will see how to check a number is divisible by 3 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 3, if the sum of digits is divisible by 3.Example#include using namespace std; bool isDiv3(string num){    int n = num.length();    long sum = accumulate(begin(num), end(num), 0) - '0' * n;    if(sum % 3 == 0)       return true;       return false; } int main() {    string num = "3635883959606670431112222";    if(isDiv3(num)){       cout

Read More

Check if a large number is divisible by 5 or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 659 Views

Here we will see how to check a number is divisible by 5 or not. In this case the number is very large number. So we put the number as string.To check whether a number is divisible by 5, So to check divisibility by 5, we have to see the last number is 0 or 5.Example#include using namespace std; bool isDiv5(string num){    int n = num.length();    if(num[n - 1] != '5' && num[n - 1] != '0')    return false;    return true; } int main() {    string num = "154484585745184258458158245285265";    if(isDiv5(num)){       cout

Read More
Showing 2741–2750 of 5,962 articles
« Prev 1 273 274 275 276 277 597 Next »
Advertisements