C program to compute sum of array elements using pointers

C program to compute sum of array elements using pointers:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
#include<conio.h>
main()
{
   int num[5];
   int i, sum = 0;
   int *ptr;

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

   for(i=0;i<5;i++)
      scanf("%d",&num[i]);

   ptr=num; // ptr=&num[0]

   for(i=0;i<5;i++)
    {
      sum=sum + *ptr;
      ptr++;
    }
   printf("The sum of array elements :%d",sum);
}

OUTPUT:

Enter 5 elements :
5 7 8 9 5
The sum of array elements :34

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.