numbers

Pyramid of stars in PHP

Write a program to generate pyramid of stars.

This is the most commonly asked question to beginners. Though this seems to be very complex during my school days, but not.

Program to generate/draw pyramid of stars/asterisks in php

<?php
echo '<pre>';
$horizontal = 10;
$vertical = 10;
while ($vertical--)
echo str_repeat(' ', $vertical) . str_repeat('* ', $horizontal - $vertical) . "\n";
echo '</pre>';
?>

Prime number in PHP

A prime number is a natural number/whole number/cardinal number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
e.g. 2, 3, 5, 7, 11, 13, 17….etc are prime numbers; while 4, 6, 8, 9, 10, 12, 14, 15, 16….etc are composite numbers.

Program to determine if the supplied number is Prime or Composite using native bcmod() function.

<?php
$num = 26; // desired number to check
for ( $i = 2; $i <= $num - 1; $i++ ) {
if ( bcmod( $num, $i ) == 0 ) {
echo $num . " is composite number";
break;
}
}
if ( $num == $i )
echo $num . " is prime number";
?>