Post Views: 3
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.
Answer
<%@ 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>Sum of First and Last Digit</h2>
<form method="post">
Enter Number:
<input type="text" name="num" required>
<input type="submit" value="Calculate">
</form>
<%
if (request.getParameter("num") != null) {
String numberStr = request.getParameter("num");
int number = Integer.parseInt(numberStr);
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>