Write code to sum Odd and Even Index in an array
Write code to sum Odd and Even Index in an array
You can use the following code structure and fill in your logics.
import java.util.*; import java.io.*; public class OddEvenIndexSum { public static void mOddEvenIndex(int array[]) { //your code here } public static void main(String args[]) throws Exception { int mSize = 10; int[] Array = new int[mSize]; for (int i = 0; i < mSize; i++) { Array[i] = i*i; } mOddEvenIndex(Array); } }
import java.util.*; import java.io.*; public class OddEvenIndexSum { public static void mOddEvenIndex(int array[]) { int mOddIndexSum = 0; int mEvenIndexSum = 0; int oddtotal; int size = array.length; int[] oddarry = new int[size]; for (int i = 0; i < array.length; i++) { if (i % 2 == 0) { mEvenIndexSum = mEvenIndexSum + array[i]; System.out.println("Even: " + mEvenIndexSum + " Index : " + i); }/*from ww w. j av a 2 s .c o m*/ if (i % 2 != 0) { mOddIndexSum = mOddIndexSum + array[i]; System.out.println("ODD: " + mOddIndexSum + " Index : " + i); } } System.out.println("Total ODD: " + mOddIndexSum); System.out.println("Total Even: " + mEvenIndexSum); if (mEvenIndexSum > mOddIndexSum) { int eventotal = mEvenIndexSum - mOddIndexSum; System.out.println("Total Greter Even: " + eventotal); } if (mOddIndexSum > mEvenIndexSum) { oddtotal = mOddIndexSum - mEvenIndexSum; System.out.println("Total Greter Odd: " + oddtotal); } } public static void main(String args[]) throws Exception { int mSize = 10; int[] Array = new int[mSize]; for (int i = 0; i < mSize; i++) { Array[i] = i*i; } mOddEvenIndex(Array); } }