C++ Articles

Page 553 of 597

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 27-Sep-2019 246 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 Live Demo#include using namespace std; bool isDiv20(string ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 27-Sep-2019 306 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 divisibility by 15 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 27-Sep-2019 205 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 Live Demo#include using namespace std; bool isDiv15(string num){    int n = num.length();    if(num[n ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 27-Sep-2019 493 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 Live Demo#include using namespace std; bool hasAlternateChars(string str){    for (int i = 0; i < str.length() - 2; i++) {       if (str[i] != str[i + 2]) {          return ...

Read More

C/C++ Ternary Operator

Narendra Kumar
Narendra Kumar
Updated on 26-Sep-2019 5K+ Views

Syntax of ternary operator is −(expression-1) ? expression-2 : expression-3This operator returns one of two values depending on the result of an expression. If "expression-1" is evaluated to Boolean true, then expression-2 is evaluated and its value is returned as a final result otherwise expression-3 is evaluated and its value is returned as a final result.ExampleLet us write a program to find maximum of two numbers using ternary operator. Live Demo#include using namespace std; int main() {    int a = 10;    int b = 20;    int max = a > b ? a : b;    cout

Read More

C++17 If statement with initializer

Narendra Kumar
Narendra Kumar
Updated on 26-Sep-2019 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 − Live Demo#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 binary string has a 0 between 1s or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Sep-2019 217 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 Live Demo#include ...

Read More

Find One&rsquo;s Complement of an Integer in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Sep-2019 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

Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Sep-2019 384 Views

Here we will see another problem, where one string and another integer value say k is given. We have to find the number of substrings of length k, whose sum of ASCII values of characters is divisible by k.Suppose a string is “BCGABC”. And the value of k is 3. Here string BCG has ASCII sum 300, ABC has ASCII sum 294, both are divisible by k = 3.The approach is simple. At first we have to find the ASCII value of characters of first substring, whose length is k. We have to use the sliding window technique and subtract ...

Read More

Find any pair with given GCD and LCM in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Sep-2019 358 Views

In this section we will see how to get number of pairs using the given GCD and LCM values. Suppose the GCD and LCM values are 2 and 12. Now the possible pairs of numbers are (2, 12), (4, 6), (6, 4) and (12, 2). So our program will find the count of pairs. That is 4.Let us see the algorithm to understand what will be the technique to solve this problem.AlgorithmcountPairs(gcd, lcm): Begin    if lcm is nit divisible by gcd, then       return 0    temp := lcm/gcd    c := primeFactorCount(temp)    res := shift ...

Read More
Showing 5521–5530 of 5,961 articles
« Prev 1 551 552 553 554 555 597 Next »
Advertisements