Write a php program to compute the sum of the prime numbers less than 100 using do while loop
PHP: Exercise-37 with SolutionWrite a PHP program to compute the sum of the prime numbers less than 100. Show Note: There are 25 prime numbers are there in less than 100. Sample Solution: - PHP Code:
Sample Output: 1060 Flowchart: 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. PHP: Tips of the DayPHP: 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 ?
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
Output:
answered Nov 29, 2014 at 17:26
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:
Then, when you're done, you can do your printing mechanism:
And then you can do your summing mechanism:
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:
answered Nov 29, 2014 at 17:29
MH2K9MH2K9 11.8k7 gold badges32 silver badges48 bronze badges ";
for($j=0;$j
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.
|