// accept numbers from 0.1 to 99.9 then count the number of tens, ones, and tenths

import java.io.*;

public class accept99_9 {

    public static void main(String[] args)throws IOException {

        BufferedReader abc=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a value: ");
        String input=abc.readLine();
        float v=Float.parseFloat(input);
        float tens=v %100;
        float ones=v %10;
        float tenths=v %10;
        System.out.println("Number of tens: "+tens);
        System.out.println("Number of ones: "+ones);
        System.out.println("Number of tenths: "+tenths);
        System.exit(0);
    }
}

How will i come up with an output like these?

example....
Enter a value: 12.3
Number of tens: 1
Number of ones: 2
Number of tenths: 3

thanks........

Dani AI

Generated

’s integer-casting idea is the right direction: isolate digits by treating the number as an integer after appropriate scaling rather than trying to read decimal digits directly out of a raw float. The original attempt used modulo on floating-point values, which is fragile because floating-point stores decimal numbers approximately (for example 12.3 may be represented internally as 12.2999999...), so digit extraction can give surprising results on edge cases.

A safer, simpler approach is to work from the textual input (or use exact decimal arithmetic). Parse and validate the input string, split on the decimal point, then pick characters for tens/ones from the integer part and the first fractional character for the tenths place. This eliminates binary-floating rounding problems and is easy to reason about.

Example (string-based extraction):

String s = input.trim();
if (!s.matches("^[0-9]{1,2}(\\.[0-9]+)?$")) {
    // invalid or out-of-range (reject or handle)
}
String[] parts = s.split("\\.");
String intPart  = parts[0];
String fracPart = (parts.length > 1) ? parts[1] : "0";

int ones   = intPart.charAt(intPart.length() - 1) - '0';
int tens   = (intPart.length() >= 2) ? intPart.charAt(intPart.length() - 2) - '0' : 0;
int tenths = (fracPart.length() > 0) ? fracPart.charAt(0) - '0' : 0;

Notes and troubleshooting

  • If inputs with more than one fractional digit must be supported, decide whether to truncate or round first. For rounding, use BigDecimal (exact decimal rounding) and then extract digits from the scaled integer.
  • Validate negatives and out-of-range values (the thread expects 0.1–99.9).
  • ’s compact integer-math solution is fine for most simple cases; for production code that must be correct on every decimal input, prefer the string method or BigDecimal to avoid subtle float errors.

Recommended Answers

All 2 Replies

you're looking for ints, and the last one must be mutiplied by 10 other wise the int value will always be 0 (obviously) and the first one divided by ten or it will be e.g. 10 instead of 1.

int tens=(int) ((v / 10) % 10);
int ones=(int) (v % 10);
int tenths=(int) ((v * 10) % 10);

........wow........it worked......thanks a lot for your help sir masijade.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.