SHARE
TWEET

07. Sum Prime Non Prime

MartinGeorgiev Dec 11th, 2018 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $primeSum = 0;
  3. $nonPrimeSum = 0;
  4.  
  5. while (1){
  6.     $input = readline();
  7.     if ($input == 'stop'){
  8.         break;
  9.     }
  10.     if ($input < 0) {
  11.         echo 'Number is negative.'. PHP_EOL;
  12.         continue;
  13.     }
  14.     if ($input == 1) {
  15.         $nonPrimeSum++;
  16.         continue;
  17.     }
  18.      $isPrime = true;
  19.     for($i = 2; $i < $input; $i++){
  20.         if($input % $i == 0){
  21.             $isPrime = false;
  22.             break;
  23.         }
  24.     }
  25.  
  26.     if ($isPrime){
  27.         $primeSum+= $input;
  28.     }else {
  29.         $nonPrimeSum += $input;
  30.     }
  31. }
  32.  
  33. echo "Sum of all prime numbers is: $primeSum\n";
  34. echo "Sum of all non prime numbers is: $nonPrimeSum\n";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top