C program to read N integers and store them in an array A, and so find the sum of all these elements using pointer. Output the given array and and the computed sum with suitable heading

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to read N integers and store them in an array A, and so find the sum of all these elements using pointer. Output the given array and and the computed sum with suitable heading */

#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);

}     /* End of main() */

/*—————————-
Output
Enter the size of array A
4
Enter Elements of First List
10
20
30
40
Sum of all elements in array = 100
————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to read a matrix A (MxN) and to find the following using functions a) Sum of the elements of each row b) Sum of the elements of each column c) Find the sum of all the elements of the matrix Output the computed results with suitable headings
  2. C program to read N integers (zero, +ve and -ve) into an array A and to a) Find the sum of negative numbers b) Find the sum of positive numbers and c) Find the average of all input numbers Output the various results computed with proper headings
  3. C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings
  4. C program to read N names, store them in the form of an array and sort them in alphabetical order. Output the give names and the sorted names in two columns side by side with suitable heading
  5. C program to input N numbers (integers or reals) and store them in an array. Conduct a linear search for a given key number and report success or failure in the form of a suitable message

Leave a Comment

Previous post:

Next post: