Slip No 7 Q A

Write a JSP program to calculate sum of first and last digit of a given number. Display sum in Red Color with font size 18.

sumFirstLast.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
    <title>Sum of First and Last Digit</title>
</head>
<body>

<h2>Calculate Sum of First and Last Digit</h2>

<form method="post">
    Enter Number:
    <input type="text" name="num" required>
    <input type="submit" value="Calculate">
</form>

<%
    String n = request.getParameter("num");

    if (n != null) {
        int number = Integer.parseInt(n);

        int lastDigit = number % 10;
        int firstDigit = number;

        while (firstDigit >= 10) {
            firstDigit = firstDigit / 10;
        }

        int sum = firstDigit + lastDigit;
%>

<p style="color:red; font-size:18px;">
    Sum of First Digit (<%= firstDigit %>) and Last Digit (<%= lastDigit %>) is:
    <b><%= sum %></b>
</p>

<%
    }
%>

</body>
</html>
Spread the love

Leave a Comment

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

Scroll to Top