Write a php program to compute the sum of the prime numbers less than 100 using do while loop

Last update on August 19 2022 21:50:29 (UTC/GMT +8 hours)

PHP: Exercise-37 with Solution

Write a PHP program to compute the sum of the prime numbers less than 100.

Note: There are 25 prime numbers are there in less than 100.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 and sum of all these numbers is 1060.

Sample Solution: -

PHP Code:



Sample Output:

1060       

Flowchart:

Write a php program to compute the sum of the prime numbers less than 100 using do while loop

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to test if a given string occurs at the end of another given string.
Next: Write a PHP program to valid an email address.

PHP: Tips of the Day

PHP: How to find the last day of the month from date?

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

Ref : https://bit.ly/31coSLD

I have the following code, to output all prime numbers from array. I would like to get the sum of the output in ex: 2+3+5 = 10, Any hint how to get that ?

$n = array(1,2,3,4,5,6);

function prime($n){


    for($i=0;$i<= count($n);$i++){

        $counter = 0; 
        for($j=1;$j<=$i;$j++){ 

            if($i % $j==0){ 

                $counter++;
            }
        }

        if($counter == 2){

            print $i."
"; } } } print prime($n);

Write a php program to compute the sum of the prime numbers less than 100 using do while loop

Rizier123

57.8k16 gold badges91 silver badges142 bronze badges

asked Nov 29, 2014 at 17:16

3

Then this should work for you:

(Here i used $sum which i initialized before the foreach loop and then used the += operator to add the sum together)

 $v) {
            $counter = 0; 
            for($j = 1; $j <= $v; $j++) { 
                if($v % $j == 0)
                    $counter++;

            }

            if($counter == 2) {
                echo $v."
"; $sum += $v; } } echo "Sum: " . $sum; } prime($n); ?>

Output:

2
3
5
Sum: 10

answered Nov 29, 2014 at 17:26

Write a php program to compute the sum of the prime numbers less than 100 using do while loop

Rizier123Rizier123

57.8k16 gold badges91 silver badges142 bronze badges

3

As @IMSoP commented above, one option is to compile the list of primes into a new array:

$m = [];

// looping code...

// If prime:
array_push( $m, $primeNumber );

Then, when you're done, you can do your printing mechanism:

print implode( "
", $m );

And then you can do your summing mechanism:

print "

Sum: " . array_sum( $m ) . "

";

The added benefit here is you can split out each piece of functionality into it's own function or method (which you should do to have a good design).

answered Nov 29, 2014 at 17:30

rockerestrockerest

10.3k3 gold badges36 silver badges67 bronze badges

1

try this

answered Nov 29, 2014 at 17:31

jay.jivanijay.jivani

1,5541 gold badge15 silver badges33 bronze badges

You can try something like this:

function isPrime($n){
    if($n == 1) return false;
    if($n == 2) return true;
    for($x = 2; $x <= sqrt($n); $x++){
        if($n % $x == 0) return false;
    }
    return true;
}

$sum = 0;
$n = array(1,2,3,4,5,6);
foreach($n as $val){
    if(isPrime($val)) {
        echo $val . "
"; $sum += $val; } } echo "Sum: " . $sum;

answered Nov 29, 2014 at 17:29

MH2K9MH2K9

11.8k7 gold badges32 silver badges48 bronze badges

";
    print_r($prime_no);
    echo  "
"; for($j=0;$j

Write a php program to compute the sum of the prime numbers less than 100 using do while loop

answered Sep 14, 2016 at 2:18

What is prime number in php?

A number which is only divisible by 1 and itself is called prime number. Numbers 2, 3, 5, 7, 11, 13, 17, etc. are prime numbers.

How can I print prime numbers from 1 to 100 in php?

php function checkPrime($num) { if ($num == 1) return 0; for ($i = 2; $i <= $num/2; $i++) { if ($num % $i == 0) return 0; } return 1; } echo '

Prime Numbers between 1 and 100

'; for($num = 1; $num <= 100; $num++) { $flag = checkPrime($num); if ($flag == 1) { echo $num." "; } } ?>

How many first 100 prime numbers are there?

There are 25 prime numbers between 1 to 100 which are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.

What is the sum of prime numbers from 1 to 100?

when i run this program and enter 100 it's showing the result is 1058 But the sum of all prime numbers upto 100 must be 1060.