Wednesday, November 24, 2010

Array

/*
   This is a simple example of an array where a user
    asks for 3 values,stores those values in an array and
    computes the total of the 3 values.
    The content is diplayed using the second loop code.
    The third loop displays the value of an array
    from the last index to index 0.
    The sum of the 3 values is then displayed

*/
import java.util.*;
public class Array1 {

static Scanner c = new Scanner(System.in);

public static void main(String a[]){

int number [] = new int[3];
int sum = 0;

for(int i = 0; i<3; i++){
System.out.print("Enter any number");
number[i]= c.nextInt();
sum = sum + number[i];
}

System.out.println();

for(int i = 0; i<3; i++){
System.out.print(number[i]+"\t");
}

System.out.println();
for(int i = 2; i>=0; i--){
System.out.print(number[i]+"\t");
}

System.out.println("\nThe Sum of the values entered is:"+ sum);
}
}
 
 

No comments:

Post a Comment