Jun 2, 2007

Single Dimensional Array in JAVA:

// Array is a group of variables of same data types.

class arrdemo
{
public static void main(String[] args)
{
int marks[]; // array declaration that array would be of integer type
marks =new int[5]; // new operator will allocates memory to the array
int tot=0;

for (int i=0;i<=4 ;i++ )
{
marks[i] = i; // storing values in array elements
}

for (int i=0;i<=4 ;i++ ) // printing value of array elements.
{
System.out.println(marks[i]);
}

for (int i=0;i<=4;i++ )
{
tot = marks[i] + tot; // adding array elements
}

System.out.println("Addition of array elements is="+tot);

for (int i=4;i>=0 ;i-- )
{
System.out.println(marks[i]); // printing value of array elements in reverse order
}

}
}

No comments:

Post a Comment