Found 7197 Articles for C++

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

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

619 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

259 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

192 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

317 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

278 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

137 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

Activity Selection Problem (Greedy Algo-1) in C++?

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

2K+ Views

There are n different activities are given with their starting time and ending time. Select maximum number of activities to solve by a single person.We will use the greedy approach to find the next activity whose finish time is minimum among rest activities, and the start time is more than or equal with the finish time of the last selected activity.The complexity of this problem is O(n log n) when the list is not sorted. When the sorted list is provided the complexity will be O(n).InputA list of different activities with starting and ending times.{(5, 9), (1, 2), (3, 4), ... Read More

Absolute Difference of even and odd indexed elements in an Array (C++)?

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

572 Views

Here we will see how we can get the absolute differences of odd and even indexed elements in an array. The absolute difference indicates that if the difference of one pair is negative, the absolute value will be taken. For an example, let the numbers are {1, 2, 3, 4, 5, 6, 7, 8, 9}. So the even position elements are 1, 3, 5, 7, 9 (starting from 0), and odd place elements are 2, 4, 6, 8. So the difference for even placed data are |1 - 3| = 2, then |2 - 5| = 3, |3 - 7| ... Read More

Advertisements