-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOddEvenIndexSum.java
56 lines (42 loc) · 1.4 KB
/
OddEvenIndexSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Read input from STDIN. Print your output to STDOUT*/
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);
}
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 {
//Write code here
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size : ");
int mSize=sc.nextInt();
System.out.print("Values: ");
int[] Array=new int[mSize];
for(int i=0;i<mSize;i++){
Array[i]=sc.nextInt();
}
mOddEvenIndex(Array);
}
}