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

517 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

193 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 Large Number is Divisible by 9 in C++

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

307 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 in C++

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

455 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 in C++

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

213 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 in C++

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

598 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

Print All Odd Numbers in a Range using Python

Pavitra
Updated on 27-Sep-2019 08:05:27

955 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a range, we need to print all the odd numbers in the given range.The brute-force approach is discussed below −Here we apply a range-based for loop which provides all the integers available in the input interval.After this, a check condition for odd numbers is applied to filter all the even numbers.This approach takes O(n) + constant time of comparison.Now let’s see the implementation below −Examplestart, end = 10, 29 # iteration for num in range(start, end + 1):    # check   ... Read More

Check Large Number Divisibility by 3 in C++

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

471 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

Print All Even Numbers in a Range using Python

Pavitra
Updated on 27-Sep-2019 08:01:48

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a range, we need to print all the even numbers in the given range.The brute-force approach is discussed below −Here we apply a range-based for loop which provides all the integers available in the input interval.After this, a check condition for even numbers is applied to filter all the odd numbers.This approach takes O(n) + constant time of comparison.Now let’s see the implementation below −Examplestart, end = 10, 29 # iteration for num in range(start, end + 1):    # check   ... Read More

Get All Tables from MySQL Database with Specific Column

AmitDiwan
Updated on 27-Sep-2019 08:00:45

249 Views

Let’s say we have a database “web” and we need to get all the tables having a specific column ’StudentFirstName’.For this, below is the query −mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';This will produce the following output −+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+ 7 rows in set (0.14 sec)Therefore, the above tables have one of the column names as “StudentFirstName”.Let us check the description ... Read More

Advertisements