← Back to Forum

Prime numbers using js

Posted on December 5, 2024, 1:14 pm


<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body class="bg-teal-500 flex items-center justify-center min-h-screen">
<div class="bg-white rounded-lg shadow-lg p-8 w-full max-w-sm">
<div class="bg-teal-600 text-white text-center py-4 rounded-t-lg">
<h2 class="text-xl font-semibold">Prime Identifier</h2>
</div>
<div class="flex items-center border border-white rounded-lg overflow-hidden">

<input type="text" id="num" placeholder="Enter a number" class="w-full p-3 focus:outline-none">
</div>

<button onclick="primecheck()" class="w-full bg-teal-600 text-white py-3 rounded-lg font-semibold">Check</button>
<center> <b id="res"></b></center>

</div>
<script>
function primecheck()
{
var i,flag;
flag = 0;
var x = parseInt(document.getElementById("num").value);
if(x<=1)
{
document.getElementById("res").innerHTML = "Prime Number";
}
for(i=2;i<x;i++)
{
if(x%i==0)
{
flag = 1;
}
}

if(flag==1)
{
document.getElementById("res").innerHTML = "Non Prime Number";
}
else
{
document.getElementById("res").innerHTML = "Prime Number";
}
}
</script>
</body>
</html>


Add a Reply


Replies (0)

No replies yet. Be the first to reply!