0% found this document useful (0 votes)
26 views5 pages

Ajava PBL

Advance Java

Uploaded by

ppkkddpp179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Ajava PBL

Advance Java

Uploaded by

ppkkddpp179
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ADVANCE JAVA PROGRAMMING

PROJECT BASED LEARNING

Topic : Find the one digit to be removed to form palindrome

Submitted by –
Praveen Kumar(06511503121)
Index.jsp
<!DOCTYPE html>
<html>
<head>
<title>Find Digit to Remove for Palindrome</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f0f0;
}

h1 {
text-align: center;
color: #3498db;
}

form {
display: flex;
flex-direction: column;
align-items: center;
margin: 10px auto;
padding: 20px;
border-radius: 5px;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

input[type="number"] {
width: 200px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 16px;
}

input[type="submit"] {
background-color: #3498db;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
}

input[type="submit"]:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<h1>Find Digit to Remove for Palindrome</h1>
<form method="post" action="findDigit">
Enter a number: <input type="number" name="number" required>
<br><br>
<input type="submit" value="Find Digit">
</form>
</body>
</html>

result.jsp

<!DOCTYPE html>
<html>
<head>
<title>Result</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}

h1 {
color: #00698f;
}

#result {
font-size: 24px;
font-weight: bold;
color: #009688;
}
</style>
</head>
<body>
<h1>Result</h1>
<p id="result"><%= request.getAttribute("message") %></p>
</body>
</html>

FindDigit.java
package com.example;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/findDigit")
public class FindDigit extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
int number = Integer.parseInt(request.getParameter("number"));
int digitToRemove = findDigitToRemove(number);

String message;
if (digitToRemove != -1) {
message = "Digit " + digitToRemove + " needs to be removed to
form a palindrome.";
} else {
message = "The number " + number + " is already a
palindrome.";
}

request.setAttribute("message", message);
request.getRequestDispatcher("/result.jsp").forward(request,
response);
}

private int findDigitToRemove(int number) {


String numberStr = String.valueOf(number);
int left = 0;
int right = numberStr.length() - 1;

while (left < right) {


if (numberStr.charAt(left) != numberStr.charAt(right)) {
if (isPalindrome(numberStr.substring(left + 1, right +
1))) {
return
Character.getNumericValue(numberStr.charAt(left));
}
if (isPalindrome(numberStr.substring(left, right))) {
return
Character.getNumericValue(numberStr.charAt(right));
}
return -1;
}
left++;
right--;
}

return -1;
}

private boolean isPalindrome(String str) {


int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - i - 1)) {
return false;
}
}
return true;
}
}

OUTPUT

You might also like