C++ Articles

Page 277 of 597

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 526 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 264 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 628 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 604 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

C++ Program to Find Deepest Left Leaf in a Binary Tree

Samual Sam
Samual Sam
Updated on 11-Mar-2026 248 Views

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to Find Deepest Left Leaf in a Binary TreeAlgorithmBegin.    function deepestLLeafutil() find the deepest left leaf in a given    binary tree:         lvel is level of current node.       maxlvel is pointer to the deepest left leaf node found so far         isLeft Indicates that this node is left child of its parent         resPtr is Pointer to the result         If root is equal to Null ...

Read More

C++ Program to Find Lowest Common Ancestor in a Binary Search Tree

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 585 Views

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to find the lowest common ancestor in a Binary Tree.AlgorithmBegin Create a structure n to declare data d, a left child pointer l and a right child pointer r.    Create a function to create newnode. Call a function LCA() to Find lowest common ancestor in a binary tree:    Assume node n1 and n2 present in the tree.    If root is null, then return.       If root is not null there are two cases.   ...

Read More

exit() vs _Exit() function in C and C++

George John
George John
Updated on 11-Mar-2026 507 Views

In this section we will see what are the differences between exit() and _Exit() in C and C++. In C the exit() terminates the calling process without executing the remaining code which is present after exit() function.In C++11, one new function is present called _Exit(). So what is the feature of this function? The exit() function performs some cleaning before terminating the program. It clears the connection termination, buffer flushes etc. This _Exit() function does not clean anything. If we test using atexit() method, it will not work.Let us see two examples where at first we are using exit() function, ...

Read More

C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree

Samual Sam
Samual Sam
Updated on 11-Mar-2026 207 Views

This is a C++ Program to Find Size of the Largest Independent Set (LIS) in a Given a Binary Tree.AlgorithmBegin.    Create a structure n to declare data d, a left child pointer l and a right child pointer r.    Call a function max() to return maximum between two integers. Create a function LIS() to return the    size of the largest independent set in a given binary tree.    Calculate size excluding the current node    int size_excl = LIS(root->l) + LIS(root->r)    Calculate size including the current node    int size_incl = 1;    if (root->l)   ...

Read More
Showing 2761–2770 of 5,961 articles
« Prev 1 275 276 277 278 279 597 Next »
Advertisements