0

How do you calculate the sum of elements in an array ?

17th Feb 2017, 10:32 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
6 Respuestas
+ 3
int sum=0; for (int a : array) sum += a;
17th Feb 2017, 5:13 PM
Tamás Barta
Tamás Barta - avatar
+ 1
//java code import java.lang.*; import java.util.*; class Array1 { public static void main(String [] args) { int a[] = new int[1000]; int n,sum=0; Scanner s = new Scanner(System.in); System.out.println("\n Enter the number of elements "); n=s.nextInt(); System.out.println("\n Enter the elements "); for(int i=0;i<n;i++) { a[i]=s.nextInt(); } for(int i=0;i<n;i++) { sum = sum + a[i]; } System.out.println("\n The sum of elements is "+sum); } }
17th Feb 2017, 11:20 AM
Chiranji Murali Sai Ganesh
Chiranji Murali Sai Ganesh - avatar
0
Hey Richardo ill be honest i dont really know i am currently learning html and javascript but i dont yet know what arrays are
17th Feb 2017, 10:39 AM
The Youngtube
The Youngtube - avatar
0
lol thx for your honesty tube
17th Feb 2017, 10:52 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
I wanted the answer in Java
17th Feb 2017, 11:04 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
// C language // maximum 100 elements only #include<stdio.h> int main() { int a[100],n,i,sum=0; printf("Enter the num of elements\n"); scanf("%d",&n); printf("Enter the elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { sum=a[i]+sum; } printf("The sum of elemennts is %d",sum); }
17th Feb 2017, 11:21 AM
Chiranji Murali Sai Ganesh
Chiranji Murali Sai Ganesh - avatar