C++ Articles

Page 8 of 597

C/C++ Program for Finding the vertex, focus and directrix of a parabola?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 233 Views

A parabola is a U-shaped curve that can be described by a quadratic equation. In C programming, we can find key properties of a parabola including its vertex, focus, and directrix using mathematical formulas. The general equation of a parabola is − Syntax y = ax² + bx + c Where a, b, and c are coefficients and a ≠ 0. ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 523 Views

An array of integers is given. We have to find the sum of all contiguous elements whose sum is largest. This problem is known as the Maximum Subarray Problem and is efficiently solved using Kadane's Algorithm. Using dynamic programming, we store the maximum sum up to the current element. This helps find the optimal contiguous subarray with maximum sum. Syntax int maxSubarraySum(int arr[], int n); Algorithm: Kadane's Algorithm The algorithm maintains two variables − Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of ...

Read More

C/C++ program to shutdown a system?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 882 Views

Here we will see how we can shut down the system by writing a simple C program. The shutdown process varies in different operating systems. If we are Linux user, we can use this terminal command to shut down − shutdown -P now If we are using Windows system, we can use this command − c:\windows\system32\shutdown /i Note: These programs require administrator/root privileges to execute successfully. On Linux, you may need to run with sudo. On Windows, run the program as administrator. Syntax int system(const char *command); ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 1K+ Views

Here we will see how we can count the number of set bits in an integer. Set bits are the bits that have a value of 1 in the binary representation of a number. For example, the number 13 has binary representation 1101, which contains three set bits, so the count will be 3. To solve this problem, we will shift the number to the right and check if the least significant bit (LSB) is 1. If it is, we increment our count. This process continues until the number becomes 0. Syntax int countSetBits(int n); ...

Read More

C/C++ Program to Count Inversions in an array using Merge Sort?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 974 Views

The inversion count in an array indicates how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions. When the array is sorted in reverse order, the number of inversions is maximum. We can efficiently solve this problem using a modified merge sort algorithm to achieve O(n log n) time complexity. Syntax int merge(int arr[], int temp[], int left, int mid, int right); int mergeSort(int arr[], int temp[], int left, int right); int countInversions(int arr[], int n); Algorithm The approach uses a ...

Read More

C/C++ Program for Triangular Matchstick Number?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 190 Views

Here we will see how to count number of matchsticks required to make a triangular pyramid. The base of the pyramid is given. So if the base is 1, it will take 3 matchsticks to make a pyramid, for base 2, 9 matchsticks are needed, for base size 3, it will take 18 matchsticks. Base = 1 3 matchsticks Base = 2 9 matchsticks ...

Read More

C/C++ Program for Maximum height when coins are arranged in a triangle?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 675 Views

In this section, we will see one interesting problem. There are N coins, and we have to find the maximum height we can make if we arrange the coins as a pyramid. In this fashion, the first row will hold 1 coin, the second will hold 2 coins, and so on. Coin Pyramid 1 1 1 1 ...

Read More

c32rtomb() function in C/C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 197 Views

In C programming, the c32rtomb() function is used to convert a 32-bit wide character (char32_t) to its corresponding multibyte character representation. This function is defined in the uchar.h header file and is part of the C11 standard for Unicode support. Syntax size_t c32rtomb(char *s, char32_t c32, mbstate_t *ps); Parameters s − Pointer to a character array where the multibyte character will be stored c32 − The 32-bit wide character to convert ps − Pointer to an mbstate_t object that holds the conversion state Return Value The function returns the number ...

Read More

Power Function in C/C++

Sharon Christine
Sharon Christine
Updated on 15-Mar-2026 1K+ Views

The power function in C is used to calculate the power of a given number. The pow() function finds the value of a raised to the power b i.e., ab. Syntax double pow(double base, double exponent); The pow() function accepts two double values as parameters and returns a double value as output. It is defined in the math.h header file. If you pass integers to the power function, they are automatically converted to double data type. However, there's a potential precision issue with this conversion. Sometimes floating-point representation might store values slightly differently (e.g., ...

Read More

Why do we check for a NULL pointer before deleting in C/C++?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 576 Views

In C programming, checking for a NULL pointer before calling free() is often seen in code, but it's technically unnecessary. The free() function is designed to safely handle NULL pointers by doing nothing when passed a NULL value. Syntax void free(void *ptr); Why NULL Check is Unnecessary According to the C standard, free(NULL) is guaranteed to be a no-op (no operation). This means the following code is redundant − #include #include int main() { int *ptr = NULL; ...

Read More
Showing 71–80 of 5,962 articles
« Prev 1 6 7 8 9 10 597 Next »
Advertisements