PROGRAM FIND SUM OF N ELEMENTS
/*PROGRAM FIND SUM OF N ELEMENTS*/
#include <stdio.h>
#include <stdlib.h>
intmain()
{
intn,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL){
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i){
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d\n",sum);
free(ptr);
return 0;
}
OUTPUT :-
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment