CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Steve
Level 6
Hartford
  • 02.06.2020
  • 615views
  • 3comments

not sure why last condition fails, everything works fine

Question about the task Even and odd digits
Java Syntax,  Level 6,  Lesson 5
Resolved


Use the keyboard to enter a positive number. Determine the number of even digits and odd digits in the entered number.
If a number is divisible by 2 without a remainder (i.e. the remainder is zero), then it is even.
And we'll increase the even digit counter (static variable even) by 1.
Otherwise, the number is odd, so we'll increase the odd digit counter (static variable odd).
Display the following message: "Even: a Odd: b", where a is the number of even digits and b is the number of odd digits.

Example for 4445:
Even: 3 Odd: 1

Requirements:
  • The program must read data from the keyboard.
  • The main method should calculate how many even digits are in the entered number and then write this number to the variable even.
  • The main method should calculate how many odd digits are in the entered number and then write this number to the variable odd.
  • The program should display text on the screen.
  • The displayed text must match the task conditions.
package com.codegym.task.task06.task0606; import java.io.*; /* Even and odd digits */ 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)); int x = Integer.parseInt(reader.readLine()); while (x>1){ if ((x%2)>0){ odd++; x = x/10; } else{ even++; x = x/10; } } System.out.println("Even: " + even + " Odd: " + odd); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gellert Varga
Level 23 , Szekesfehervar, Hungary
2 June 2020, 23:15solution
If the input is 148, then output is: Even: 2 Odd: 0 Line 19: When the loop would run for third time - for purpose to check the third digit (this is the '1'!) - then this happens: while (x>1) // but now the 'x' is equal to 1! So, it is false, and it will not run anymore, and this third digit will not be checked. So, the 'odd' = 0. ((This is the default initializing value of the 'odd', from Line 13.))
+3
Mike McKenna
Level 25 , Wilmington, United States
2 June 2020, 22:14
Hi my line 17 is . int parsedNumber = Integer.parseInt(reader.readLine()); my line 19 -21 while(parsedNumber 1 = 0){ if ((parsedNumber %10) %2 ==0){ as Seb noted u need to check for positive and negative input . hope i helped
+1
Seb
Level 41 , Crefeld, Germany
2 June 2020, 20:46
Check your program for a negative input as well as an input of the number 1. Both cases need to work correctly too.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.