Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

program in java to print random letters from A to J

0 votes
how to print random letters from A to J in java
asked by ilma

1 Answer

0 votes

Math.random() generates a random double number between 0 (inclusive) and 1 (exclusive).

Multiplying it by 10 generates a number between 0 (inclusive) and 10 (exclusive).

Add the random number to 'A' to get a letter between 'A' and 'J'.

Program

public class RandomLetter {
    public static void main(String[] args) {
        int rand = (int) (Math.random() * 10);
        char randomChar = (char) ('A' + rand);
        System.out.println("Random letter between A and J = " + randomChar);
    }
}

Sample output

Random letter between A and J = C

 

answered by Ranjith (2,683 points)
...