Slip No 24 Q A

Create a JSP page to accept a number from a user and display it in words:
Example: 123 – One Two Three. The output should be in red color.

<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
    <title>Number to Words</title>
</head>
<body>
    <h2>Enter a Number</h2>
    <form method="post">
        Number: <input type="text" name="number" required>
        <input type="submit" value="Convert">
    </form>

<%
    String num = request.getParameter("number");
    if (num != null && !num.isEmpty()) {
        String[] words = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < num.length(); i++) {
            char ch = num.charAt(i);
            if (Character.isDigit(ch)) {
                result.append(words[ch - '0']).append(" ");
            } else {
                result.append(ch).append(" "); // For non-digit characters like '-' etc.
            }
        }
%>
        <h3 style="color:red;">Number in Words: <%= result.toString().trim() %></h3>
<%
    }
%>
</body>
</html>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top