0% found this document useful (0 votes)
2 views4 pages

Exp10 JavaScript

The document outlines the creation of an HTML page featuring a textbox and four buttons for mathematical operations: Factorial, Fibonacci, Prime, and Palindrome. It provides a step-by-step procedure for coding in a text editor, saving the file, and testing it in a web browser. The source code includes JavaScript functions for each operation, handling user input and displaying results dynamically.

Uploaded by

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

Exp10 JavaScript

The document outlines the creation of an HTML page featuring a textbox and four buttons for mathematical operations: Factorial, Fibonacci, Prime, and Palindrome. It provides a step-by-step procedure for coding in a text editor, saving the file, and testing it in a web browser. The source code includes JavaScript functions for each operation, handling user input and displaying results dynamically.

Uploaded by

vu.241fa04239
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Aim: -

To create an HTML page with a textbox and four buttons (Factorial, Fibonacci, Prime,
Palindrome) where each button triggers respective JavaScript functions.

Procedure
1. Open a text editor (Notepad++ or VS Code).

2. Write the HTML, CSS and JavaScript code as given in the Source Code section.

3. Save the file with .html extension.

4. Open the file in a web browser.

5. Test the output and ensure it meets the given requirements.

Software Requirements:

1. Text Editor (VS Code, Sublime Text, Notepad++, etc.)

2. Web Browser (Chrome, Firefox, Edge, etc.)

Source Code
<!DOCTYPE html>

<html>

<head>

<title>Number Operations</title>

<script>

function getNumber() {

let num = parseInt([Link]("num").value);

if (isNaN(num)) {

[Link]("result").innerHTML = "Please enter a valid number.";

return null;

return num;

}
function factorial() {

let num = getNumber();

if (num === null) return;

if (num < 0) {

[Link]("result").innerHTML = "Factorial is not defined for


negative numbers.";

return;

let fact = 1;

for (let i = 1; i <= num; i++) {

fact *= i;

[Link]("result").innerHTML = "Factorial of " + num + " is: " + fact;

function fibonacci() {

let num = getNumber();

if (num === null) return;

let a = 0, b = 1, series = [];

for (let i = 0; i < num; i++) {

[Link](a);

let temp = a + b;

a = b;

b = temp;

[Link]("result").innerHTML = "Fibonacci series up to " + num + "


terms: " + [Link](", ");
}

function primeNumbers() {

let num = getNumber();

if (num === null) return;

let primes = [];

for (let i = 2; i <= num; i++) {

let isPrime = true;

for (let j = 2; j <= [Link](i); j++) {

if (i % j === 0) {

isPrime = false;

break;

if (isPrime) [Link](i);

[Link]("result").innerHTML = "Prime numbers up to " + num + ":


" + [Link](", ");

function palindrome() {

let numStr = [Link]("num").value;

if ([Link]() === "") {

[Link]("result").innerHTML = "Please enter a value.";

return;

}
let reversed = [Link]("").reverse().join("");

if (numStr === reversed) {

[Link]("result").innerHTML = numStr + " is a Palindrome.";

} else {

[Link]("result").innerHTML = numStr + " is NOT a Palindrome.";

</script>

</head>

<body>

<h2>Number Operations</h2>

<input type="text" id="num" placeholder="Enter a number">

<br><br>

<button onclick="factorial()">Factorial</button>

<button onclick="fibonacci()">Fibonacci</button>

<button onclick="primeNumbers()">Prime</button>

<button onclick="palindrome()">Palindrome</button>

<br><br>

<div id="result" style="font-weight: bold; color: blue;"></div>

</body>

</html>

You might also like