-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractical28.html
50 lines (46 loc) · 1.67 KB
/
Practical28.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!--JavaScript Program to Find if a number is prime. If the number is not prime, then display
the Factors of a Number.-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Check and Factors</title>
</head>
<body>
<script>
// Program to check if a number is prime or not and find its factors
// Take input from the user
const number = parseInt(prompt("Enter a positive number: "));
let isPrime = true;
// Check if number is equal to 1
if (number === 1) {
document.write("1 is neither prime nor a composite number.");
} else if (number > 1) {
// Looping through 2 to number/2
for (let i = 2; i <= number / 2; i++) {
if (number % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
document.write(`${number} is a prime number.<br>`);
} else {
document.write(`${number} is not a prime number.<br>`);
// Program to find the factors of the number
document.write(`The factors of ${number} are:<br>`);
// Looping through 1 to number
for (let i = 1; i <= number; i++) {
// Check if number is a factor
if (number % i === 0) {
document.write(`${i} `);
}
}
}
} else {
document.write("Invalid input. Please enter a positive number.");
}
</script>
</body>
</html>