Advertisement
Porr011

Ch 9 lesson 3

Jan 29th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. //this program finds all the prime numbers betwen 0 and 101
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5. #include <iomanip>
  6. #include <stdio.h>
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11.     int num, divisors, prime=0, count, three, sum, rest;
  12.    
  13.    
  14.     for (num=0; num<=101; num++){ // this for loop finds the sum of all numbers between 1 to 101.
  15.        
  16.         sum=sum+num;
  17.        
  18.     }
  19.    
  20.     cout << "This code will show the sum of all integers from 0 to 101: They are  " << sum << endl;
  21.    
  22.     for (num=0; num<=101; num++){ // this for loop finds all the sum of all multiples of three.
  23.        
  24.         if (num%3<1){
  25.            
  26.             three=three+num;
  27.            
  28.         }
  29.        
  30.     }
  31.    
  32.     cout << "This code will find and display of sum of all multiples of three from 0 to 101: They are " << three << endl;
  33.    
  34.     for (num=0; num<=101; num++){ // this loop finds the sum of all prime numbers between 1 and 101.
  35.        
  36.         if (num>1){
  37.            count=num;
  38.         }
  39.        
  40.         for (divisors=2; divisors<num; divisors++){
  41.            
  42.             rest=num%divisors;
  43.             if (rest<1){  // this is the if statement that makes the added value 0 if the tested number has anny divisors.
  44.                
  45.                count=0;
  46.                
  47.             }
  48.            
  49.         }
  50.        
  51.         prime=prime+count;
  52.     }
  53.    
  54.     cout << "This will display the sum of all primes from 0 to 101: they are " << prime << endl;
  55.    
  56.    
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement