Write a program to display the last digit of a number(any number %10 will return the last digit

Here's an example program that displays the last digit of a number using HTML, CSS, and JavaScript:


1. Create the HTML structure in a file named `last_digit.html`:

html
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link rel="stylesheet" href="style.css">
		<title>Last Digit Display</title>
	</head>
	<body>
		<div class="container">
			<h1>Last Digit Display</h1>
			<label for="numberInput">Enter a number:</label>
			<input type="number" id="numberInput">
			<button id="checkButton">Get Last Digit</button>
			<p id="result"></p>
		</div>

		<script src="script.js"></script>
	</body>
</html>

2. Create the CSS styling in a file named `style.css`:

css
body {
	font-family: Arial, sans-serif;
	background-color: #f4f4f4;
	margin: 0;
	padding: 0;
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
}

.container {
	text-align: center;
	background-color: #fff;
	padding: 20px;
	border-radius: 5px;
	box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}

button {
	padding: 10px 20px;
	background-color: #007bff;
	color: #fff;
	border: none;
	border-radius: 5px;
	cursor: pointer;
	}

button:hover {
	background-color: #0056b3;
}

3. Create the JavaScript logic in a file named `script.js`:

javascript
document.addEventListener("DOMContentLoaded", function () {
	const numberInput = document.getElementById("numberInput");
	const checkButton = document.getElementById("checkButton");
	const result = document.getElementById("result");

	checkButton.addEventListener("click", function () {
		const number = parseInt(numberInput.value);

		if (isNaN(number)) {
			result.textContent = "Please enter a valid number.";
			return;
		}

		const lastDigit = number % 10;
		result.textContent = `Last digit: ${lastDigit}`;
	});
});

4. Run a Python web server to host the files. Open your terminal and navigate to the directory containing the HTML, CSS, and JavaScript files, then run the following command:

python -m http.server


5. Open your web browser and go to `http://localhost:8000/last_digit.html`. You should see the last digit display form. Enter a number and click the "Get Last Digit" button to see the result.


Output:
img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext