C program to find the sum of 'n' numbers using dynamic memory allocation

Write a C program to find the sum of ‘n’ numbers using dynamically allocating memory :

In this tutorial, we will learn how to find the sum of ‘n’ numbers by allocating memory dynamically. By allocating memory dynamically, we can use only the amount of memory that is required. If the user will enter 10 integers, we will allocate memory for 10 integers. Similarly, for 100 integers, we will allocate memory for 100 integers. We will use the malloc method for dynamic memory allocation and free method to free up unused memory.

How malloc() works:

The malloc function is used for memory allocation. We can pass the size of the memory block to allocate as its parameter and it will allocate that amount of memory. The syntax of the malloc function is:

void *malloc(size_t size)

Where size is the size of the memory to allocate in bytes. It will return a pointer to the allocated memory and we can cast that to the required type. It returns a NULL pointer if the memory can’t be allocated.

For example:

int *p = (int*) malloc(10 * sizeof(int));

This code snippet will allocate memory for 10 integers. The returned value is casted to an integer pointer.

free() method:

The free method is used to de-allocate memory dynamically in C. We should always de-allocate memory if we are not using it. The syntax of the free method is:

free(p)

It takes the pointer of the allocated memory as its parameter.

Method 1: C program to allocate memory dynamically and find the sum of ‘n’ numbers using for loop:

Let’s write a C program that allocates the memory dynamically for n numbers to find the sum of these numbers.

#include <stdio.h>
#include <stdlib.h>

int main()
{
	// 1
	int i;
	int count;
	int *arr;
	int sum = 0;

	// 2
	printf("Enter the total number of elements you want to enter : ");
	scanf("%d", &count);

	// 3
	arr = (int *)malloc(count * sizeof(int));

	// 4
	for (i = 0; i < count; i++)
	{
		// 5
		printf("Enter element %d : ", (i + 1));
		scanf("%d", arr + i);

		// 6
		sum += *(arr + i);
	}

	// 7
	printf("sum is %d \n", sum);

	// 8
	free(arr);
	return 0;
}

Download the code on Github

Explanation :

The commented numbers in the above program denote the step numbers below:

  1. Initialize all the variables. The integer i is used in the loop, count is used to store the total number of elements the user will enter, int *arr is a pointer to use for the allocated memory, and the sum is to hold the sum of the numbers. The sum variable is initialized as 0.
  2. Ask the user to enter the total number of elements and assign this value to the variable count.
  3. Allocate memory to the int pointer variable. We are using malloc to allocate memory for count number of integers.
  4. Use one for loop to read the integer values. On each iteration of the loop:
    1. Ask the user to enter each number. Read it and assign it to the ith position of the allocated memory space.
    2. Add the number to the sum variable. The sum variable holds the total sum of all the user entered numbers.
  5. After the loop is completed, we have the total sum saved in the variable sum. Print out this value to the user.
  6. At the end of the program, we need to use the free method to de-allocate the allocated memory.

Sample Output :

Enter the total number of elements you want to enter : 5
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 4
sum is 14

Enter the total number of elements you want to enter : 10
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 5
Enter element 6 : 6
Enter element 7 : 7
Enter element 8 : 8
Enter element 9 : 9
Enter element 10 : 10
sum is 55

Method 2: C program to allocate memory dynamically and find the sum of ‘n’ numbers with a while loop:

Similar to the above example, we can also use a while loop to find the sum of n numbers with dynamic memory allocation. The following program uses a while loop:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i = 0;
	int count;
	int *arr;
	int sum = 0;

	printf("Enter the total number of elements you want to enter : ");
	scanf("%d", &count);

	arr = (int *)malloc(count * sizeof(int));

	while (i < count)
	{
		printf("Enter element %d : ", (i + 1));
		scanf("%d", arr + i);

		sum += *(arr + i);
		i++;
	}

	printf("sum is %d \n", sum);

	free(arr);
	return 0;
}
  • We have to initialize the variable i before the loop starts. It is initialized as 0.
  • The loop will run until the value of i is smaller than count.
  • At the end of each iteration of the loop, the value of i is incremented by 1.

If you run this program, it will give similar output as the previous program.

Download the code on Github

Method 3: By using a do-while loop:

We can also use a do..while loop instead of while loop. It works in a similar way. The do..while loop runs the code in the do block before it checks the condition of the while block. The complete program is:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i = 0;
	int count;
	int *arr;
	int sum = 0;

	printf("Enter the total number of elements you want to enter : ");
	scanf("%d", &count);

	arr = (int *)malloc(count * sizeof(int));

	do
	{
		printf("Enter element %d : ", (i + 1));
		scanf("%d", arr + i);

		sum += *(arr + i);
		i++;
	} while (i < count);

	printf("sum is %d \n", sum);

	free(arr);
	return 0;
}

It will give similar results.

Download the code on Github

C example to find sum of n numbers with dynamic memory allocation

You might also like: