C++ Articles

Page 290 of 597

Aliquot Sequence in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 479 Views

Aliquot sequence is a special sequence of numbers. The sequence starts from the number itself and the next number of the sequence is the sum of the proper divisors of the previous terms.Let’s take an example of the sequence to learn the concept better −Input : 8 Output : 8 7 1 0 Explanation :    Proper divisors of 8 are 4, 2, 1. The sum is 7    Proper divisors of 7 are 1. The sum is 1    Proper divisors of 1 are 0. The sum is 0Perfect number is the number that has the aliquot sequence of ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 891 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

Check if a number can be expressed as 2^x + 2^y in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 249 Views

Here we will see, if we can represent a number as sum of two non-zero powers of 2. So we will check the given number N can be represented as (2x + 2y) where x, y > 0. Suppose a number is 10, this can be represented as 23 + 21.The approach is simple. There are two cases. If the number n is even, it can be represented as 2x. Where x > 0. Another case is that is N is odd, it can never be represented as sum of powers of 2. We cannot use power as 0, so ...

Read More

Arc length from given Angle in C++?

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 524 Views

An Angle is formed when two rays meet at a point. The point on the plane at which these rays meet is vertex.Arc of a circle is a portion of the circumference that is described by an angle.In this problem, we are given an angle of the circle. And we need to find the length of arc using the given diameter of the circle. For example, Input : Angle = 45° Diameter = 28 Output : Arc = 11ExplanationLength of arc = (circumference) X (angle/360°)= (π * d)*(angle/360°)To make a program that calculates the length of Arc from the given ...

Read More

Assign other value to a variable from two possible values in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 544 Views

Problem Statementwe have to assign a variable the value of other variables from two possible values without using any conditional operator.DescriptionIn this problem, we are given a variable let's say a which can have a value of any of the two variables x and y. Now, we have to create a program to assign the value of another than its current value without using any conditional operator i.e. we can’t check the value of x.Let’s take an example to understand the problem better −Input : a = 43 ; x = 43 and y = 21 Output : 21Explanation − ...

Read More

Balanced Prime in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 290 Views

Balanced prime number is a prime number that has the same difference for its previous and next prime numbers. i.e. it is the mean of the nearest next prime and previous prime.For a prime number to be a balanced prime, it should follow the following formula −Pn = (P(n-1) + P(n+1)) / 2Where n is the index of the prime number pn in the ordered set of a prime number.The ordered set of prime numbers: 2, 3, 5, 7, 11, 13, ….First, balanced primes are 5, 53, 157, 173 , …In this problem, we are given a number n and ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 660 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#include //header file for the square root function #include using namespace std; ...

Read More

Replace substring with another substring C++

Nitya Raut
Nitya Raut
Updated on 11-Mar-2026 2K+ Views

Here we will see how to replace substring with another substring. It replaces the portion of the string that begins at character pos and spans len characters.The structure of the replace function is like below:string& replace (size_t pos,  size_t len,  const string& str,  size_t subpos,  size_t sublen);The parameters are pos: It is an insertion point, str : It is a string object, len : It contains information about number of characters to erase.AlgorithmStep 1: Get the main string, and the string which will be replaced. And the match string Step 2: While the match string is present in the main string: Step 2.1: Replace it with the given ...

Read More

How to convert a std::string to const char* or char* in C++?

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 2K+ Views

In this section, we will see how to convert C++ string (std::string) to const char* or char*. These formats are C style strings. We have a function called c_str(). This will help us to do the task. It returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.Following is the declaration for std::string::c_str.const char* c_str() const;This function returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. If an exception is thrown, ...

Read More

Which one is better in between pass by value or pass by reference in C++?

Nishtha Thakur
Nishtha Thakur
Updated on 11-Mar-2026 624 Views

In C++ we can pass arguments into a function in different ways. These different ways are −Call by ValueCall by ReferenceCall by AddressSometimes the call by address is referred to as call by reference, but they are different in C++. Incall by address, we use pointer variables to send the exact memory address, but in call by reference we pass the reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer to get that effect.In this section, we will see what are the advantages of call by reference over call ...

Read More
Showing 2891–2900 of 5,962 articles
« Prev 1 288 289 290 291 292 597 Next »
Advertisements