Found 7197 Articles for C++

5 Different methods to find length of a string in C++?

Arnab Chakraborty
Updated on 05-Dec-2024 13:57:43

11K+ Views

Here we will see five different ways to get the string lengths in C++. In C++ we can use the traditional character array string, and C++ also has String class. In different area there are different methods of calculating the string lengths. The C++ String class has length() and size() function. These can be used to get the length of a string type object. To get the length of the traditional C like strings, we can use the strlen() function. That is present under the cstring header file. Another two approaches are straight forward. One by using the while loop, ... Read More

C/C++ Program for Largest Sum Contiguous Subarray?

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

458 Views

An array of integers is given. We have to find sum of all elements which are contiguous. Whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find sum for contiguous elements in the array.Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is : 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := 1 to n-1, ... Read More

BFS using STL for competitive coding in C++?

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

672 Views

The Breadth First Search (BFS) traversal is an algorithm, which is used to visit all of the nodes of a given graph. In this traversal algorithm one node is selected and then all of the adjacent nodes are visited one by one. After completing all of the adjacent vertices, it moves further to check another vertices and checks its adjacent vertices again.In The competitive coding, we have to solve problems very quickly. We will use the STL (Standard Library of C++) to implement this algorithm, we need to use the Queue data structure. All the adjacent vertices are added into ... Read More

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

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

167 Views

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

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

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

166 Views

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

Arithmetic Mean in c++?

Ravi Ranjan
Updated on 25-Jul-2025 17:07:37

2K+ Views

An arithmetic mean is the average of all the given numbers, which is calculated by summing all the numbers and dividing this calculated sum by the total number of elements. The formula for calculating the arithmetic mean is: $$ \bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i = \frac{x_1 + x_2 + x_3 + \cdots + x_n}{n} $$ Here, we are given an array of integers and our task is to calculate the arithmetic mean of these numbers using the above formula: Scenario 1 Input: num = 2, 7, 4, ... Read More

Alternate Primes till N in C++?

Nishu Kumari
Updated on 05-Aug-2025 17:05:25

2K+ Views

A prime number is a number greater than 1 that is divisible only by 1 and itself. Given a number N, our task is to print all alternate prime numbers up to N. This means we skip every second prime number and only print the 1st position, 3rd position, 5th position, and so on. Let's look at the example scenarios to understand the problem clearly: Scenario 1 Input: N = 15 Output: 2 5 11 Explanation: Prime numbers up to 15 are: 2, 3, 5, 7, 11, 13 Taking alternate primes (1st, 3rd, 5th): 2, 5, 11 Scenario 2 ... Read More

Aliquot sum in C++?

Manisha Chand
Updated on 31-Jul-2025 14:59:55

298 Views

Aliquot sum in C++ Aliquot sum of a positive integer n is the sum of all proper divisors of n. A positive proper divisor is a divisor of a number, excluding the number itself. For example, for n = 6; 1, 2, 3 are positive proper divisors but 6 itself is not. Scenario 1 Input: n = 20 Output: 22 Explanation: Proper divisors of 20 are: 1, 2, 4, 5, 10 Sum of proper divisors are: 1 + 2 + 4 + 5 + 10 = 22 So, aliquot sum of 20 is : 22 Scenario 2 Input: 15 ... Read More

C/C++ program to shutdown a system?

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

778 Views

Here we will see how we can shut down the system by writing a simple C or C++ code. The shutdown process varies in different OS. If we are Linux user, we can use this terminal command to shut down.shutdown –P nowIf we are using Windows system, we can use this command −c:\windows\system32\shutdown /iWe will see the code for Linux and WindowsExample(Linux)#include using namespace std; int main() {    system("shutdown -P now"); }Example(Windows)#include using namespace std; int main() {    system("c:\windows\system32\shutdown /i "); }

C/C++ Program to Count set bits in an integer?

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

1K+ Views

Here we will see how we can check number of set bits in an integer number. The set bits are 1’s in the binary representation of a number. For an example the number 13 has three set bits 1101. So the count will be 3.To solve this problem, we will shift the number to the right, and if the LSb is 1, then increase count. Until the number becomes 0, it will run.AlgorithmcountSetBit()begin    count := 0    while count is not 0, do       if LSb of n is set, then          count := ... Read More

Advertisements