Server Side Programming Articles - Page 2148 of 2651

C Program for replacing one digit with other

Sunidhi Bansal
Updated on 21-Oct-2019 11:14:31

4K+ Views

Given a number n, we have to replace a digit x from that number with another given number m. we have to look for the number whether the number is present in the given number or not, if it is present in the given number then replace that particular number x with another number m.Like we are given with a number “123” and m as 5 and the number to be replaced i.e. x as “2”, so the result should be “153”.ExampleInput: n = 983, digit = 9, replace = 6 Output: 683 Explanation: digit 9 is the first digit ... Read More

C Program for Reversed String Pattern

Sunidhi Bansal
Updated on 21-Oct-2019 11:09:27

766 Views

Given a string str, our task is to print its reversed pattern. The Pattern will be incremental in reversed order, and when the string is completed fill ‘*’ in the remaining places.Like we enter a string “abcd”, now in first line we have to print “a” then in next line we have to print “cb” and then in third line we will print “**d”.ExampleInput: str[] = { “abcd” } Output: a c b * * dExplanation −In first line print 1 characterIn second line print 2 characters in reverse orderIn third line Print 3 characters in reverse order, if the ... Read More

C Program to check if an Array is Palindrome or not

Sunidhi Bansal
Updated on 21-Oct-2019 11:05:02

15K+ Views

Given an array arr[] of any size n, our task is to find out that the array is palindrome or not. Palindrome is a sequence which can be read backwards and forward as same, like: MADAM, NAMAN, etc.So to check an array is palindrome or not so we can traverse an array from back and forward like −ExampleInput: arr[] = {1, 0, 0, 1} Output: Array is palindrome Input: arr[] = {1, 2, 3, 4, 5} Output: Array is not palindromeApproach used below is as follows −We will traverse the array from starting as well as from the end until ... Read More

C Program to check Plus Perfect Number

Sunidhi Bansal
Updated on 21-Oct-2019 10:56:13

321 Views

Given a number x with n number of digits, our task is to check whether the given number’s Plus Perfect number or not. In order to check that the number is Plus Perfect Number we find the nth power of every digit d (d^n) and then sum all the digits, if the sum is equal to n then the number is Plus Perfect Number. Plus Perfect number is similar like finding an Armstrong of any number.Like In the given example below −ExampleInput: 163 Output: Number is not a perfect_number Explanation: 1^3 + 6^3 + 3^3 is not equal to 163 ... Read More

Check if a number is formed by Concatenation of 1, 14 or 144 only in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:27:08

235 Views

Here we will see one problem, that can tell that whether a string or a number is a concatenation of 1, 14 or 144 only. Suppose a string is “111411441”, this is valid, but “144414” is not valid.The task is simple, we have to fetch a single digit, double-digit and triple-digit number from the last, and check whether they match with any of these three (1, 14 and 144), if we get one match, divide the number with it, and repeat this process until the entire number is not exhausted.Example#include #include using namespace std; bool checkNumber(long long number) ... Read More

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

Arnab Chakraborty
Updated on 21-Oct-2019 10:22:18

194 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 Live Demo#include #include ... Read More

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

Arnab Chakraborty
Updated on 21-Oct-2019 10:19:20

219 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 Live Demo#include #include using namespace ... Read More

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

Arnab Chakraborty
Updated on 21-Oct-2019 10:16:33

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 has bits in alternate pattern - Set-2 O(1) Approach in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:12:32

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

Check if a number has bits in alternate pattern - Set 1 in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:08:52

286 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: check each digit using binary equivalent, and if two consecutive are same, return false, otherwise true.Example#include using namespace std; bool hasAlternatePattern(unsigned int n) {    int previous = n % 2;    n = n/2;    while (n > 0) {       int current = n % 2;       if (current == previous) // If current bit is same as previous       return false;       previous = current;       n = n / 2;    }    return true; } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Advertisements