Programming_Solutions_with_Output
Programming_Solutions_with_Output
Algorithm:
1. Define a recursive function that takes the array and the size of the array as parameters.
3. Otherwise, recursively call the function with size - 1, and add the current element to the result.
Code in C:
#include <stdio.h>
if (size == 0) {
return 0;
int main() {
return 0;
}
Output:
Sum: 15
Algorithm:
1. Define a union that contains a variable for both circle and square dimensions.
2. The union will hold either the radius (for circle) or the side (for square).
3. Define a structure or function to calculate the area of both using the union.
Code in C:
#include <stdio.h>
#include <math.h>
union Shape {
};
}
int main() {
circle.radius = 5.0;
square.side = 4.0;
", area_of_circle(circle));
", area_of_square(square));
return 0;
Output:
Algorithm:
3. Iterate through the array using the pointer, adding the value to the sum.
4. Check if the current value is greater than the current maximum, and update the maximum.
#include <stdio.h>
*sum = 0;
*max = arr[0];
*sum += arr[i];
*max = arr[i];
int main() {
printf("Sum: %d
", sum);
printf("Maximum: %d
", max);
return 0;
Output:
Sum: 15
Maximum: 5
- In C, this is done using functions like malloc(), calloc(), realloc(), and free().
- malloc() allocates a block of memory of a specified size and returns a pointer to it.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
return 1;
}
arr[i] = i + 1;
// Print values
return 0;
Output:
12345