import javax.swing.*;
public class sumofarrayelements
{
public static void main(String[] args) {
int n;
n=Integer.parseInt(JOptionPane.showInputDialog("enter size "));
int []a= new int[n];
int sum=0;
System.out.print("enter array elements\n");
for(int i=0; i<n; i++)
{
a[i]=Integer.parseInt(JOptionPane.showInputDialog(" "));
}
for(int i=0; i<n; i++)
{
sum=sum+a[i];
}
System.out.print("sum =" +sum);
}
}
public class sumofarrayelements
{
public static void main(String[] args) {
int n;
n=Integer.parseInt(JOptionPane.showInputDialog("enter size "));
int []a= new int[n];
int sum=0;
System.out.print("enter array elements\n");
for(int i=0; i<n; i++)
{
a[i]=Integer.parseInt(JOptionPane.showInputDialog(" "));
}
for(int i=0; i<n; i++)
{
sum=sum+a[i];
}
System.out.print("sum =" +sum);
}
}
output:
enter size 5
enter array elements 1
2
3
4
5
sum = 15
No comments:
Post a Comment
SAY HELLO!!