sum of all the elements of an array using pointer IN C program
C- program to find the sum of all the elements of an array using pointer
This is a C program to find the answer of sum of the array elements using pointer
Problem Description
calculate the sum of array elements using pointer. This c program add the array elements values uses pointer.
problem solution
Problem Description
calculate the sum of array elements using pointer. This c program add the array elements values uses pointer.
problem solution
- First create a pointer variable , which point an int data.
- Take a size of array as input.
- after create a size of array * size of all array elements
- after iterate for loop taking element as input.
- again use for loop to access the address stored in pointer ,add the iterator value , so as to all access elements of array .
source code
This source code of the C program to calculate the sum of array using pointer . this program successfully compilied and tested by netbeans .
Program
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int *p,a[20];
int i, n, sum = 0;
printf("enter the size of array \n");
scanf("%d",&n);
printf("\n give the elements one by one ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<n;i++)
{
sum = sum+*p;
++p;
}
printf("\n\n The sum of array element:%d",sum);
return (EXIT_SUCCESS);
}
Output
Comments
Post a Comment