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 Arnab Chakraborty
Page 182 of 377
A Puzzle on C/C++ R-Value Expressions?
Here we will see one puzzle about R-value expressions in C. Suppose there is a program which is given below, we have to tell what will be the output and why? Syntax ~expression; // R-value: computes but doesn't store variable = ~expression; // L-value: stores the result Example 1: R-value Expression In this example, the complement operation is performed but not assigned to any variable − #include int main() { int x = 0xab; ~x; /* ...
Read MoreA Pancake Sorting Problem?
Pancake sorting is a unique sorting algorithm that mimics the process of sorting a stack of pancakes by flipping portions of the stack. In this algorithm, we can only use one operation: rev(arr, i), which reverses elements from index 0 to index i. The goal is to sort the entire array using only this flip operation. The algorithm works similarly to selection sort − we repeatedly find the largest element and move it to its correct position at the end of the current unsorted portion. Syntax void rev(int arr[], int i); int maxIndex(int arr[], int n); ...
Read MoreA nested loop puzzle?
In this section we will see one interesting nested loop performance puzzle. We will analyze two code segments with different loop arrangements to understand which one runs faster, assuming the compiler does not optimize the code. The Problem Both code segments execute the same total number of iterations (10 × 100 = 1000), but their performance characteristics differ due to loop overhead − Segment 1: Outer Loop with Fewer Iterations #include int main() { int count = 0; for(int i ...
Read MoreA matrix probability question ?
This problem calculates the probability of staying within a matrix after N moves from a given position. From any cell, we can move in four directions (left, right, up, down) with equal probability (0.25 each). If we cross the matrix boundary, the probability becomes 0. Syntax double matProb(int m, int n, int x, int y, int N); int isSafe(int x, int y, int m, int n); Parameters m, n − Matrix dimensions (m rows, n columns) x, y − Starting position coordinates N − Number of moves to make Algorithm ...
Read MoreC Program for Find largest prime factor of a number?
In this section, we will see how we can get the largest prime factor of a number in an efficient way. There is a number say n = 1092, we have to get the largest prime factor of this. The prime factors of 1092 are 2, 2, 3, 7, 13. So the largest is 13. To solve this problem, we have to follow this rule − When the number is divisible by 2, then store 2 as largest, and divide the number by 2 repeatedly. Now the number must be odd. Now starting from 3 to square root ...
Read MoreC Program for Extended Euclidean algorithms?
The Extended Euclidean Algorithm is used to find the greatest common divisor (GCD) of two integers along with the coefficients x and y such that − ax + by = gcd(a, b) This algorithm extends the standard Euclidean algorithm by not only computing the GCD but also finding the linear combination coefficients. It uses the recursive relation gcd(a, b) = gcd(b mod a, a) while keeping track of the coefficients. Syntax int extendedGCD(int a, int b, int* x, int* y); Algorithm ExtendedEuclidean(a, b, x, y) begin ...
Read MoreC Program for efficiently print all prime factors of a given number?
In this section, we will see how we can get all the prime factors of a number in an efficient way. There is a number say n = 1092, we have to get all prime factors of this. The prime factors of 1092 are 2, 2, 3, 7, 13. To solve this problem, we have to follow this rule − When the number is divisible by 2, then print 2, and divide the number by 2 repeatedly. Now the number must be odd. Now starting from 3 to square root of the ...
Read MoreC Program for Difference between sums of odd and even digits?
In C, we can find the difference between the sum of digits at odd positions and even positions in a number. The positions are counted from left to right starting at index 0. If this difference is zero, it indicates a special mathematical property. For example, in the number 156486: Even positions (0, 2, 4): 1 + 6 + 8 = 15 Odd positions (1, 3, 5): 5 + 4 + 6 = 15 Difference: 15 − 15 = 0 Syntax int checkDifference(int number); // Returns 1 if difference is zero, 0 otherwise ...
Read MoreC Program for cube sum of first n natural numbers?
In this problem we will see how we can get the sum of cubes of first n natural numbers. Here we are using one for loop that runs from 1 to n. In each step we are calculating cube of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use the mathematical formula. Syntax sum = 1³ + 2³ + 3³ + ... + n³ sum = [n(n+1)/2]² Method 1: Using Loop (O(n) Time) ...
Read MoreBash program to find A to the power B?
In C programming, calculating A to the power B can be done using the built-in pow() function from the math library, or by implementing custom algorithms. The pow() function provides an easy way to compute powers of numbers. Syntax #include double pow(double base, double exponent); Method 1: Using pow() Function The pow() function from math.h calculates base raised to the power of exponent − Note: When using pow() function, you need to link the math library by adding -lm flag during compilation: gcc program.c -lm #include #include ...
Read More