Thursday, April 18, 2013

perfect number check using php

A perfect number is a positive integer where the sum of all its positive divisors, except itself, is equal to the number itself. For example 6 is a perfect number as 1,2 and3 are its divisors and the sum of divisors=1+2+3=6. Here we have created a program that will take the number from the user and reports whether it is perfect or not

<?php
$num=6;
$perfectNo =0;
        for ($i = 1;$i < $num; $i++) {
            if ($num % $i == 0) {
                $perfectNo += $i;
               
            }
        }
        if ($perfectNo == $num) {
            echo("number is a perfect number");
        } else {
            echo("number is not a perfect number");
        }

No comments:

Post a Comment