#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
usingnamespace std;
int main()
{
// Function A: Numbers divisible by 5, 6, 9, and 10
int k, Hold;
k = 0;
while ( k < 20 )
{
Hold = rand();
if ( Hold > 101 && Hold < 3000 && Hold % 5 == 0 && Hold % 6 == 0 && Hold % 9 == 0 && Hold % 10 == 0 )
{
cout << setw(4) << Hold << endl;
k = k+1;
}
}
}
However, can someone please help me with code on how to calculate prime numbers. There must be two different functions: One must be a list of 20 random, prime numbers greater than 100 and less than 2000. The other function must be a list od 20, random, prime numbers greater than 200 and less than 5000.
Please try to use code, similar to the one shown above.
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<cmath>
using std::sqrt;
bool prime(int number); //function prototype
int main(){
int numberOfPrimes=0;
int firstNumber;
int secondNumber;
cout<<"Find primes numbers from: ";
cin>>firstNumber;
cout<<"To(greater than the first number please!): ";
cin>>secondNumber;
cout<<'\n';
for(int number=firstNumber;number<=secondNumber;number++){
if(prime(number)==true){
++numberOfPrimes;
cout<<"["<<number<<"] is prime"<<endl;
}//end if
}//end loop for
cout<<"\nFound: "<<numberOfPrimes<<endl;
return 0; //indicates success
}//end main
bool prime(int number){
bool isPrime=false;
int divisibleNumbers=1;
int limit=sqrt(number);
int notPrime=0;
for(int counter=1;counter<=limit;counter++){
if(number%counter==0)
if(counter==1){
divisibleNumbers++;
}else{
notPrime++;
}//end if...else
}//end loop for
if((divisibleNumbers==2&¬Prime==0)&&number!=1){
isPrime=true;
}//end if
return isPrime;
}//end function prime
Find primes numbers from: 2
To(greater than the first number please!): 10
[2] is prime
[3] is prime
[5] is prime
[7] is prime
Found: 4