Found 7346 Articles for C++

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

Arnab Chakraborty
Updated on 27-Sep-2019 07:53:14

209 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 11 or not in C++

Arnab Chakraborty
Updated on 27-Sep-2019 07:51:07

431 Views

Here we will see how to check a number is divisible by 11 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 11, if the sum of odd position values and the sum of even position values are same, then the number is divisible by 11.Example Live Demo#include using namespace std; bool isDiv11(string num){    int n = num.length();    long odd_sum = 0, even_sum = 0;    for(int i = 0; i < n; i++){       if(i % 2 == 0){ ... Read More

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

Arnab Chakraborty
Updated on 27-Sep-2019 07:48:52

135 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
Updated on 27-Sep-2019 07:43:32

356 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

Alternating Vowels and Consonants in C/C++

Narendra Kumar
Updated on 26-Sep-2019 13:41:39

274 Views

Given an input string with vowels and consonants. Rearrange the string such a way that vowels and consonants occupies alternate position in final string. As we are arranging vowels and consonants at alternate position the input string must satisfy either of the following conditions −Number of vowels and consonants must be same e.g. string "individual" has 5 vowels and 5 consonants.If number of vowels are more, then difference between number of vowels and number of consonants must be 1 e.g. string "noe" has 2 vowels and 1 consonant.If number of consonants are more, then difference between number of consonants and ... Read More

C/C++ Ternary Operator

Narendra Kumar
Updated on 26-Sep-2019 13:36:49

4K+ 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

C++17 If statement with initializer

Narendra Kumar
Updated on 26-Sep-2019 13:34:49

11K+ 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

Check if a directed graph is connected or not in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:54:52

1K+ Views

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case the traversal algorithm is recursive DFS traversal.Input − Adjacency matrix of a graph0100000100000111000001000Output − The Graph is connected.Algorithmtraverse(u, visited) Input: The start node u and the ... Read More

Check if a binary string has two consecutive occurrences of one everywhere in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:03:49

152 Views

Here we will see another interesting problem. We have to write a code that accepts a string, which has following criteria.Every group of consecutive 1s, must be of length 2every group of consecutive 1s must appear after 1 or more 0sSuppose there is a string like 0110, this is valid string, whether 001110, 010 are not validHere the approach is simple. we have to find the occurrences of 1, and check whether it is a part of sub-string 011 or not. If condition fails, for any substring then return false, otherwise true.Example Live Demo#include using namespace std; bool isValidStr(string str) ... Read More

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

Arnab Chakraborty
Updated on 25-Sep-2019 13:56:06

111 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

Advertisements