Home  • Programming • JavaScript

How to check prime number in JavaScript?

you-can-follow-my-code You can follow my code step by step. And I hope Finally you can understand the logical matter to find prime number. so lets start step by step.. Step-1: write the following HTML code like:
<div class="content"> 
	<div class="leftCol"> 
    	<input type="text" placeholder="number" id="InputNumber"/>
    </div>
	<div class="buttonCol"> 
    	<input type="button" value="Check" onclick="isPrime()"/>
    </div>
	<div class="rightCol">     	
    	<textarea placeholder="Show Output" rows="5" cols="24" id="outputNumber" disabled=""></textarea>
    </div>
</div>
Step-2: write the following style code for style like:
<style> 
	.content{width: 600px;margin:20px auto;}
	input,textarea{font-size:16px; padding:6px;}
	.leftCol input{width:100%; margin-bottom:10px}
	.buttonCol input{width:20%; margin-bottom:10px;}
	.rightCol textarea{width:100%;);}
</style>
Step-3: And Finally write the following JavaScript code for find prime Number:
<script> 
	function isPrime(){ 
		var n=document.getElementById('InputNumber').value;
		var prime="This is prime number";
		for(i=2;i<n;i++){ 
			if(n%i==0){ 
				prime="This is not a Prime Number";
				break;
			}// end if
		}// end loop
		show=document.getElementById('outputNumber').value=prime;
		return show;
	}//end function

</script>
Thanks for trying this logical problem solution tricks. Please like and share for inspire me for future new logical post.

Comments 0


About Author
MD. Shafiqul Islam
Copyright © 2024. Powered by Intellect Software Ltd