Sunday, January 12, 2014

Java Arrays Simple Example Programes

//Note:  Analysis the programs errors,outputs and Understand the Java Array Concept

class Demo1{
public static void main(String arg[]){
int a[]=new int[10];
for(int i=0;i<=9;i++)
{
a[i]=i*i*i;
System.out.println(a[i]);
}
int s=0;
for(int i=0;i<=9;i++)
{
s=s+a[i];
}
System.out.println("s="+s);
}
}



class Demo2{
public static void main(String arg[]){
int a[]=new int[10];
for(int i=0;i<=9;i++)
{
a[i]=i;
System.out.println(a[i]);
}

int s=0;
for(int i=0;i<=9;i++)
{
s=s+a[i];
}
System.out.println("s="+s);
int t=0;
for(int i=0;i<=9;i++){
for(int j=i+1; j<=9;j++){
if(a[i]<a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i=0;i<=9;i++)
{
System.out.println(a[i]);
}
}
}


class Demo3{
public static void main(String arg[]){
int a[]={1,5,7,8,9,2,17,18,19,11};
System.out.println("The given numbers are ..");

for(int i=0;i<=9;i++)
{
System.out.println(a[i]);
}
int t=0;
for(int i=0;i<=9;i++){
for(int j=i+1; j<=9;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println("The numbers in ascending order are ..");
for(int i=0;i<=9;i++)
{
System.out.println(a[i]);
}
}
}


class Demo4{
public static void main(String arg[]){
int a[][]= new int[3][3];
a[0][0]=7;
a[0][1]=7;
a[0][2]=8;
a[1][0]=9;
a[1][1]=1;
a[1][2]=2;
a[2][0]=7;
a[2][1]=7;
a[2][2]=8;
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
System.out.print(a[i][j]+" ");
}
System.out.println(" ");
}
}
}


class Demo5{
public static void main(String arg[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
System.out.print(a[i][j]+" ");
}
System.out.println(" ");
}
int s=0;
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
if(i==j)
s=s+a[i][j];
}
}
System.out.println("The sum is "+s);
}
}



Post a Comment