- 2

Guys i need a answer for this question

Given string is 1,2,3,4,5 we need to print as even 246 and odd 135 using

7th Feb 2017, 5:53 AM
tharun
tharun - avatar
4 Answers
+ 9
GUYS he NEEDS an answer for this question. LIVES ARE AT STAKE DANG IT!!!
7th Feb 2017, 5:58 AM
Ahri Fox
Ahri Fox - avatar
+ 3
You can do it by putting a conditional statement and checking for even and odd like if(str.charAt(i)%2==0) if true then it is even and if it is false then it's odd number. Then print the result.
7th Feb 2017, 6:00 AM
Varun Moghe
Varun Moghe - avatar
+ 3
My solution is maybe a bit more complicated but it should work ;) import java.util.ArrayList; import java.util.List; public class Program { public static void main(String[] args) { String input = "1,2,3,4,5"; String[] splitted = input.split(","); List odd = new ArrayList(); List even = new ArrayList(); for(String x : splitted) { if(Integer.parseInt(x) % 2 == 0) { even.add(x); } else { odd.add(x); } } System.out.println("Odd Numbers: " + odd.toString()); System.out.println("Even Numbers: " + even.toString()); } }
7th Feb 2017, 9:27 AM
R4xx4r
R4xx4r - avatar
+ 2
C program-- you can follow this method for other languages as well by changing syntax according to language you use. int a[4]={1,2,3,4,5}; int i; for(i=1;i<5;i+=2) { printf("%d",a[i]); }
7th Feb 2017, 6:35 AM
Gulshan Yadav
Gulshan Yadav - avatar