Server Side Programming Articles - Page 2229 of 2646

C++ Program to count Vowels in a string using Pointer?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

607 Views

To get the vowels from a string, we have to iterate through each character of the string. Here we have to use pointers to move through the string. For this we need C style strings. If the string is pointed by str, then *str will hold the first character at the beginning. Then if str is increased, the *str will point next character and so on. If the character is in [a, e, i, o, u] or [A, E, I, O, U] then it is vowel. So we will increase the countAlgorithmcountVowels(str)begin    count := 0    for each character ... Read More

C++ program to concatenate a string given number of times?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

628 Views

Here we will see how we can concatenate a string n number of times. The value of n is given by the user. This problem is very simple. In C++ we can use + operator for concatenation. Please go through the code to get the idea.AlgorithmconcatStrNTimes(str, n)begin    res := empty string    for i in range 1 to n, do       res := concatenate res and res    done    return res endExample Live Demo#include using namespace std; main() {    string myStr, res = "";    int n;    cout > myStr;    cout > n;    for(int i= 0; i < n; i++) {       res += myStr;    }    cout

C++ Program for Sum of squares of first n natural numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

650 Views

In this problem we will see how we can get the sum of squares of first n natural numbers. Here we are using one for loop, that runs from 1 to n. In each step we are calculating square of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use this series formula −AlgorithmsquareNNatural(n)begin    sum := 0    for i in range 1 to n, do       sum := sum + i^2    done    return ... Read More

C++ program for Solving Cryptarithmetic Puzzles

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

In the crypt-arithmetic problem, some letters are used to assign digits to it. Like ten different letters are holding digit values from 0 to 9 to perform arithmetic operations correctly. There are two words are given and another word is given as answer of addition for those two words. As an example we can say that two words ‘BASE’ and ‘BALL’, and the result is ‘GAMES’. Now if we try to add BASE and BALL by their symbolic digits, we will get the answer GAMES.NOTE − There must be ten letters maximum, otherwise it cannot be solved.InputThis algorithm will take ... Read More

C++ Program for Smallest K digit number divisible by X?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

293 Views

In this problem we will try to find smallest K-digit number, that will be divisible by X. To do this task we will take the smallest K digit number by this formula (10^(k-1)). Then check whether the number is divisible by X or not, if not, we will get the exact number by using this formula.(min+ 𝑋)−((min+ 𝑋) 𝑚𝑜𝑑 𝑋)One example is like a 5-digit number, that is divisible by 29. So the smallest 5-digit number is 10000. This is not divisible by 29. Now by applying the formula we will get −(10000+ 29)−((10000+29) 𝑚𝑜𝑑 29)=10029−24=10005The number 10005 is divisible ... Read More

C++ Program for GCD 0.of more than two (or array) numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

216 Views

Here we will see how we can get the gcd of more than two numbers. Finding gcd of two numbers are easy. When we want to find gcd of more than two numbers, we have to follow the associativity rule of gcd. For example, if we want to find gcd of {w, x, y, z}, then it will be {gcd(w, x), y, z}, then {gcd(gcd(w, x), y), z}, and finally {gcd(gcd(gcd(w, x), y), z)}. Using array it can be done very easily.Algorithmgcd(a, b)begin    if a is 0, then       return b    end if    return gcd(b ... Read More

C++ Program for Common Divisors of Two Numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

340 Views

Here we will see how we can get the number of common divisors of two numbers. We are not going to find all common divisors, but we will count how many common divisors are there. If two numbers are like 12 and 24, then common divisors are 1, 2, 3, 4, 6, 12. So there are 6 common divisors, so the answer will be 6.AlgorithmcountCommonDivisor(a, b)begin    count := 0    gcd := gcd of a and b    for i := 1 to square root of gcd, do       if gcd is divisible by 0, then   ... Read More

Add n binary strings in C++?

Nishu Kumari
Updated on 29-Jul-2025 18:52:05

315 Views

We are given n binary strings, and we need to add them together and return the result (as a binary string). A binary string is a string that consists only of the characters '0' and '1', which represent binary digits. We will add them bit by bit using binary addition rules. Addition of Binary Numbers Here's how binary addition with binary numbers works: 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 ... Read More

Add minimum number to an array so that the sum becomes even in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

163 Views

Suppose there is an array with some numbers. We have to tell minimum how many numbers will be added with it to make the sum of the elements even. The number must be greater than 0. So if the sum of the elements is odd, we will add 1, but if the sum is already even, then we will add 2 with it to make it even.AlgorithmaddMinNumber(arr)begin    s := 0    for each element e from arr, do       s := e + s    done    if s is even, then return 2, otherwise 1 endExample Live ... Read More

Adam Number in C++

Ravi Ranjan
Updated on 11-Jul-2025 18:16:36

2K+ Views

The Adam number is a number such that the square of the given number 'n' is the reverse of the square of the reverse of that number 'n'. In this article, our task is to write a program that can check whether the given number is an Adam number or not. Here is an example to check whether 12 is an Adam number or not: Input: num = 12 Output: 12 is an Adam number Explanation: num = 12 , square of num = ... Read More

Advertisements