Found 7347 Articles for C++

C++ program to check whether it is possible to finish all the tasks or not from given dependencies

Ayush Gupta
Updated on 03-Oct-2019 11:25:23

107 Views

In this article, we will be discussing a program to check if it is possible to finish all the given tasks on the basis of the given prerequisites.For example, let us say we have been given three tasks and prerequisites are [[1, 0], [2, 1], [3, 2]].( [1, 0] means that to pick up ‘1’ task; the ‘0’ task must be completed first.)Then, in this example since the ‘0’ task doesn’t have any prerequisite it can be completed first. Then the ‘1’ task can be completed, since the ‘0’ task has been completed. Similarly, both ‘2’ and ‘3’ tasks can ... Read More

C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers

Ayush Gupta
Updated on 03-Oct-2019 11:21:14

696 Views

In this article, we will be discussing a program to find ways an integer (say X) can be expressed as sum of n-th power of unique natural numbers.For example, let X = 100 and n = 2Then there would be 3 ways to express 100 as sum of squares of natural numbers.100 = 102 100 = 62 + 82 100 = 12 + 32 + 42 + 52 + 72This can be done easily by using recursion. We would start from 1 and go till the n-th root of the given number. In every run, we would subtract the n-th ... Read More

C++ program to find unique pairs such that each element is less than or equal to N

Ayush Gupta
Updated on 03-Oct-2019 11:17:31

81 Views

In this article, we will be discussing a program to find unique pairs of numbers having elements less than or equal to N and following some certain conditions −The square of difference between the two numbers must be equal to the LCM of those two numbers.The HCF of those two numbers can be represented as a product of any two consecutive numbers.The best approach to solve this problem would be to take two consecutive numbers (starting from 1) and finding the multiples of product of those numbers. Then among the multiples, to specify to one pair of numbers we need ... Read More

C++ program to find union and intersection of two unsorted arrays

Ayush Gupta
Updated on 03-Oct-2019 11:14:47

1K+ Views

In this article, we will be discussing a program to find the union and intersection of two given unsorted arrays.Let us denote the two arrays with ‘A’ and ‘B’. Then union of those arrays is denoted by A ∪ B which is basically an array of all the elements in both the given arrays; provided that each element repeats only once.To find this, we will create a separate array and copy down all the elements from the first array. Then we will traverse through the elements of the second array and check if it is already present in the union ... Read More

C++ program to find uncommon characters in two given strings

Ayush Gupta
Updated on 03-Oct-2019 11:11:58

686 Views

In this article, we will be discussing a program to find out the uncommon characters during comparison of two different given strings.As we know, strings are nothing but an array of characters. Therefore, for comparison we would be traversing through the characters of one string and simultaneously checking if that element exists in the other string.If we let the first string be A and the second string B.Then it would give us A - B. Similarly we can calculate B - A.Combining both of these results we would get( A - B ) ∪ ( B - A )i.e the ... Read More

C++ program to find two numbers with sum and product both same as N

Ayush Gupta
Updated on 03-Oct-2019 11:00:48

472 Views

In this tutorial, we will be discussing a program to find two numbers (say ‘a’ and ‘b’) such that botha+b = N and a*b = N are satisfied.Eliminating ‘a’ from both the equations, we get a quadratic equation in ‘b’ and ‘N’ i.eb2 - bN + N = 0This equation will have two roots which will give us the value of both ‘a’ and ‘b’. Using the determinant method to find the roots, we get the value of ‘a’ and ‘b’ as, $a= (N-\sqrt{N*N-4N)}/2\ b= (N+\sqrt{N*N-4N)}/2 $Example Live Demo#include //header file for the square root function #include using namespace ... Read More

Check if a number N starts with 1 in b-base in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:49:33

106 Views

We have a number N and a base b. In this program we have to check whether the number is starting with 1 in base b or not. Suppose a number 6 is given. In binary this is 110, so it starts with 1, in base 4 also it will be 124. Here also it starts with 1.As we know, if a number N is represented in base b, b gets converted to m+1 bit sequence bm bm-1 … b0. This implies bm bm + bm-1 * bm-1 + … + b0*b0 = N. The largest number will be 2*bm ... Read More

Check if a number is Quartan Prime or not in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:46:17

255 Views

Here we will see another program to check whether a number is Quartan Prime or not. Before dive into the logic, let us see what are the Quartan Prime numbers? The Quartan primes are prime numbers, that can be represented as x4 + y4. The x, y > 0.To detect a number is like that, we have to check whether the number is prime or not, if it is prime, then we will divide the number by 16, and if the remainder is 1, then that is Quartan prime number. Some Quartan prime numbers are {2, 17, 97, …}Example Live Demo#include ... Read More

Check if a number is Full Prime in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:37:39

246 Views

Here we will see, how to check, whether a number is full prime or not. A number is said to be a full prime, if it is prime, and all of its digits are also prime. Suppose a number is 37, this is full prime. But 97 is not full prime as 9 is not a prime number.One efficient approach is that; first we have to check whether any digit is present that is not prime. Digits must be in 0 to 9. In that range 2, 3, 5 and 7 are prime, others are not prime. If all are ... Read More

Check if a number is a Pythagorean Prime or not in C++

Arnab Chakraborty
Updated on 27-Sep-2019 10:34:40

770 Views

Here we will see another program to check whether a number is Pythagorean Prime or not. Before dive into the logic, let us see what are the Pythagorean Prime numbers? The Pythagorean primes are prime numbers, that can be represented as 4n + 1.To detect a number is like that, we have to check whether the number is prime or not, if it is prime, then we will divide the number by 4, and if the remainder is 1, then that is Pythagorean prime number. Some Pythagorean prime numbers are {5, 13, 17, 29, 37, 41, 53, …}Example Live Demo#include ... Read More

Advertisements