HOW TO CALCULATE THE SUM OF THE ELEMENT IN A GIVEN ARRAY: -
Here is the solution to this query in C Languageguage.
STEP 1:- Input the size of the array and store the value in m and arr[m].
STEP 2: -To store the sum of array elements, initialize a variable sum = 0.
STEP 3: -To find the sum of all elements, iterate through each element and add the current element to the sum.
STEP 4: -Inside the loop add the current array element to sum i.e.
sum = sum + arr[i] or sum += arr[i].
CODE:-
#include<stdio.h>
int main()
{
int i,num,a[100],sum=0;
printf("Enter the value of number: ");
scanf("%d",&num);
printf("Enter value of array: ");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("The sum is %d",sum);
return 0;
}
Comments
Post a Comment