Server Side Programming Articles

Page 1458 of 2109

Check if a number is magic (Recursive sum of digits is 1) in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see one program, that can check whether a number is magic number or not. A number is said to be magic number, when the recursive sum of the digits is 1. Suppose a number is like 50311 = 5 + 0 + 3 + 1 + 1 = 10 = 1 + 0 = 1, this is magic number.To check whether a number is magic or not, we have to add the digits until a single-digit number is reached.Example#include using namespace std; int isMagicNumber(int n) {    int digit_sum = 0;    while (n > 0 ...

Read More

Check if a number is sandwiched between primes in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 135 Views

Here we will see whether a number is sandwiched between primes or not. A number is said to be sandwiched between primes when the number just after it, and just below it is prime numbers. To solve this, check whether n-1 and n+1 are prime or not.Example#include #include #define N 100005 using namespace std; bool isPrime(int n) {    if (n == 0 || n == 1)       return false;    for (int i=2;i

Read More

Check if a + b = c is valid after removing all zeroes from a, b and c in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 158 Views

Suppose we have three numbers a, b, c, we have to check whether a + b = c, after removing all 0s from the numbers or not. Suppose the numbers are a = 102, b = 130, c = 2005, then after removing 0s, the numbers will be a + b = c : (12 + 13 = 25) this is trueWe will remove all 0s from a number, then we will check after removing 0s, a + b = c or not.Example#include #include using namespace std; int deleteZeros(int n) {    int res = 0;    int ...

Read More

Check if a binary string contains consecutive same or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 384 Views

Suppose we have a binary string. Our task is to check whether the string has consecutive same characters or not. If there are consecutive same characters, then that is invalid, otherwise valid. Then the string “101010” is valid, but “10111010” is invalid.To solve this problem, we will traverse from left to right, if two consecutive characters are the same, then return false, otherwise true.Example#include #include using namespace std; bool isConsecutiveSame(string str){    int len = str.length();    for(int i = 0; i

Read More

Check if a given array is pairwise sorted or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 247 Views

We have an array A, with n elements. We have to check whether the array is pairwise sorted or not. Suppose the array is like {8, 10, 18, 20, 5, 15}. This is pairwise sorted as (8, 10), (18, 20), (5, 15) are sorted. If the array has an odd number of elements, then the last one will be ignored.The approach is too simple, by taking I from 0 to n-1, we will see if the ith element is less than the i+1th element or not, if not, then return false, otherwise increase I by 2.Example#include #include using namespace std; bool isPairwiseSorted(int arr[], int n) {    if(n

Read More

Check if a given number is sparse or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 687 Views

In this section, we will see how to check a number is sparse or not. A number is said to be sparse if the binary representation of the number, has no two or more than two consecutive 1s. Suppose a number is like 72. This is 01001000. Here no two or more consecutive 1s.To check a number is sparse or not, we will take the number as n, then shift that number one bit to the right, and perform bitwise AND. if the result is 0, then that is a sparse number, otherwise not.Example#include using namespace std; bool isSparseNumber(int ...

Read More

Check if a number has bits in alternate pattern - Set-2 O(1) Approach in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 534 Views

Let us consider we have an integer n. The problem is to check, whether this integer has alternate patterns in its binary equivalent or not. The alternate pattern means 101010….The approach is like: calculate num = n XOR (n >> 1), now if all bits of num is 1, then the num has alternating patterns.Example#include #include using namespace std; bool isAllBitSet(int n){    if (((n + 1) & n) == 0)       return true;    return false; } bool hasAlternatePattern(unsigned int n) {    unsigned int num = n ^ (n >> 1);    return isAllBitSet(num); } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Read More

Check if a number is a Krishnamurthy Number or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number, The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 266 Views

Here we will see one program, that can check whether a number is divisible by 23 or not. Suppose a number 1191216 is given. This is divisible by 23.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timeadd 7 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.17043, so 1704 + 7*3 = 1725 1725, so 172 + 7 * 5 = 207 207, this is 9 * 23, so 17043 is divisible by 23.Example#include #include using namespace std; ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 246 Views

Here we will see one program, that can check whether a number is divisible by 41 or not. Suppose a number 104413920565933 is given. This is divisible by 41.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timesubtract 4 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.30873, so 3087 - 4*3 = 3075 3075, so 307 - 4 * 5 = 287 287, so 28 – 4 * 7 = 0 So, 30873 is divisible by 41.Example#include #include ...

Read More
Showing 14571–14580 of 21,090 articles
Advertisements