﻿ PHP Exercise: Compute sum of the prime numbers less than 100 - w3resource # PHP Exercises: Compute sum of the prime numbers less than 100

## 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:

``````<?php
\$primes = array();
\$is_prime_no = false;
for (\$i = 2; \$i<100; \$i++) {
\$is_prime_no = true;
for (\$j = 2; \$j<=(\$i/2); \$j++) {
if (\$i%\$j==0) {
\$is_prime_no = false;
break;
}
}
if (\$is_prime_no) {
array_push(\$primes,\$i);
}
if (count(\$primes)==100) {
break;
}
}
echo array_sum(\$primes)."\n";
?>
```
```

Sample Output:

```1060
```

Flowchart: PHP Code Editor:

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

What is the difficulty level of this exercise?

﻿

## PHP: Tips of the Day

How do I strip all spaces out of a string in PHP?

Do you just mean spaces or all whitespace?

For just spaces, use str_replace:

`\$string = str_replace(' ', '', \$string);`

For all whitespace (including tabs and line ends), use preg_replace:

`\$string = preg_replace('/\s+/', '', \$string);`

Ref : https://bit.ly/2Cnb3QZ
{"ft_lang_label":"__label__en","ft_lang_prob":0.6408205,"math_prob":0.9559301,"size":1768,"snap":"2020-34-2020-40","text_gpt3_token_len":513,"char_repetition_ratio":0.10487528,"word_repetition_ratio":0.02970297,"special_character_ratio":0.3388009,"punctuation_ratio":0.20949721,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99398017,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-14T14:13:21Z\",\"WARC-Record-ID\":\"<urn:uuid:b5cc34a5-2fbd-44a2-8ecf-1f5f2514a737>\",\"Content-Length\":\"117444\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bf916f2b-75bb-43bc-88e2-8af975f2e0db>\",\"WARC-Concurrent-To\":\"<urn:uuid:a323290d-7b53-4f35-8e29-4ee4c394d0db>\",\"WARC-IP-Address\":\"172.67.69.127\",\"WARC-Target-URI\":\"https://www.w3resource.com/php-exercises/php-basic-exercise-37.php\",\"WARC-Payload-Digest\":\"sha1:KQ7MYS4VC7ELNAV2W6UHJGG6J5X5WWEG\",\"WARC-Block-Digest\":\"sha1:TTNNEICVLZLRJWGDO4SKWVEUQ6DSHWMU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439739328.66_warc_CC-MAIN-20200814130401-20200814160401-00355.warc.gz\"}"}