C program - sum of all elements in an array
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
void
main()
{
int
i,n,sum=0;
int
*a;
clrscr();
printf
(
"Enter the size of array A\n"
);
scanf
(
"%d"
, &n);
a=(
int
*)
malloc
(n*
sizeof
(
int
));
/*Dynamix Memory Allocation */
printf
(
"Enter Elements of First List\n"
);
for
(i=0;i<n;i++)
{
scanf
(
"%d"
,a+i);
}
/*Compute the sum of all elements in the given array*/
for
(i=0;i<n;i++)
{
sum = sum + *(a+i);
}
printf
(
"Sum of all elements in array = %d\n"
, sum);
}
Output:
Post a Comment