- 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
4 Answers
+ 9
GUYS he NEEDS an answer for this question.
LIVES ARE AT STAKE DANG IT!!!
+ 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.
+ 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());
}
}
+ 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]);
}