C

Sum of All Array Elements in C

In this tutorial, we are going to see how to find sum of all array elements in C. For example if we have the following array arr[4] = {2, 4, 6, 10} the sum of all array elements will be 22.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

Sum of All Array Elements in C
#include <stdio.h>
 
int main() {
   int i, nbr, sum;
   int arr[30];
 
   printf("Enter the number of elements in the array: ");
   scanf("%d", &nbr);
 
   printf("Enter the array elements: ");
   for (i = 0; i < nbr; i++)
      scanf("%d", &arr[i]);
 
   sum = 0;
   for (i = 0; i < nbr; i++)
      sum = sum + arr[i];
 
   printf("Sum is %d", sum);
 
   return (0);
}

Output:

Enter the number of elements in the array: 4
Enter the array elements: 2 4 6 10
Sum is 22

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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