0
How do you calculate the sum of elements in an array ?
6 Respuestas
+ 3
int sum=0;
for (int a : array) sum += a;
+ 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);
}
}
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
0
lol thx for your honesty tube
0
I wanted the answer in Java
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);
}