We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,sum=0;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++){
sum += arr[i];
}
printf("%d",sum);
return 0;
because we have to print sum in output.
So , in for loop we traverse all element and add it in variable names sum and display it
eg - we have array={1,2,3,4,5}
when we run loop from i=0 it add a[0] (i.e - 1) in sum variable , similarly we run it till i=4 (i.e -5) .
so sum=1+2+3+4+5
1D Arrays in C
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
int main() {
}
**explian please why 'sum'?
for(int i=0;i
because we have to print sum in output. So , in for loop we traverse all element and add it in variable names sum and display it eg - we have array={1,2,3,4,5} when we run loop from i=0 it add a[0] (i.e - 1) in sum variable , similarly we run it till i=4 (i.e -5) . so sum=1+2+3+4+5
i'm not good in explaining but hope it helps :)
thanku so much i got it :) thanku is less to u. u solve my problem in a second. i search this so much on chatgpt also but dont got this :)