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 32 of 2547
C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value
Suppose we are given two integers k and n. Our task is to perform three operations; bitwise AND, bitwise OR, and bitwise XOR between all pairs of numbers up to range n. We return the maximum value of all three operations between any two pairs of numbers that is less than the given value k. So, if the input is like n = 5, k = 5, then the output will be 4 3 4. The greatest value of AND, OR, and XOR operations between all pairs of numbers that are less than 5 are 4, 3, and ...
Read MoreC program to sort triangles based on area
Suppose we have an array of different triangles where triangles[i] = [ai, bi, ci] these are the sides of ith triangle. We shall have to sort the triangles based on their area. The area of a triangle using sides is calculated with Heron's formula: square root of p*(p-a)*(p-b)*(p-c) where p = (a+b+c)/2. So, if the input is like (7, 24, 25), (5, 12, 13), (3, 4, 5), then the output will be (3, 4, 5), (5, 12, 13), (7, 24, 25) Syntax struct Triangle { int a, b, c; }; int ...
Read MoreC program to find sum, max and min with Variadic functions
Variadic functions in C allow you to create functions that can accept a variable number of arguments. This is useful when you need functions like sum(), max(), and min() that can work with different numbers of input values. To implement variadic functions, we use the ellipsis (...) notation and include the header file. Syntax return_type function_name(fixed_parameter, ...); The key macros for handling variable arguments are − va_list: Stores information about variable arguments va_start: Initializes the va_list to start accessing arguments va_arg: Retrieves the next argument of specified type va_end: Cleans up the ...
Read MoreC program to find permutations of given strings
In C, generating permutations of strings means finding all possible arrangements of a given array of strings. We can use the next permutation algorithm to systematically generate all permutations in lexicographical order. Syntax int next_permutation(int n, char **strings); Algorithm The next permutation algorithm works by − Finding the rightmost element that is smaller than its next element Finding the smallest element on the right side that is larger than the found element Swapping these two elements Reversing the sequence after the original smaller element Example Let us see the ...
Read MoreC program to find frequency of each digit in a string
In C, finding the frequency of each digit in a string involves scanning through the string character by character and counting occurrences of digits (0-9). We use an array to store the frequency count for each digit. Syntax int freq[10] = {0}; // Array to store frequency of digits 0-9 if (str[i] >= '0' && str[i] = '0' && s[i] 0) { printf("(Number %d, Freq %d)", i, freq[i]); } } } ...
Read MoreC program to print string tokens
In C, string tokenization is the process of breaking a string into smaller parts (tokens) based on specified delimiters. The strtok() function from the string.h library is commonly used for this purpose. Syntax char *strtok(char *str, const char *delim); Parameters str − The string to be tokenized (NULL for subsequent calls) delim − A string containing the delimiter characters How It Works The strtok() function works by following these steps − First call: Pass the string and delimiter to get the first token Subsequent calls: Pass NULL as ...
Read MoreC program to dynamically make array and print elements sum
In C programming, dynamic memory allocation allows us to create arrays of variable size at runtime. This is useful when we don't know the array size at compile time. We use malloc() or calloc() functions from the stdlib.h header to allocate memory dynamically. So, if the input is like n = 6, and array elements 9, 8, 7, 2, 4, 3, then the output will be 33 because sum of 9 + 8 + 7 + 2 + 4 + 3 = 33. Syntax int *arr = (int*) malloc(size * sizeof(int)); int *arr = (int*) calloc(size, ...
Read MoreC program to find amount of volume passed through a tunnel
In C programming, we can solve problems involving conditional volume calculations using structures and functions. This program determines which boxes can pass through a tunnel based on height restrictions and calculates their volumes. Syntax struct Box { int length, width, height; }; int volume(struct Box box); int canPass(struct Box box, int tunnelHeight); Problem Description Given a tunnel with height 41 and unlimited width, we need to find the total volume of boxes that can pass through it. A box can pass if its height is strictly less than the ...
Read MoreC 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 More