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 10 of 377
C program to find marks of students for boys or girls
In C programming, we can solve problems involving array processing based on index patterns. This program calculates the sum of marks for either boys or girls, where boys' marks are stored at even indices (0, 2, 4, ...) and girls' marks are stored at odd indices (1, 3, 5, ...). Syntax int calculateMarks(int marks[], int n, char gender); Algorithm To solve this problem, we follow these steps − Initialize g_sum and b_sum to 0 Iterate through the array from index 0 to n-1 If index is odd, add marks to g_sum (girls) ...
Read MoreC program to find nth term of given recurrence relation
In C programming, we can find the nth term of a given recurrence relation where each term is the sum of the previous three terms. This type of recurrence relation follows the pattern S(n) = S(n-1) + S(n-2) + S(n-3) for n > 3, with given base cases. Syntax int solve(int a, int b, int c, int n); Recurrence Relation Definition The recurrence relation is defined as follows − S(1) = a (first base case) S(2) = b (second base case) S(3) = c (third base case) S(n) = S(n-1) + S(n-2) ...
Read MoreC program to find sum of digits of a five digit number
In C programming, finding the sum of digits of a five-digit number involves extracting each digit and adding them together. We use the modulo operator (%) to extract the last digit and integer division (/) to remove it from the number. Syntax int digitSum = 0; while (number != 0) { digitSum += number % 10; number /= 10; } So, if the input is like num = 58612, then the output will be 22 because 5 + 8 + 6 + 1 + 2 = 22. ...
Read MoreC program to write all digits into words using for loop
In C programming, converting digits to their word equivalents is a common task that helps beginners understand conditional statements and loops. This program takes a range of digits and prints each digit as its corresponding word using a for loop. Syntax for(initialization; condition; increment) { // Convert digit to word using if-else statements } Example: Converting Digits to Words in a Range This example demonstrates how to convert all digits from 3 to 8 into their word representations using a for loop − #include void digitToWord(int ...
Read MoreC program to convert digit to words
In C programming, converting a digit to its corresponding word representation is a common task. This involves taking a single digit (0-9) and converting it to its English word equivalent like "Zero", "One", "Two", etc. Syntax void digitToWord(int digit); Method 1: Using If-Else Statements This approach uses a series of if-else statements to check each digit and print the corresponding word − #include void digitToWord(int d) { if (d < 0 || d > 9) { printf("Beyond range ...
Read MoreC program to find sum and difference using pointers in function
In C programming, functions can return only one value at a time. However, when we need to calculate multiple results like sum and difference of two numbers, we can use pointers as function parameters. This technique allows us to modify the original variables directly through their memory addresses. Syntax void functionName(int *ptr1, int *ptr2); // Call the function functionName(&variable1, &variable2); Algorithm To solve this problem, we will follow these steps − Define a function that takes addresses of two variables as parameters Store the sum in a temporary variable Calculate the difference ...
Read MoreC program to find maximum of four integers by defining function
In C programming, we can find the maximum of four integers by defining our own function. We create a max() function that compares two numbers and returns the larger one, then use it repeatedly to find the maximum among all four numbers. So, if the input is like a = 5, b = 8, c = 2, d = 3, then the output will be 8. Syntax int max(int x, int y); Algorithm To solve this, we will follow these steps − Define a function max() that takes two integers x ...
Read MoreC program to find sum and difference of two numbers
In C programming, finding the sum and difference of two numbers is a fundamental operation. This program demonstrates how to perform arithmetic operations on different data types and handle the output formatting correctly based on the operands' types. Syntax // For integer arithmetic int result = num1 + num2; int result = num1 - num2; // For floating-point arithmetic float result = num1 + num2; float result = num1 - num2; Example 1: Sum and Difference of Integer Numbers This example shows basic arithmetic operations on integer values − ...
Read MoreC Program to read and write character string and sentence
In C programming, reading different types of input requires different functions. Reading a single character uses scanf() with %c, strings without spaces use scanf() with %s, and sentences with spaces require fgets() for proper handling. Syntax // Character input scanf("%c", &character); // String input (no spaces) scanf("%s", string); // Sentence input (with spaces) fgets(sentence, size, stdin); Example This program demonstrates reading a character, string, and sentence from user input − #include int main() { char character; char string[500]; ...
Read MoreFind a permutation that causes worst case of Merge Sort in C
Merge sort has a consistent O(n log n) time complexity, but in practice, certain input permutations require more comparisons during the merge operations. The worst case occurs when the merge operation at each level compares every element from both subarrays before completing. Syntax void generateWorstCase(int arr[], int left, int right); Understanding the Worst Case The worst case for merge sort happens when at each merge operation, elements from left and right subarrays are alternately selected. This forces maximum comparisons since we cannot skip any elements during merging. Algorithm To generate the worst ...
Read More