Programming Articles

Page 948 of 2547

Finding even and odd numbers in a set of elements dynamically using C language

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 1K+ Views

In C programming, we can find and calculate the sum of even and odd numbers from a set of elements using dynamic memory allocation. This approach allows us to handle variable-sized datasets efficiently by allocating memory at runtime. Syntax ptr = (int *)malloc(n * sizeof(int)); if (ptr == NULL) { // Handle memory allocation failure } // Use the allocated memory free(ptr); Algorithm The logic to separate even and odd numbers is straightforward − Even numbers: Numbers divisible by 2 (remainder is 0 when divided by 2) Odd ...

Read More

Explain dynamic memory allocation in C with an example

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 13K+ Views

Dynamic memory allocation in C allows programmers to allocate and deallocate memory during program execution rather than at compile time. This provides flexibility to handle varying data sizes efficiently. Syntax void* malloc(size_t size); void* calloc(size_t num, size_t size); void* realloc(void* ptr, size_t new_size); void free(void* ptr); Dynamic Memory Allocation Functions The main functions used for dynamic memory allocation in C are − malloc() − allocates a block of memory in bytes at runtime calloc() − allocates continuous blocks of memory and initializes them to zero realloc() − used to resize previously allocated ...

Read More

Post and Pre incremented of arrays in C language

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 5K+ Views

In C programming, pre and post increment operators can be applied to array elements to modify their values. Understanding how these operators work with arrays is crucial for effective C programming. Syntax // Pre-increment: increment first, then use value variable = ++array[index]; // Post-increment: use value first, then increment variable = array[index]++; Pre-increment vs Post-increment Increment operator (++) − It is used to increment the value of a variable by 1 There are two types of increment operators − pre increment and post increment Pre-increment (++variable): Increment operator is placed before ...

Read More

Matrix row sum and column sum using C program

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 3K+ Views

In C programming, calculating the sum of rows and columns in a matrix is a fundamental operation. This involves iterating through each row to compute row sums and through each column to compute column sums using nested loops. Syntax for(i=0; i

Read More

How to print the numbers in different formats using C program?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 776 Views

In C programming, we can print numbers and symbols in different patterns like pyramids and triangles using nested loops. The outer loop controls the rows while inner loops handle spacing and pattern generation. Syntax for(int i = 1; i

Read More

Explain Compile time and Run time initialization in C programming?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 20K+ Views

In C programming, array initialization can occur at two different times: compile time and run time. Understanding the difference between these two approaches is crucial for effective memory management and program design. Syntax // Compile time initialization type array_name[size] = {value1, value2, ..., valueN}; // Run time initialization type array_name[size]; // Values assigned during program execution Compile Time Initialization In compile time initialization, array values are specified directly in the source code when the array is declared. The compiler allocates memory and assigns values during the compilation process − #include ...

Read More

Why is the compiler not reading string after integer in C programming?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 2K+ Views

When reading string input after integer input in C, a common issue occurs where the string appears to be skipped. This happens because scanf() leaves a newline character in the input buffer after reading the integer. The Problem When you use scanf() to read an integer and then try to read a string using gets() or fgets(), the leftover newline character from the integer input is immediately consumed by the string function, causing it to terminate without reading actual string input. Solution Methods Method 1: Using a Temporary Character Read the leftover newline character using ...

Read More

What is the common error occurred while using scanf() statement in C language?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 1K+ Views

The scanf() function in C is commonly used to read formatted input, but it often causes issues when reading strings after numeric values. The main problem occurs because scanf() leaves a newline character in the input buffer after reading numbers, which interferes with subsequent string input functions. Common Error When using scanf() to read numeric data followed by string input with gets() or fgets(), the string input is skipped because the leftover newline character from the previous scanf() is immediately consumed. Example: Demonstrating the Problem Here's a program that shows the common error when reading a ...

Read More

How to print a name multiple times without loop statement using C language?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 8K+ Views

In C programming, printing a name multiple times typically requires loops. However, we can achieve this without using any loop or goto statement by implementing recursive functions or using multiple printf statements. Syntax void recursiveFunction(parameters) { // Base case if (condition) return; // Process printf("text"); // Recursive call recursiveFunction(modified_parameters); } Method 1: Using ...

Read More

Find the ASCII value of the uppercase character 'A' using implicit conversion in C language?

Bhanu Priya
Bhanu Priya
Updated on 15-Mar-2026 602 Views

Implicit type conversion occurs when the compiler automatically converts a smaller data type to a larger data type without explicit casting. In C, when a char is used in an arithmetic operation, it gets implicitly converted to an int, allowing us to access its ASCII value. Syntax int ascii_value = character + 0; // Implicit conversion from char to int Example 1: ASCII Value of 'A' The following example demonstrates finding the ASCII value of uppercase character 'A' using implicit conversion − #include int main() { ...

Read More
Showing 9471–9480 of 25,466 articles
« Prev 1 946 947 948 949 950 2547 Next »
Advertisements