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
Neil
Level 5
Hyderabad
  • 26.02.2020
  • 646views
  • 2comments

Help please?

Question about the task Describing numbers
Java Syntax,  Level 4,  Lesson 7
Archived


Enter an integer from the keyboard in the range 1 - 999. Display a string description as follows:
"even single-digit number" - if the number is even and has one digit,
"odd single-digit number" - if the number is odd and has one digit,
"even two-digit number" - if the number is even and has two digits,
"odd two-digit number" - if the number is odd and has two digits,
"even three-digit number" - if the number is even and has three digits,
"odd three-digit number" - if the number is odd and has three digits.
If the entered number does not fall in the range 1 - 999, don't display anything.

Example for 100:
even three-digit number

Example for 51:
odd two-digit number

Requirements:
  • The program should read one number from the keyboard.
  • The program should use System.out.println() or System.out.print()..
  • If the number is even and has one digit, display "even single-digit number".
  • If the number is odd and has one digit, display "odd single-digit number".
  • If the number is even and has two digits, display "even two-digit number".
  • If the number is odd and has two digits, display "odd two-digit number".
  • If the number is even and has three digits, display "even three-digit number".
  • If the number is odd and has three digits, display "odd three-digit number".
  • If the entered number doesn't fall in the range 1 - 999, don't display anything
  • The program should display a string description of the number and nothing else.
package com.codegym.task.task04.task0427; /* Describing numbers */ import java.io.*; import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { //write your code here Scanner sc = new Scanner(System.in); int num = sc.nextInt(); if(num <= 10 ) { if(num % 2 == 0) { System.out.println("even single-digit number"); } else { System.out.println("odd single-digit number"); } } else if(num > 10 && num < 101) { if(num % 2 == 0) { System.out.println("even two-digit number"); } else { System.out.println("odd two-digit number"); } } else if(num > 100 && num < 1001) { if(num % 2 == 0) { System.out.println("even three-digit number"); } else { System.out.println("odd three-digit number"); } } else { System.out.println(); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
26 February 2020, 16:08useful
the code reads: line 18: Numbers between negative MAX_INTEGER_VALUE to 10, inclusive are single digit line 29: Numbers between 11 and 100, inclusive, are two digit line 40: numbers between 101 and 1000, inclusive, are three digit It should be numbers between 1-9 single digit between 10-99 two digit 100-999 three digit Also, line 53 is a violation of the condition that to not displaying anything if the number is out of range. Tip: If you look at the output line, only the first two words are interchangeable. The "-digit number" part is always the same. I would code this to figure out the what the first 2 words should be then put the pieces together in one output line:
String firstWord;
String secondWord;

{
   // in this block figure out what the first word should be: "even" or "odd"
}

{
   // in this block figure out what the second word should be: "single", "two, or "three"
}

   //output the results:
System.out.println(firstWord + " " + secondWord + "-digit number");
You already did this in your code, just in 3 times the amount of steps.
+1
Neil
Level 5 , Hyderabad, India
27 February 2020, 15:25
thank u...
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.