Found 7347 Articles for C++

Check if a number can be expressed as a sum of consecutive numbers in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:26:52

304 Views

Here we will see if one number can be represented as sum of two or more consecutive numbers or not. Suppose a number is 12. This can be represented as 3+4+5.There is a direct and easiest method to solve this problem. If a number is power of 2, then it cannot be expressed as sum of some consecutive numbers. There are two facts that we have to keep in mind.Sum of any two consecutive numbers is odd, then one of them will be odd, another one is even.Second fact is 2n = 2(n-1) + 2(n-1).Example Live Demo#include using namespace std; ... Read More

Check if a number can be expressed as 2^x + 2^y in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:24:44

98 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

Check if a line passes through the origin in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:22:17

554 Views

In this section we will see, how to check a line segment is passes through the origin or not. We have two coordinate points to represent the endpoints of the line segment.The approach is simple. If we can form the equation of the straight line, and by placing (0, 0) into the equation, and the equation satisfies, then the line passes through the origin.Suppose the points are and So the equation of line passes through these two lines is −$$y-y_{1}=\left(\frac{y_{2}-y_{1}}{x_{2}-x_{1}}\right)*\lgroup x-x_{1}\rgroup+c$$Putting x = 0 and y = 0, we get$$x_{1}\lgroup y_{2}-y_{1}\rgroup=y_{1}\lgroup x_{2}-x_{1}\rgroup$$Example Live Demo#include using namespace std; bool checkPassOrigin(int x1, int ... Read More

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

Arnab Chakraborty
Updated on 27-Sep-2019 08:18:34

212 Views

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

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

Arnab Chakraborty
Updated on 27-Sep-2019 08:14:44

359 Views

Here we will see how to check a number is divisible by 8 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 8, if the number formed by last three digits are divisible by 8.Example Live Demo#include using namespace std; bool isDiv8(string num){    int n = num.length();    int last_three_digit_val = (num[n-3] - '0') * 100 + (num[n-2] - '0') * 10 + ((num[n-1] - '0'));    if(last_three_digit_val % 8 == 0)       return true;       return false; } int main() {    string num = "1754586672360";    if(isDiv8(num)){       cout

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

Arnab Chakraborty
Updated on 27-Sep-2019 08:11:34

86 Views

Here we will see how to check a number is divisible by 75 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 75, when the number is divisible by 3 and also divisible by 25. if the sum of digits is divisible by 3, then the number is divisible by 3, and if last two digits are divisible by 25, then the number is divisible by 25.Example Live Demo#include using namespace std; bool isDiv75(string num){    int n = num.length();    long sum = accumulate(begin(num), end(num), ... Read More

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

Arnab Chakraborty
Updated on 27-Sep-2019 08:09:21

503 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 Live Demo#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

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

Arnab Chakraborty
Updated on 27-Sep-2019 08:04:05

370 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 Live Demo#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

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

Arnab Chakraborty
Updated on 27-Sep-2019 07:58:01

153 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 Live Demo#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

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

Arnab Chakraborty
Updated on 27-Sep-2019 07:55:42

152 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

Advertisements