Found 7347 Articles for C++

Check if a number is magic (Recursive sum of digits is 1) in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:46:25

2K+ Views

Here we will see one program, that can check whether a number is magic number or not. A number is said to be magic number, when the recursive sum of the digits is 1. Suppose a number is like 50311 = 5 + 0 + 3 + 1 + 1 = 10 = 1 + 0 = 1, this is magic number.To check whether a number is magic or not, we have to add the digits until a single-digit number is reached.Example Live Demo#include using namespace std; int isMagicNumber(int n) {    int digit_sum = 0;    while (n > ... Read More

Check for integer overflow on multiplication in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:25:08

1K+ Views

Suppose we want to find the result after multiplying two numbers A and B. We have to check whether the multiplied value will exceed the 64-bit integer or not. If we multiply 100, and 200, it will not exceed, if we multiply 10000000000 and -10000000000, it will overflow.To check this, we have to follow some steps. These are like below −Steps −If anyone of the numbers is 0, then it will not exceedOtherwise, if the product of two divided by one equals to the other, then it will not exceedFor some other cases, it will exceed.Example Live Demo#include #include ... Read More

Find two numbers whose sum and GCD are given in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:21:57

143 Views

We have the sum and gcd of two numbers a and b. We have to find both numbers a and b. If that is not possible, return -1. Suppose the sum is 6 and gcd is 2, then the numbers are 4 and 2.The approach is like, as the GCD is given, then it is known that the numbers will be multiples of it. Now there following stepsIf we choose the first number as GCD, then the second one will be sum − GCDIf the sum of the numbers is chosen in the previous step is the same as the ... Read More

Find the resulting Colour Combination in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:16:57

230 Views

We have a string with three colors (G, B, Y). We have to find the resulting color based on these relations −B * G = YY * B = GG * Y = BSuppose the string is “GBYGB” is B. If the string is “BYB”, then it will be Y.The approach is simple; we will take the string. Compare each alphabet with adjacent characters, using the given condition, find the color.Example Live Demo#include using namespace std; char combination(string s) {    char color = s[0];    for (int i = 1; i < s.length(); i++) {       ... Read More

Find the other number when LCM and HCF given in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:09:36

58 Views

Suppose we have a number A, and LCM and GCD values, we have to find another number B. If A = 5, LCM is 25, HCF = 4, then another number will be 4. We know that −$$𝐴∗𝐵=𝐿𝐶𝑀∗𝐻𝐶𝐹$$$$𝐵= \frac{LCM*HCF}{A}$$Example Live Demo#include using namespace std; int anotherNumber(int A, int LCM, int GCD) {    return (LCM * GCD) / A; } int main() {    int A = 5, LCM = 25, GCD = 4;    cout

Find Surpasser Count of each element in array in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:07:00

106 Views

Suppose one array A is given. We have to find a number of surpasser of each element in that array. The surpassers are greater elements which are present at the right side of the array of the current element. Suppose A = {2, 7, 5, 3, 0, 8, 1}, the surpassers are {4, 1, 1, 1, 2, 0, 0}, so 2 has 4 numbers at right side, which are greater than 4, and the same rule for others. The solution is very simple, two nested loops will be there, for each element, it will count surpassers, then store them into ... Read More

Find nth term of the Dragon Curve Sequence in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:04:02

149 Views

Here we will see a program, that can find nth term of the Dragon Curve sequence. The Dragon curve sequence is an infinite binary sequence. It starts with 1, and in each step, it alternatively adds 1s and 0s before and after each element of the previous term, to form the next term.Term 1 : 1Term 2 : 110Term 3 : 1101100Term 4 : 110110011100100We will start with 1, then add 1 and 0, alternatively after each element of the preceding term. When the new term obtained becomes the current term, then repeat the steps from 1 to n to ... Read More

Find LCM of rational number in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:59:16

107 Views

Here we will see how to find the LCM of Rational numbers. We have a list of rational numbers. Suppose the list is like {2/7, 3/14, 5/3}, then the LCM will be 30/1.To solve this problem, we have to calculate LCM of all numerators, then gcd of all denominators, then the LCM of rational numbers, will be like −$$LCM =\frac{LCM\:of\:all\:𝑛𝑢𝑚𝑒𝑟𝑎𝑡𝑜𝑟𝑠}{GCD\:of\:all\:𝑑𝑒𝑛𝑜𝑚𝑖𝑛𝑎𝑡𝑜𝑟𝑠}$$Example Live Demo#include #include #include using namespace std; int LCM(int a, int b) {    return (a * b) / (__gcd(a, b)); } int numeratorLCM(vector vect) {    int result = vect[0].first;    for (int i = 1; i ... Read More

Find if nCr is divisible by the given prime in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:52:50

93 Views

Suppose there are three variables N, R and P. The N and R are used to get the NCR and P is a prime. We have to find whether NCR is divisible by P. Suppose we have some numbers N = 7, R = 2 and P = 3, then 7C2 = 21, this is divisible by 3, so the output will be true.We know that NCR = N! / (R! * (N – R)! ). We will use Legendre Formula to largest power of P, which divides any N!, R! and (N – R)! in order to NCR to ... Read More

Find if a point lies inside a Circle in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:46:31

2K+ Views

Suppose, one circle is given (the center coordinate and radius), another point is also given. We have to find whether the point is inside the circle or not. To solve it, we have to find the distance of the given point from circle center. If that distance is less or equal to the radius, then that is inside the circle, otherwise not.Example Live Demo#include #include using namespace std; bool isInsideCircle(int cx, int cy, int r, int x, int y) {    int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);    if ( dist

Advertisements