google.com, pub-6167773875660516, DIRECT, f08c47fec0942fa0 Write a program to find the sum of all array elements using dynamic memory allocation - 2nd puc computer science

Write a program to find the sum of all array elements using dynamic memory allocation

Write a program to find the sum of all array elements using dynamic memory allocation

c program to find sum of n elements using malloc() function

#include<stdio.h>

#include<conio.h>

void main()

{

int i,n,sum=0,*a;

clrscr();

printf("How many elements? ");

scanf("%d",&n);

a=(int *)malloc(n*sizeof(int));

printf("Enter the elements:\n");

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

scanf("%d",a+i);

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

sum=sum+*(a+i);

printf("Sum of all elements in array=%d",sum);

getch();

}

Output:

How many elements? 5

Enter the elements:                                                            

1                                                                              

2                                                                              

3                                                                               

4                                                                              

5                                                                              

Sum of all elements in array=15           

Write a program to find the sum of all array elements using dynamic memory allocation  Write a program to find the sum of all array elements using dynamic memory allocation Reviewed by Vision Academy on December 22, 2021 Rating: 5

No comments:

CheckOut

Powered by Blogger.