Found 7347 Articles for C++

C++ Program for Common Divisors of Two Numbers?

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

231 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 digits to A such that it is divisible by B after each addition in C++?

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

127 Views

Here we will see how to generate a number A by adding N digits with it, and while adding new digits in each stage it will be divisible by another number B. Let us consider we are going to make a 5-digit number by adding 4 extra digits with it. We will check the divisibility by 7. The number will start from 8. So at first it will append 4 with it, so the number will be 84, that is divisible by 7. Then add 0 with the number so it will remain divisible by 7. If the number cannot ... Read More

Add n binary strings in C++?

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

145 Views

Here we will see how we can write a program that can add n binary numbers given as strings. The easier method is by converting binary string to its decimal equivalent, then add them and convert into binary again. Here we will do the addition manually.We will use one helper function to add two binary strings. That function will be used for n-1 times for n different binary strings. The function will work like below.AlgorithmaddTwoBinary(bin1, bin2)begin    s := 0    result is an empty string now    i := length of bin1, and j := length of bin2   ... 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

77 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++

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

1K+ Views

In this section we will see how to write a program that can check whether a given number is Adam number or not. Before diving into code let us see what is the Adam number?The Adam number is a number say n, then if the square of n and square of the reverse of n are reverse of each other, then the number is Adam number. For an example let us consider a number 13. The reverse is 31. Then square of 13 is 169, and square of 31 is 961. The 169 and 961 are reverse of each other ... 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

acos() function for complex number in C++?

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

122 Views

Here we will see the acos() method for the complex numbers. The complex numbers can be used using complex header file. In that header file the acos() function is also present. This is complex version of normal acos() function. This is used to find complex arc cosine of a complex number.This function takes a complex number as input parameter, and returns the arc cosine as output. Let us see one example to get the idea.Example Live Demo#include #include using namespace std; main() {    complex c1(5, 2);    //acos() function for complex number    cout

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

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

457 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

Absolute Difference of all pairwise consecutive elements in an array (C++)?

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

434 Views

In this problem we will see how we can get the absolute differences between the elements of each pair of elements in an array. If there are n elements, the resultant array will contain n-1 elements. Suppose the elements are {8, 5, 4, 3}. The result will be |8-5| = 3, then |5-4| = 1, |4-3|=1.AlgorithmpairDiff(arr, n)begin    res := an array to hold results    for i in range 0 to n-2, do       res[i] := |res[i] – res[i+1]|    done endExample Live Demo#include #include using namespace std; void pairDiff(int arr[], int res[], int n) {    for ... Read More

A Sum Array Puzzle in C++?

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

118 Views

Here we will see one interesting problem related to array. There is an array with n elements. We have to create another array of n elements. But the i-th position of second array will hold the sum of all elements of the first array except the i-th element. And one constraint is that we cannot use the subtraction operator in this problem.If we can use the subtraction, operation, we can easily solve this problem, by getting the sum of all elements, then subtract i-th element of first array and store it into i-th place of the second array.Here we are ... Read More

Advertisements