Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Sum the Elements of an Array

C Language Examples - Sum the Elements of an Array

C Language Examples C Language Examples

Use a for loop to iterate over the elements and add them together:

Example

#include

<

stdio.h

int

main

(

)

{

int

array

[

10

]

=

{

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

,

0

}

;

int

sum

,

loop

;

sum

=

0

;

for

(

loop

=

9

;

loop

=

0

;

loop

--

)

{

sum

=

sum

array

[

loop

]

;

}

printf

(

"

The sum of the elements is: %d

"

,

sum

)

;

return

0

;

}

The output is:



元素和为:45

C Language Examples C Language Examples