0% found this document useful (0 votes)
72 views

Javaex

The document contains code for a method that calculates the alternating sum of elements in an array. It also contains code that takes user input to populate an array, calculates the sum and average of elements, and checks properties of elements at specific indices.

Uploaded by

Koko Koko
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Javaex

The document contains code for a method that calculates the alternating sum of elements in an array. It also contains code that takes user input to populate an array, calculates the sum and average of elements, and checks properties of elements at specific indices.

Uploaded by

Koko Koko
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

public double alternatingSum(double in) {

double alternatingSum = in;

if(data != null || dataSize > 0) {

for(int i = 0; i < dataSize; i = i + 2) {

alternatingSum += data[i];

for(int i = 1; i < dataSize; i = i + 2) {

alternatingSum -= data[i];

return alternatingSum;

public static void main(String[] args) {

Scanner input=new Scanner (System.in);

// -----------------------------------------------------------------------------

// create the array of integers, fill the array with the elements

// -----------------------------------------------------------------------------

System.out.print("enter the array size:");

int size=input.nextInt();

System.out.print("enter "+size+" integers");

int[] array=new int[size];

int sum=0,count=0;

for(int i=0;i<array.length-1;i++){

array[i]=input.nextInt();
sum+=array[i];

// -----------------------------------------------------------------------------

// Display the elements , calculate and display the sum

// -----------------------------------------------------------------------------

System.out.println("the array elements are: ");

for(int i=0;i<array.length-1;i++){

System.out.print(array[i] + " ");array[i]=input.nextInt();

sum+=array[i];

double avg=(double)sum/size;

System.out.println("the sum is: " + sum + ", the avg is: "+avg);

// -----------------------------------------------------------------------------

// compare the first and the last element in the array

// -----------------------------------------------------------------------------

if(array[0]>array[size-1])

System.out.println("the first element is the bigger");

else if(array[0]<array[size-1])

System.out.println("the first element is the smaller");

else

System.out.println("the two element are equal");

// -----------------------------------------------------------------------------

// Test if the third element is positive/negative and odd/even


// -----------------------------------------------------------------------------

if(array[2] > 0)

System.out.print("the third element is positive");

else

System.out.print(" the third element is negative");

if(array[2] %2 == 0)

System.out.print("the third element is even");

else

System.out.print("the third element is odd");

You might also like