How To Write a C Program To Compute Sum Of The Array Elements Using Pointers in C Programming Language ?
Solution For C Program To Compute Sum Of The Array Elements Using Pointers:
#include<stdio.h> #include<conio.h> void main() { int a[10]; int i,sum=0; int *ptr; printf("Enter 10 elements:n"); for(i=0;i<10;i++) scanf("%d",&a[i]); ptr = a; /* a=&a[0] */ for(i=0;i<10;i++) { sum = sum + *ptr; //*p=content pointed by 'ptr' ptr++; } printf("The sum of array elements is %d",sum); }Output :
Enter 10 elements : 11 12 13 14 15 16 17 18 19 20 The sum of array elements is 155Explanation of Program :
Accept the 10 elements from the user in the array.for(i=0;i<10;i++) scanf("%d",&a[i]);We are storing the address of the array into the pointer.ptr = a;Now in the for loop we are fetching the value from the location pointer by pointer variable. Using De-referencing pointer we are able to get the value at address.for(i=0;i<10;i++) { sum = sum + *ptr; ptr++; }Suppose we have 2000 as starting address of the array. Then in the first loop we are fetching the value at 2000. i.esum = sum + (value at 2000) = 0 + 11 = 11In the Second iteration we will have following calculation -sum = sum + (value at 2002) = 11 + 12 = 23
Tags: C Program To Compute Sum Of The Array Elements Using Pointers, c program to find sum of array elements using pointers, c program to find sum of array elements using functions, c program to find sum of array elements using recursion, accessing array elements using pointers in c, c program to find sum of array elements, wap to sort an array using pointers, c program to sort n numbers using pointers, sum of array elements using function in c.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount