0% found this document useful (0 votes)
3 views

Programming_Solutions_with_Output

The document provides solutions for various programming problems in C, including calculating the sum of array elements using recursion, finding the area of a circle and square using a union, and determining the sum and maximum of an array using pointers. It also explains dynamic memory allocation in C, detailing functions like malloc, calloc, realloc, and free. Each solution includes an algorithm, code examples, and output results.

Uploaded by

mm.nitish76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming_Solutions_with_Output

The document provides solutions for various programming problems in C, including calculating the sum of array elements using recursion, finding the area of a circle and square using a union, and determining the sum and maximum of an array using pointers. It also explains dynamic memory allocation in C, detailing functions like malloc, calloc, realloc, and free. Each solution includes an algorithm, code examples, and output results.

Uploaded by

mm.nitish76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Solutions for Programming Problems with Output

1. Sum of Array Elements using Recursion

Algorithm:

1. Define a recursive function that takes the array and the size of the array as parameters.

2. If the size is 0, return 0 (base case).

3. Otherwise, recursively call the function with size - 1, and add the current element to the result.

4. Return the sum.

Code in C:

#include <stdio.h>

int sum(int arr[], int size) {

if (size == 0) {

return 0;

return arr[size - 1] + sum(arr, size - 1);

int main() {

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Sum: %d", sum(arr, size));

return 0;

}
Output:

Sum: 15

2. Area of Circle and Square using Union

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 {

float radius; // For circle

float side; // For square

};

float area_of_circle(union Shape s) {

return M_PI * s.radius * s.radius;

float area_of_square(union Shape s) {

return s.side * s.side;

}
int main() {

union Shape circle;

union Shape square;

circle.radius = 5.0;

square.side = 4.0;

printf("Area of Circle: %.2f

", area_of_circle(circle));

printf("Area of Square: %.2f

", area_of_square(square));

return 0;

Output:

Area of Circle: 78.54

Area of Square: 16.00

3. Sum and Maximum of an Array using Pointer

Algorithm:

1. Define a function that takes a pointer to the array.

2. Initialize sum to 0 and maximum to the first element of the array.

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.

5. Return both the sum and the maximum.


Code in C:

#include <stdio.h>

void sum_and_max(int *arr, int size, int *sum, int *max) {

*sum = 0;

*max = arr[0];

for (int i = 0; i < size; i++) {

*sum += arr[i];

if (arr[i] > *max) {

*max = arr[i];

int main() {

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

int sum, max;

sum_and_max(arr, size, &sum, &max);

printf("Sum: %d

", sum);

printf("Maximum: %d

", max);

return 0;
Output:

Sum: 15

Maximum: 5

4. Explanation of Dynamic Memory Allocation

Dynamic Memory Allocation:

- It is the process of allocating memory during the runtime of the program.

- 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.

- calloc() is similar but also initializes the memory to zero.

- realloc() is used to resize a previously allocated memory block.

- free() releases the dynamically allocated memory to prevent memory leaks.

Example:

#include <stdio.h>

#include <stdlib.h>

int main() {

int *arr;

int n = 5;

arr = (int*)malloc(n * sizeof(int)); // Dynamically allocate memory for an array of 5 integers

if (arr == NULL) {

printf("Memory allocation failed!");

return 1;
}

// Assign values to the array

for (int i = 0; i < n; i++) {

arr[i] = i + 1;

// Print values

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

free(arr); // Free the dynamically allocated memory

return 0;

Output:

12345

You might also like