PHP Program to find Composite Number

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.

NumberDivisorResult
131, 13Not a Composite Number
151, 3, 5, 15Composite Number
471, 47Not 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

PHP Compiler
<?php $num = 12; $count = 0; for($i=1; $i<=$num; $i++) { if($num % $i == 0) $count++; } if($count > 2) printf("$num is a composite number"); else printf("$num is not a composite number"); ?>

Using while loop

In the following example, we will check whether the number 12 is a Composite number or not using while loop.

Example

PHP Compiler
<?php $num = 12; $count = 0; $i = 1; while($num >= $i) { if($num % $i == 0) $count++; $i++; } if($count > 2) printf("$num is a composite number"); else printf("$num is not a composite number"); ?>

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

PHP Compiler
<?php $num = 12; $count = 0; $i = 1; do{ if($num % $i == 0) $count++; $i++; }while($i<= $num); if($count > 2) printf("$num is a composite number"); else printf("$num is not a composite number"); ?>

Composite Numbers between the Given Range

In the following example, we will find all the Composite numbers between 1 and 10.

Example

PHP Compiler
<?php $start = 1; $end = 10; $count = 0; $i = 1; echo "Composite Numbers between $start and $end: <br>"; for($start=$start; $start<=$end; $start++) { for($i=1; $i<=$start; $i++) { if($start % $i == 0) $count++; } if($count > 2) echo "$start "; $count = 0; } echo "<br>"; ?>

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

PHP Compiler
<?php $num = 12; $count = 0; for($i=1; $i<=$num; $i++) { if($num % $i == 0) $count++; } if($count == 2) printf("$num is a prime number"); else printf("$num is a composite number"); ?>

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