Knowee
Questions
Features
Study Tools

Write a program in java to input a natural number less than 1000 and display it in words.

Question

Write a program in java to input a natural number less than 1000 and display it in words.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that converts a number less than 1000 into words. This program uses an array of strings to map numbers to words.

public class NumberToWords {
    private static final String[] units = { "", "One", "Two", "Three", "Four",
        "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
        "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
        "Eighteen", "Nineteen" };

    private static final String[] tens = {
        "",         // 0
        "",         // 1
        "Twenty",   // 2
        "Thirty",   // 3
        "Forty",    // 4
        "Fifty",    // 5
        "Sixty",    // 6
        "Seventy",  // 7
        "Eighty",   // 8
        "Ninety"    // 9
    };

    public static String convert(final int n) {
        if (n < 0) {
            return "Minus " + convert(-n);
        }

        if (n < 20) {
            return units[n];
        }

        if (n < 100) {
            return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
        }

        return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
    }

    public static void main(final String[] args) {
        int n = 999;
        System.out.println(convert(n));
    }
}

This program will print "Nine Hundred Ninety Nine" for the input 999. You can change the value of n in the main method to test with different numbers.

This problem has been solved

Similar Questions

Java program that reads an integer between 0 and 1000 and adds all the digits in the integerWrite a Java program that reads an integer between 0 and 1000 and adds all the digits in the integer.An integer is a number that can be written without a fractional component. For example, 23, 6, 0, and −1245 are integers, while 3.25, ​7 1⁄2, and √3 are not. Click on the image for preview.Constraints:N/AExample:Inputan integer between 0 and 1000: 565Output:The sum of all digits in 565 is 16Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 56516

Design an algoritm and write a Java program with a class that read an integer ‘n’ as input and print the ‘n’ natural numbers in descending order sequence and their “Sum”. Print ‘invalid’ if the boundary conditions are not met.Boundary Condition: 0 < n > 1000Input Format:The first line contains an integer ‘n’Output Format:The first ‘n’ line contains the natural numbers in descending sequence or invalidThe ‘n+1’th line contains the Sum

Write a program that reads in a number (integer) as input, adds 1 to it, and then prints out the result. Here is an example interaction between your program and the user:Please enter a number: 34

Olivia likes to play with numbers. She told her friends about a game in which Olivia would tell them three numbers and they would have to find the smallest among them. As it was time-consuming, her friends decided to write a program to get three numbers and print the smallest one. Help them by developing an application using your Java skills.Note:Use ternary operator.If all 3 numbers are same then print "All numbers are equal".Negative and zero are also accepted as inputs and the smallest number has to be found accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers587543Sample Output 1:The smallest number is 43Sample Input 2:Enter the numbers8-74Sample Output 2:The smallest number is -7Sample Input 3:Enter the numbers555Sample Output 3:All numbers are equalSample Input 4:Enter the numbers8878Sample Output 4:The smallest number is 8

Kevin is trying to solve a number puzzle. If the number is greater than fifty, he wants the difference between the digits of the number; otherwise, if the number is less than or equal to fifty, he wants to reverse the number and find the difference in digits. Help Kevin to solve the problem using a Java program.Requirements:The input number should be a two-digit integer, or else display "Invalid number".Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input/Output 1:Enter the number56-1Explanation:56 is greater than 50, so subtract 5 - 6 = -1Sample Input/Output 2:Enter the number484Explanation:48 is less than 50, so reverse 48 to 84 then subtract 8 - 4 = 4Sample Input/Output 3:Enter the number9Invalid number

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.