War Between Even And Odd in Java
You will be given an array and you need to find odd numbers and add them, find even numbers and add them. Then compare the result, if the sum of even numbers found to be greater then print 'Even' to the output, if sum of odd number is greater than print 'odd' to the output. If sum of both found to be equal, then print 'Tied' to the output.
Input
9
74 32 31 91 77 88 96 44 23
Output:
Even
Input:
5
2 3 4 9 6
Output:
Tied
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You will be given an array and you need to find odd numbers and add them, find even numbers and add them. | |
* Then compare the result, if the sum of even numbers found to be greater then print 'Even' to the output, | |
* if sum of odd number is greater than print 'odd' to the output. If sum of both found to be equal, | |
* then print 'Tied' to the output. | |
*/ | |
import java.util.*; | |
public class WarBetweenEvenAndOdd { | |
public static void main(String [] args){ | |
Scanner sc=new Scanner(System.in); | |
int size=0; | |
size=Integer.parseInt(sc.nextLine().trim()); | |
if(size>1){ | |
String output=""; | |
String arrayElements=sc.nextLine(); | |
String [] array=arrayElements.split(" "); | |
int even=0,odd=0; | |
for(int i=0;i<array.length;i++){ | |
if(Integer.parseInt(array[i])%2==0){ | |
even+=Integer.parseInt(array[i]); | |
}else{ | |
odd+=Integer.parseInt(array[i]); | |
} | |
} | |
if(even>odd){ | |
output="Even"; | |
}else if(odd>even){ | |
output="Odd"; | |
}else if(even==odd){ | |
output="Tied"; | |
} | |
System.out.println(output); | |
} | |
sc.close(); | |
} | |
} |
Input
9
74 32 31 91 77 88 96 44 23
Output:
Even
Input:
5
2 3 4 9 6
Output:
Tied
Comments
Post a Comment