Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by sudhir sharma
Page 18 of 98
C/C++ Program for the Odd-Even Sort (Brick Sort)?
The odd-even sort, also known as brick sort, is a comparison-based sorting algorithm that divides the sorting process into two alternating phases: odd phase and even phase. This technique is similar to bubble sort but operates on different index pairs in each phase, making it suitable for parallel processing. The odd phase compares and swaps elements at odd indices (1, 3, 5...) with their adjacent elements, while the even phase works on even indices (0, 2, 4...) with their adjacent elements. Syntax void oddEvenSort(int arr[], int n); How It Works The algorithm alternates ...
Read MoreC/C++ Program for Number of solutions to Modular Equations?
We have a given number of coins and we need to arrange them to form a pyramid of maximum height. The coins are arranged such that the first row contains 1 coin, the second row contains 2 coins, the third row contains 3 coins, and so on. 1 2 3 4 5 ...
Read MoreAdd minimum number to an array so that the sum becomes even in C programming
Given an array, we need to add the minimum positive number to make the sum of all array elements even. The key insight is that we only need to analyze the parity (odd/even nature) of numbers to determine the minimum addition required. Syntax int findMinimumToAdd(int arr[], int n); Method 1: Calculate Total Sum Calculate the sum of all elements in the array, then check if the sum is even. If the sum is already even, add 2 (minimum positive even number). If the sum is odd, add 1 to make it even − ...
Read MoreArithmetic Mean in C programming
Arithmetic mean is the sum of a collection of numbers divided by the number of numbers in the collection. It is one of the most commonly used measures of central tendency in statistics and mathematics. Syntax mean = (sum of all numbers) / (count of numbers) Basic Properties of Arithmetic Mean If each observation is increased by p, the mean increases by p. If each observation is decreased by p, the mean decreases by p. If each observation is multiplied by p, the mean is multiplied by p. If each observation is divided ...
Read MoreC/C++ Program for nth Catalan Number?
Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects. The nth Catalan number can be calculated using the recursive formula or dynamic programming approaches. Syntax C(n) = (2n)! / ((n+1)! * n!) C(n) = C(0)*C(n-1) + C(1)*C(n-2) + ... + C(n-1)*C(0) Mathematical Properties Catalan numbers have several interpretations − Cn is the number of Dyck words of length 2n (strings with n X's and n Y's where no prefix has more Y's than X's) Cn counts valid parentheses combinations with n pairs Cn ...
Read MoreC Program to Multiply two Floating Point Numbers?
In C, multiplying two floating-point numbers is a fundamental arithmetic operation. Floating-point numbers can represent real numbers with decimal points, such as 4320.0, -3.33, or 0.01226. The term "floating point" refers to the fact that the decimal point can "float" to support a variable number of digits before and after it. Syntax float result = float_num1 * float_num2; double result = double_num1 * double_num2; Floating Point Data Types Type Size Range Precision float 4 bytes ±1.18 x 10-38 to ±3.4 x 1038 6-7 digits double ...
Read MoreC Program to Check if a Given String is a Palindrome?
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward. Words such as "madam" or "racecar" or the number "10801" are palindromes. To check if a string is a palindrome, we need to compare characters from both ends moving toward the center. If the first character matches the last, second matches second-last, and so on, then the string is a palindrome. Syntax int isPalindrome(char str[]); // Returns 1 if palindrome, 0 otherwise Example 1: Using Character Comparison This approach compares characters from both ends ...
Read MoreWrite a program to Delete a Tree in C programming
To delete a tree in C programming, we need to traverse each node and free the memory allocated to them. The key is to delete nodes in the correct order − children must be deleted before their parents to avoid memory leaks and dangling pointers. Post-order traversal is ideal for this operation as it visits children before the parent node. Syntax void deleteTree(struct node* root) { if (root == NULL) return; deleteTree(root->left); deleteTree(root->right); free(root); } How Post-order Traversal Works ...
Read MoreWrite a function that returns 2 for input 1 and returns 1 for 2 in C programming
A function that returns 2 for input 1 and 1 for input 2 can be implemented using various approaches. This is a common programming exercise that demonstrates different logical and mathematical techniques for value swapping between two specific numbers. Syntax int functionName(int x); Method 1: Using Conditional Statement The simplest approach uses an if-else statement to check the input value and return the corresponding output − #include int reverseif(int x) { if (x == 1) return 2; ...
Read MoreWrite a C program to print " Tutorials Point " without using a semicolon
In C programming, semicolons are statement terminators that mark the end of each statement. However, it's possible to print text without using semicolons by leveraging control structures and the return value of printf(). Syntax int printf(const char *format, ...); The printf() function returns an integer representing the number of characters printed. This return value can be used in conditional expressions within control structures that don't require semicolons. Method 1: Using if Statement The if statement can evaluate the return value of printf() without requiring a semicolon − #include int ...
Read More