<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>prime number</title>
</head>
<body>
<input type="text" name="" id="given_value">
<button onclick="output()">Show prime</button>
<p>Result</p>
<div id="result"></div>
<script>
function is_prime(number){
check = true;
if(number > 1){
var range = parseInt(number/2);
for(var i = 2; i <= range; i++){
if(number % i == 0){
check = false;
break;
}
}
} else {
check = false;
}
return check;
}
function show_prime(given_number){
var count = 0;
var result;
for (var k= 2; k <= given_number; k++){
if(is_prime(k)){
count++;
if(result == undefined){
result = k;
} else {
result = result + "<br> " + k;
}
}
}
result = result + "<br>" + "Total prime number: <br>" + count;
return result;
}
function output(){
var result;
var given_value = parseInt(document.getElementById('given_value').value);
if(isNaN(given_value)){
result = "Your given value must be 2 and afterwords integer";
}else {
result = show_prime(given_value);
}
document.getElementById('result').innerHTML = result;
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>prime number</title>
</head>
<body>
<input type="text" name="" id="given_value">
<button onclick="output()">Show prime</button>
<p>Result</p>
<div id="result"></div>
<script>
function is_prime(number){
check = true;
if(number > 1){
var range = parseInt(number/2);
for(var i = 2; i <= range; i++){
if(number % i == 0){
check = false;
break;
}
}
} else {
check = false;
}
return check;
}
function show_prime(given_number){
var count = 0;
var result;
for (var k= 2; k <= given_number; k++){
if(is_prime(k)){
count++;
if(result == undefined){
result = k;
} else {
result = result + "<br> " + k;
}
}
}
result = result + "<br>" + "Total prime number: <br>" + count;
return result;
}
function output(){
var result;
var given_value = parseInt(document.getElementById('given_value').value);
if(isNaN(given_value)){
result = "Your given value must be 2 and afterwords integer";
}else {
result = show_prime(given_value);
}
document.getElementById('result').innerHTML = result;
}
</script>
</body>
</html>
No comments:
Post a Comment