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 16 of 98
C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
This program finds the sum of a mathematical series where the n-th term is defined as Tn = n2 - (n-1)2. We need to calculate the sum Sn = T1 + T2 + T3 + ... + Tn modulo (109 + 7). Syntax result = ((n % mod) * (n % mod)) % mod; Mathematical Derivation First, let's simplify the term Tn − Tn = n2 - (n-1)2 Tn = n2 - (n2 - 2n + 1) Tn = n2 - n2 + 2n - 1 Tn = 2n - 1 ...
Read MoreAdd n binary strings?
In C programming, adding n binary strings involves performing binary addition on multiple binary numbers represented as strings. We need to add all binary strings together to produce a single binary result. The approach uses binary addition logic with carry propagation, adding all strings one by one to get the final result. Syntax char* addBinaryStrings(char* binary1, char* binary2); char* addNBinaryStrings(char** binaryArray, int n); Example: Adding Multiple Binary Strings This example demonstrates adding three binary strings using a helper function that adds two binary strings at a time − #include #include ...
Read MoreAdd all greater values to every node in a given BST?
In this problem, we need to modify each node in a Binary Search Tree (BST) by adding the sum of all greater node values to the current node's value. This transforms the BST while maintaining its structure but changing the node values. Syntax void addGreaterValues(struct Node* root, int* sum); Problem Statement Given a Binary Search Tree, we need to add to each node the sum of all node values that are greater than the current node value. Input BST 10 5 ...
Read MoreAdd 1 to a number represented as a linked list?
The linked list representation of a number stores each digit in a separate node, where the first node contains the most significant digit and the last node contains the least significant digit. For example, the number 202345 is represented as (2→0→2→3→4→5). To add 1 to this linked list represented number, we need to handle carry propagation from the least significant digit. If the last digit is less than 9, we simply increment it. Otherwise, we propagate the carry to the next digits. Syntax struct Node* addOne(struct Node* head); Algorithm The algorithm follows these ...
Read MoreA Number Link Game?
Number Link is a logic puzzle where players must connect matching numbers on a grid using continuous paths. The paths cannot cross or branch, and each number must be at the endpoint of exactly one path. This implementation generates a random Number Link puzzle and its solution using a union-find data structure. Syntax struct _node { struct _node *parent; int rank; int path_number; int endpoint; }; Algorithm Overview The algorithm works by generating random paths first, then creating ...
Read MoreC program to calculate the value of nPr?
Permutations, nPr can also be represented as P(n, r), is a mathematical formula to find the number of ways to arrange r objects from n objects where order matters. The formula of P(n, r) is n! / (n − r)!. The number of permutations on a set of n elements is given by n! where "!" represents factorial. Syntax nPr = n! / (n - r)! Where: n is the total number of objects r is the number of objects to be selected ! denotes factorial Example Let's calculate P(5, ...
Read MoreC program to Find the Largest Number Among Three Numbers
This program takes three numbers and finds the largest among them. We will use conditional statements to compare the numbers and determine which one has the maximum value. Syntax if (condition1) { if (condition2) { // statements } else { // statements } } else { if (condition3) { // statements } else ...
Read MoreC Program for the compound interest?
Compound interest is the interest calculated on both the principal amount and the previously earned interest. Unlike simple interest, compound interest grows exponentially because the interest earned in each period is added to the principal for the next calculation period. Syntax Compound Interest = Principal * pow((1 + Rate/100), Time) - Principal Amount = Principal * pow((1 + Rate/100), Time) Example: Calculate Compound Interest Here's a complete C program to calculate compound interest using the mathematical formula − #include #include int main() { float principal, ...
Read MoreC/C++ Program for the n-th Fibonacci number?
The Fibonacci sequence is a series where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. In this problem, we will find the nth number in the Fibonacci series. For this we will calculate all the numbers and print the n terms. Syntax // Basic approach using loop for (i = 1; i
Read More10's Complement of a decimal number?
The 10's complement of a decimal number is a mathematical operation used in digital systems to simplify arithmetic operations, particularly subtraction. It is calculated by subtracting the given number from 10n, where n is the total number of digits in the number. The 10's complement can be found in two ways − Method 1: Calculate 9's complement first, then add 1 Method 2: Direct calculation using formula: 10n - number Syntax int tensComplement = pow(10, digitCount) - number; Method 1: Using 9's Complement First find the 9's complement by subtracting ...
Read More