,SINGLE DIMENSIONAL ARRAY
Sum of all the elements, sum of all the even elements, sum of all the subscripted
elements.
import java.util.*;
public class array
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]= new int[10];
//accepting
System.out.println(“Enter the values”);
for(int i=0; i<10; i++)
{
a[i]= sc.nextInt();
}
//printing
for(int i=0; i<10; i++)
{
System.out.println(a[i] + “ “ );
}
//sum of all the elements
int asum=0;
for(int i=0; i<10; i++)
{
asum= asum+a[i];
}
//sum of all the even elements
int esum=0;
for(int i=0; i<10; i++)
{
if a[i]%2==0)
esum= esum+a[i];
}
//sum of all the odd subscripted elements
int osum=0;
for(int i=1; i<10; i=i+2)
{
, osum= osum+a[i];
}
//printing
System.out.println(“Sum of all the elements” + asum);
System.out.println(“Sum of all the even elements” + esum);
System.out.println(“Sum of all the odd subscripted elements” + osum);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Sum of all the elements: 55
Sum of all the even elements: 30
Sum of all the odd subscripted elements: 30
Find the maximum and minimum element in the array.
import java.util.*;
public class max_min
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]= new int[10];
//accepting