Programming Articles

Page 981 of 2547

Sum of squares of the first n even numbers in C Program

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 2K+ Views

The sum of squares of the first n even numbers means we calculate the square of each even number (2, 4, 6, 8, ..., 2n) and add them together. For example, for n=3, we compute 2² + 4² + 6² = 4 + 16 + 36 = 56. There are two methods to find the sum of squares of the first n even numbers − Syntax // Using loop approach sum = 0; for (i = 1; i

Read More

A C Puzzle in C Programming?

Akansha Kumari
Akansha Kumari
Updated on 15-Mar-2026 2K+ Views

C programming puzzles are small but tricky coding problems designed to challenge and enhance understanding of the C programming language through creative solutions. Problem Statement We are given two numbers, and our task is to combine them in such a way that the second integer is placed before the first integer, resulting in a single combined integer. However, we are not allowed to use any logical, arithmetic, string-related operations, or any pre-defined functions. Consider the following input/output scenarios to understand the puzzle statement better − Scenario 1 Input: 12, 54 Output: 5412 ...

Read More

A Boolean Array Puzzle in C?

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 315 Views

This is an array-based puzzle that requires you to change all the numbers of an array that contains two elements to 0. One element of the array is 0 and the other may or may not be 0. To solve this puzzle, the program needs to find the non-zero element and change it to 0. There are the following constraints that are needed to solve the boolean array puzzle − Allowed operation is complement, other operations are not allowed. Loops and conditional statements are not allowed. Direct assignment is also not allowed. Syntax ...

Read More

Abundant Number in C ?

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 3K+ Views

An abundant number (also known as excessive number) is a positive integer that is smaller than the sum of all its proper divisors. Proper divisors are all the positive divisors of a number except the number itself. For example, 12 is an abundant number because its proper divisors are 1, 2, 3, 4, and 6, and their sum (1+2+3+4+6 = 16) is greater than 12. The difference between the sum of proper divisors and the number is called abundance. For the above example, abundance = 16 − 12 = 4. Syntax // Check if number is ...

Read More

A shorthand array notation in C for repeated values?

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 245 Views

An array stores multiple values of the same data type. Sometimes you need to initialize several array elements with the same value, such as setting multiple positions to 3, 3, 3, 3. C provides a shorthand notation called designated initializers to simplify this process and reduce code repetition. Syntax [startIndex ... endIndex] value Where startIndex and endIndex define the range of positions to initialize with the specified value. Example: Array Initialization with Repeated Values This example demonstrates how to use the shorthand notation to initialize array elements with repeated values − ...

Read More

A modified game of Nim in C ?

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 752 Views

The modified game of Nim is a strategic game played on an array where two players take turns removing elements based on divisibility rules. This game determines the winner based on optimal play and starting advantage. Game Rules In this modified Nim game − Player A removes elements divisible by 3 Player B removes elements divisible by 5 Elements divisible by both 3 and 5 (i.e., divisible by 15) can be removed by either player Player A moves first The last player to make a move wins Syntax // Count moves for ...

Read More

0-1 Knapsack Problem in C?

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 27K+ Views

The 0-1 Knapsack Problem is a classic dynamic programming problem where we have a knapsack with a fixed capacity and a set of items, each with a weight and value. The goal is to maximize the total value of items in the knapsack without exceeding its weight capacity. In the 0-1 variant, each item can either be included (1) or excluded (0) − no fractional items are allowed. Syntax int knapsack(int capacity, int weights[], int values[], int n); Sample Problem Items: 3 Values: {20, 25, 40} Weights: {25, 20, 30} Knapsack Capacity: ...

Read More

Program to calculate Bitonicity of an Array

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 165 Views

The bitonicity of an array is a measure that indicates how much the array deviates from a monotonic sequence. It starts at 0 and increases by 1 when an element is greater than the previous one, decreases by 1 when smaller, and remains unchanged when equal. Syntax int calculateBitonicity(int arr[], int n); Algorithm The bitonicity calculation follows this logic − Bitonicity = 0 (initially) For i from 1 to n-1: if arr[i] > arr[i-1]: Bitonicity = Bitonicity + 1 if arr[i] < arr[i-1]: Bitonicity ...

Read More

Program to build DFA that starts and ends with 'a' from the input (a, b)

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 1K+ Views

DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or rejects a string based on its transition rules and final states. Here, we are going to make a DFA that accepts a string that starts and ends with 'a'. The input alphabet is from the set {a, b}. Let's discuss some valid and invalid cases that are accepted by this DFA. Strings accepted by DFA: "ababba", "aabba", "aa", "a" Strings not accepted by DFA: "ab", "b", "aabab" Syntax // Check if string starts and ends with 'a' if (str[0] ...

Read More

Binary array after M range toggle operations?

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

In C, we can solve the binary array toggle problem by applying range operations on an initially zero-filled array. Given an array of size n (initially all zeros) and M toggle commands, each command toggles all bits in a specified range [a, b]. This problem demonstrates bit manipulation using the XOR operation. Syntax void toggleCommand(int arr[], int start, int end); // Toggles bits from index start to end (inclusive) Algorithm toggleCommand(arr, a, b) Begin for each element e from index a to b, do ...

Read More
Showing 9801–9810 of 25,466 articles
« Prev 1 979 980 981 982 983 2547 Next »
Advertisements