package com.codegym.task.task04.task0427;
/*
Describing numbers
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
Scanner sc = new Scanner(System.in) ;
int n = sc.nextInt();
if(n>=1 && n<=999)
{ int count=0;
while(n!=0)
{ count++ ;
n/=10;
// count++;
}
if(n%2==0 && count==1)
System.out.println("even single-digit number") ;
else if(n%2!=0 && count==1)
System.out.println("odd single-digit number") ;
else if(n%2==0 && count==2)
System.out.println("even two-digit number") ;
else if(n%2!=0 && count==2)
System.out.println("odd two-digit number") ;
else if(n%2==0 && count==3)
System.out.println("even three-digit number");
else if(n%2!=0 && count==3)
System.out.println("odd three-digit number");
}
}
}
my code gives error for odd values
Archived
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Valeriy Tselishchev
12 August 2020, 17:48
I think it can be & must be done more simply:
1. Determine one time is number even or odd & remember it to String
2. Determine how is it long => if <10 =>"single" else if <100 "two" and so on.
Add the result to the right side of the string from 1. Add "-digit number" -.> sout... And be happy
Without checking & print with the ternary operator, it takes me only two lines.
0
Samuel Schmid
8 August 2020, 10:36
The problem is on line 20, you divide "n" by 10 and assign it to "n". The while loop will only exit when "n" is zero. So when you test for even/odd at the end, "n" will always be zero, so it will always be even. That's why all your "odd" test cases are failing.
0