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
Programming Articles
Page 29 of 2546
A 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 MorePrint the given pattern recursively
In C programming, we can create various patterns using recursive functions. A recursive function is one that calls itself repeatedly until a base condition is met. Here we'll learn to print a star pattern where each row contains an increasing number of stars. Syntax void printStars(int n); void printPattern(int n); Algorithm START Step 1 → function printStars(int n) If n > 0 printStars(n-1) Print * End IF End Step 2 → function printPattern(int n) ...
Read MorePrint values of 'a' in equation (a+b) <= n and a+b is divisible by x
Given an equation where we need to find values of 'a' such that a+b ≤ n and (a+b) is divisible by x. This problem involves finding all valid values of 'a' that satisfy both the sum constraint and divisibility condition. Syntax for (divisible = (b / x + 1) * x; divisible = 1) { // divisible - b gives us the value of 'a' } } Algorithm START Step 1 → Declare variables b=10, x=9, n=40 and flag=0, divisible Step ...
Read MorePrint n smallest elements from given array in their original order
Given an array of elements, the program must find the n smallest elements and display them in their original order of appearance in the array. Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Output : 1, 2, 3 Input k is 3 it means 3 smallest elements among the set needs to be displayed in original order like 1 than 2 and than 3 Syntax void findNSmallest(int arr[], int size, int k); Algorithm START Step 1 −> start variables as int i, max, pos, j, k=4 ...
Read MorePrint n terms of Newman-Conway Sequence
The Newman-Conway Sequence is a fascinating mathematical sequence that generates integers using a recursive formula. It starts with 1, 1 and each subsequent term is calculated based on previous values in the sequence. Syntax P(n) = P(P(n - 1)) + P(n - P(n - 1)) where P(1) = P(2) = 1 Algorithm The algorithm to generate n terms of Newman-Conway sequence is − START Step 1 → Input variable n (e.g. 20) Step 2 → Initialize variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 → Loop For i=3 and i
Read MorePrint the kth common factor of two numbers
Given two numbers x and y, we need to find their kth common factor. Common factors are the numbers that divide both x and y without leaving a remainder. Syntax int findKthCommonFactor(int x, int y, int k); Algorithm The approach to find the kth common factor is − Find the smaller of the two numbers (maximum possible common factor) Iterate from 1 to the smaller number Check if the current number divides both x and y Count common factors and return when we reach the kth factor Example Let's ...
Read MorePrint N lines of numbers such that every pair among numbers has a GCD K
In C programming, we can print N lines of numbers where every pair among the numbers has a specific GCD (Greatest Common Divisor). This involves generating sequences of numbers that maintain the required GCD property. What is GCD? GCD stands for Greatest Common Divisor of two or more integers (excluding 0). It is the largest positive integer that divides all the given numbers without remainder. For example, to find the GCD of 48 and 180 − 48 = 2 × 2 × 2 × 2 × 3 180 = 2 × 2 × 3 × ...
Read MorePrint the nearest prime number formed by adding prime numbers to N
Given a number N, if it is not prime, the task is to find the nearest prime number by repeatedly adding prime numbers (starting from 2) to N until we get a prime result. Input: N=6 Output: 11 Explanation Since 6 is not prime, we add the first prime number 2 to get 6+2=8. Since 8 is also not prime, we add the next prime number 3 to get 8+3=11. Since 11 is prime, the output is 11. Syntax int isPrime(int n); int findNearestPrime(int n); Algorithm Check if ...
Read MorePrint Reverse a linked list using Stack
Given a linked list, the program must print the list starting from the end till the front using the stack data structure. This approach leverages the LIFO (Last In First Out) property of stacks to reverse the order of elements. Input : 10 -> 5 -> 3 -> 1 -> 7 -> 9 Output: 9 -> 7 -> 1 -> 3 -> 5 -> 10 Syntax struct Node { int data; struct Node* next; }; // Stack operations void push_to_stack(int stack[], int* top, int value); ...
Read More