0

Program to print sum of all element of array of size N.

Please explain logic briefly

12th Feb 2020, 9:18 AM
Radhey
Radhey - avatar
5 Respostas
+ 2
Make a variable to store the sum and make it 0 Loop through all elements of the array Add each element to the sum variable End loop Print sum Actually in python you can simply use the builtin sum() function that will do the same.
12th Feb 2020, 9:21 AM
Tibor Santa
Tibor Santa - avatar
0
Just the pseudocode in c/c++ Void Sum(A[ ], int len) { int sum =i= 0 While (i<len) { sum=sum+A[i] } Print(sum) } int main() { /*insert array values to array A of length len) */ Print(Sum(A, len)) end main //just random statement }
12th Feb 2020, 10:38 AM
Deepraj Baidya
Deepraj Baidya - avatar
0
how can we do this using for loop ??
12th Feb 2020, 12:01 PM
Radhey
Radhey - avatar
0
Radhey You can also use "for loop" instead of while loop in this way:- //python code A=[1,2,8,9,10] sum =0 for i in range(0,len(A)): sum += A[i] print(sum) // c++ code int main() { int i, sum=0; int A[] ={1,2,3,7}; int len = 4 ;// Can also set it dynamically// for(i=0;i<len;i++) { sum+= A[i] ; } cout<<sum; return 0; }
12th Feb 2020, 12:33 PM
Deepraj Baidya
Deepraj Baidya - avatar