JavaScript Program to find Composite Number
February 8, 2021 10:45 am IST
What is Composite Number? A positive integer that has at least one divisor other than 1 and itself.
Tips : It is recommended to use our online Composite Numbers calculator for better understanding.
Examples The following table provides few examples of composite numbers.
Number Divisor Result 13 1, 13 Not a Composite Number 15 1, 3, 5, 15 Composite Number 47 1, 47 Not a Composite Number
Using for loop In the following example, we will check whether the number 12 is a Composite number or not using for loop .
Example <!DOCTYPE html>
<html >
<body >
<h1 >JS Composite Number</h1 >
<script >
var num = 12 ;
var i ;
var count = 0 ;
for (i = 1 ; i <= num ; i ++ )
{
if (num % i == 0 )
count ++ ;
}
if (count > 2 )
document .write (num + " is a composite number" );
else
document .write (num + " is not a composite number" );
</script >
</body >
</html >
Using while loop In the following example, we will check whether the number 12 is a Composite number or not using while loop .
Example <!DOCTYPE html>
<html >
<body >
<h1 >JS Composite Number</h1 >
<script >
var num = 12 ;
var i = 1 ;
var count = 0 ;
while (num >= i )
{
if (num % i == 0 )
count ++ ;
i ++ ;
}
if (count > 2 )
document .write (num + " is a composite number" );
else
document .write (num + " is not a composite number" );
</script >
</body >
</html >
Using do while loop In the following example, we will check whether the number 12 is a Composite number or not using do while loop .
Example <!DOCTYPE html>
<html >
<body >
<h1 >JS Composite Number</h1 >
<script >
var num = 12 ;
var i = 1 ;
var count = 0 ;
do {
if (num % i == 0 )
count ++ ;
i ++ ;
}while (i <= num );
if (count > 2 )
document .write (num + " is a composite number" );
else
document .write (num + " is not a composite number" );
</script >
</body >
</html >
Composite Numbers between the Given Range In the following example, we will find all the Composite numbers between 1 and 10.
Example <!DOCTYPE html>
<html >
<body >
<h1 >JS Find All Composite Numbers</h1 >
<script >
var start = 1 ;
var end = 10 ;
var count = 0 ;
var i = 1 ;
var txt = "" ;
document .write ("Composite Numbers between " + start + " to " + end + ":" );
for (start = start ; start <= end ; start ++ )
{
for (i = 1 ; i <= start ; i ++ )
{
if (start % i == 0 )
count ++ ;
}
if (count > 2 )
txt += start + ", " ;
count = 0 ;
}
document .write ("<br>" + txt );
</script >
</body >
</html >
Check Whether the Number is Prime or Composite In the following example, we will check whether the number 12 is a Prime number or Composite number.
Example <!DOCTYPE html>
<html >
<body >
<h1 >JS Prime Number or Composite Number</h1 >
<script >
var num = 12 ;
var i ;
var count = 0 ;
for (i = 1 ; i <= num ; i ++ )
{
if (num % i == 0 )
count ++ ;
}
if (count == 2 )
document .write (num + " is a prime number" );
else
document .write (num + " is a composite number" );
</script >
</body >
</html >
Reminder Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript .
Please do google search for:
Join Our Channel Join our telegram channel to get an instant update on depreciation and new features on HTML , CSS , JavaScript , jQuery , Node.js , PHP and Python .
This channel is primarily useful for Full Stack Web Developer.
Share this Page Meet the Author