Javatpoint Logo
Javatpoint Logo

12. C program to print the sum of all elements in an array

In this program, we need to calculate the sum of all the elements of an array. This can be solved by looping through the array and add the value of the element in each iteration to variable sum.

C program to print the sum of all elements in an array

Sum of all elements of an array is 1 + 2 + 3 + 4 + 5 = 15.

ALGORITHM:

  • STEP 1: START
  • STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
  • STEP 3: SET sum = 0
  • STEP 4: length= sizeof(arr)/sizeof(arr[0])
  • STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 UNTIL i<length
  • STEP 6: sum = sum + arr[i]
  • STEP 7: i=i+1.
  • STEP 8: PRINT "Sum of all the elements of an array:" by assigning sum.
  • STEP 9: RETURN 0.
  • STEP 10: END.

PROGRAM:

Output:

Sum of all the elements of an array: 15
Next TopicC Programs




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA