0
Program to print sum of all element of array of size N.
Please explain logic briefly
5 Respuestas
+ 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.
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
}
0
how can we do this using for loop ??
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;
}