UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C Program to Calculate the Sum of the Array Elements using Pointer

c program for how to find the sum of each elements in an array using pointer

by Krishna viswambharan


Posted on 26 Oct 2017 Category: C Views: 96

Edited on 07 May 2018


C Program to Calculate the Sum of the Array Elements using Pointer

In this article, we will discuss how to calculates the sum of array elements using pointer. The program uses the pointer to traverse the array and adds up the element values there.

Here is the source code of the C program to calculates the sum of array elements using pointer. The C program is successfully compiled. The program output is also shown below.

5.	#include <stdio.h>
6.	#include <malloc.h>
7.	#include <conio.h>
8.	 
9.	void main()
10.	{
11.	    int i, n, sum = 0;
12.	    int *a;
13.	 
14.	    printf("Enter the array limit:");  //read array limit
15.	    scanf("%d", &n);
16.	    a = (int *) malloc(n * sizeof(int)); //memory alloaction
17.	    printf("Enter array elements :"); //read array elements
18.	    for (i = 0; i < n; i++)
19.	    {
20.	        scanf("%d", a + i);
21.	    }
22.	    // Compute the sum of all elements in the given array 
23.	    for (i = 0; i < n; i++)
24.	    {
25.	        sum = sum + *(a + i);
26.	    }
27.	    printf("Sum array elements = %d", sum);
28.	    getch();
29.	}

output



Leave a Comment:


Advertisement