Found 7197 Articles for C++

Check if a number can be written as sum of three consecutive integers in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:57:56

272 Views

In this section we will see, whether a number can be represented as tree consecutive numbers or not. Suppose a number is 27. This can be represented as 8 + 9 + 10.This can be solved in two different approach. The first approach is Naïve approach. In that approach, we have to check i + (i + 1) + (i + 2) is same as number or not. Another efficient approach is by checking whether the number is divisible by 3 or not. Suppose a number x can be represented by three consecutive 1s, then x = (y - 1) ... Read More

Check if a number can be represented as sum of non zero powers of 2 in C++

Ravi Ranjan
Updated on 04-Aug-2025 19:01:46

325 Views

In this article, our task is to check if we can represent a given number as the sum of two non-zero powers of 2 in C++. For this, we will check whether the given number N can be represented as (2^x + 2^y) where x, y > 0. Here are some example scenarios: Scenario 1 Input:10 Output: Can be represented Explanation: 10 = 8 + 2 10 = 2^3 + 2^1 => 10 can be represented as 2x + 2y Scenario 2 Input:3 Output: Can be represented Explanation: 10 = 2 + ... Read More

Check if a number can be represented as a sum of 2 triangular numbers in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:39:47

180 Views

In this section, we will see if we can express one number as the sum of two triangular numbers or not. The triangular numbers are like below −From the example, we can see that 1, 3, 6, 10 are some triangular numbers. We need to express a number N (say 16) as sum of two triangular numbers (6, 10).The approach is very simple. We have to get all triangular numbers less than N. Form a set from these values. Now we have to take a number say X from the set, and check whether N – X is present in ... Read More

Check if a number can be expressed as x^y (x raised to power y) in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:36:29

198 Views

Here we will check whether we can represent a number as power, like xy or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin    if num = 1, then return true    for i := 2, i2

Check if a number can be expressed as power in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:33:30

192 Views

Here we will check whether we can represent a number as power, like ab or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin    if num = 1, then return true    for i := 2, i2

Check if a number can be expressed as a^b in C++

Arnab Chakraborty
Updated on 27-Sep-2019 08:29:51

332 Views

Here we will check whether we can represent a number like ab or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.AlgorithmisRepresentPower(num): Begin    if num = 1, then return true    for i := 2, i2

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

512 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 line passes through the origin in C++

Akansha Kumari
Updated on 05-Aug-2025 12:44:40

691 Views

In this article, we have a line and its coordinates are given. Our task is to check if this line passes through the origin or not. The basic two-point form of a straight line is given by: $$ \frac{y - y_1}{y_2 - y_1} = \frac{x - x_1}{x_2 - x_1} + c $$ For the above line to pass through the origin, put x = 0, y = 0, and c = 0. The formula for a straight line to pass through the origin can be given by: $$ y_1(x_2 - x_1) = x_1(y_2 - y_1) $$ Here are some ... 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

304 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

449 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

Advertisements