BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String n1 = reader.readLine();
char ch = n1.charAt(0);
int n = Character.getNumericValue(ch);
if (n % 2 == 0){
even++;
}
else if (n % 2 ==1){
odd++;
}
System.out.println("Even: "+even+" Odd: "+odd);
I'm not able to figure out how to check everyone number. I able to check just one.
Tried to use for and while, but it was unsuccessful.
Please help:(
Resolved
Comments (11)
- Popular
- New
- Old
You must be signed in to leave a comment
hidden #10362262
2 December 2018, 17:04
import java.io.*;
public class Solution {
public static int even;
public static int odd;
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String n1 = reader.readLine();
int position = 0;
int length = n1.length();
/*used a loop here where I increased the variable "position" and simultaneously decreased the variable
"length" */
System.out.printf("Even: %d Odd: %d", even, odd);
}
}
0
Janusz
1 December 2018, 16:58
Number of digits int n = s.length (); (s -) A string of characters has been entered
The string has been replaced by int
I check the rest from dividing the number by 10
(even if divided by 2 = 0) or odd (rest -1)
sorry for my English
0
Laurap5
23 November 2018, 14:18
Thank you, now it's working :) with loop for (int i=0; i<n1.length(); i++)
0
Roman Ivanov
22 November 2018, 17:25
Use for-loop for all chars.
And don't foget to initialize all variables at first :) +4
Justin Case
10 December 2018, 20:14
I got an error from your code. This :
...
for (int i=0 ;i<n1.length(); i++) {
...
fixed it for me.
0
Roman Ivanov
10 December 2018, 20:48
oops... :) you're right
0
Khurram
22 November 2018, 05:55
you should use loops for this, inside a loop you could use the mod (%) operator to get the right most integer, check for even / odd then use the division ( / ) operator to remove that number. Keep doing this till all the integers are evaluated
+1
Sokhib
2 January 2019, 08:37
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
int b = Integer.parseInt(reader.readLine());
int c = Integer.parseInt(reader.readLine());
int d = Integer.parseInt(reader.readLine());
// even = 0;
//odd = 0;
if (a % 2 == 0){
even++;
}
else{
odd++;
}
if (b % 2 == 0){
even++;
}
else {
odd++;
}
if (c % 2 == 0){
even++;
}
else{
odd++;
}
if (d % 2 == 0){
even++;
}
else{
odd++;
}
System.out.println("Even: " + even + " Odd: "+ odd);
}
}
0
Sokhib
2 January 2019, 08:37
can u plz tell me why it is not verifying ?
0
Khurram
2 January 2019, 16:26
you are supposed to take 1 number as an input (any digit number) not 4 separate 1-digit numbers
+1
Michael Martin
22 November 2018, 01:35
change the String into a character array. Then use a loop to look at every character in the character array.
0