C Articles

Page 42 of 96

C Program for Minimum number of jumps to reach the end

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

The minimum jumps problem involves finding the minimum number of jumps needed to reach the last index of an array from the first index. Each element in the array represents the maximum number of steps that can be taken forward from that position. Syntax int minJumps(int arr[], int n); Algorithm The naive approach starts from the first element and recursively calculates the minimum jumps needed − minJumps(start, end) = Min(minJumps(k, end)) for all k accessible from start We use dynamic programming with bottom-up approach. For each position, we check all ...

Read More

C Program for Minimum Cost Path

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

The minimum cost path problem involves finding the path from the top-left corner to the bottom-right corner of a 2D matrix with minimum total cost. You can only move right or down from any given cell. Dynamic programming provides an efficient solution to this problem. Syntax int minCostPath(int cost[][COLS], int m, int n); Problem Approach The solution uses dynamic programming to build a table where each cell contains the minimum cost to reach that position from the starting point (0, 0). The formula for any cell (i, j) is − dp[i][j] = ...

Read More

Problem with scanf() when there is fgets()/gets()/scanf() after it in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 635 Views

In C programming, a common issue occurs when using scanf() followed by fgets(), gets(), or another scanf() for character input. The problem arises because scanf() leaves a newline character in the input buffer, which interferes with subsequent input functions. Syntax scanf("format_specifier", &variable); fgets(string, size, stdin); Problem 1: scanf() Followed by fgets() When scanf() reads an integer and is followed by fgets(), the newline character left by scanf() is immediately consumed by fgets() − #include int main() { int x; char str[100]; ...

Read More

Process Synchronization in C/C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 7K+ Views

Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data. Syntax /* General structure for process synchronization */ do { entry_section(); critical_section(); exit_section(); remainder_section(); } while(1); The Critical-Section Problem Every process has a reserved segment ...

Read More

Printing source of a C program itself

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 719 Views

In C programming, a self-printing program (also called a quine) is a program that prints its own source code as output. This can be achieved using the __FILE__ macro along with file I/O operations to read and display the current source file. Syntax FILE *fopen(__FILE__, "r"); The __FILE__ macro is a preprocessor macro that returns the name of the current source file as a string literal. This allows us to open and read our own source code at runtime. Example: Understanding __FILE__ Macro First, let's see how the __FILE__ macro works − ...

Read More

C Program for Round Robin scheduling

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 10K+ Views

Round Robin is a CPU scheduling algorithm designed for time-sharing systems. It operates like FCFS scheduling but with a crucial difference − each process gets a fixed time slice called a time quantum. When a process's time quantum expires, it is preempted and moved to the back of the ready queue, allowing other processes to execute. What is Round Robin Scheduling? Round Robin treats the ready queue as a circular queue where each process gets a small unit of time (10-100 milliseconds typically). The main advantages include fair CPU allocation and good response time for interactive systems. The ...

Read More

Priority Queue using Linked List in C

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 5K+ Views

A priority queue is a special type of queue where elements are served based on their priority rather than the order of insertion. In C, we can implement a priority queue using a linked list where each node contains data, priority, and a pointer to the next node. In a priority queue, elements with higher priority (lower numerical value) are processed first. If two elements have the same priority, they follow FIFO order. Syntax typedef struct node { int data; int priority; struct node* ...

Read More

C Program for FCFS Scheduling

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 56K+ Views

We are given with the n number of processes i.e. P1, P2, P3, ..., Pn and their corresponding burst times. The task is to find the average waiting time and average turnaround time using FCFS CPU Scheduling algorithm. What is Waiting Time and Turnaround Time? Turnaround Time is the time interval between the submission of a process and its completion. Turnaround Time = completion of a process − submission of a process Waiting Time is the difference between turnaround time and burst time Waiting Time = turnaround time − burst time What is FCFS Scheduling? ...

Read More

C Program for focal length of a lens

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 478 Views

In optics, the focal length is a fundamental property of lenses that determines their converging or diverging power. Given the image distance and object distance from a lens, we can calculate the focal length using the lens equation. What is Focal Length? Focal length of an optical system is the distance between the center of lens or curved mirror and its focus. It represents the distance over which initially collimated rays are brought to a focus. ...

Read More

Code valid in both C and C++ but produce different output

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

Here we will see programs that return different results when compiled with C or C++ compilers. These differences arise due to fundamental variations in how C and C++ handle certain language features. Character Literal Size Differences In C, character literals like 'a' are treated as int type, while in C++ they are treated as char type. This affects the result of the sizeof() operator − Example 1: C Code #include int main() { printf("Character: %c, Size: %d bytes", 'a', sizeof('a')); return 0; } ...

Read More
Showing 411–420 of 953 articles
« Prev 1 40 41 42 43 44 96 Next »
Advertisements