C++ PROGRAM. (C++ INTRO QUESTION)
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows:
A = 55000
B = A + 5000
Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29.
Rules:
You should first create a boolean function called isPrime and use that function in your program. This function should take in any int and return true if the number is prime, otherwise, return a false. In the main body of your program, you should create a loop from A to B (inclusive) and use isPrime function to determine if the loop number should be counted or not.
Your program SHOULD NOT PRINT the individual prime numbers. You can print them for your own testing purpose, but in the final submission, comment out such print statements.
Your program SHOULD ONLY PRINT the answer -- which is a
number.
C++ code:
#include <iostream>
using namespace std;
//initializing isPrime function
bool isPrime(int num){
//looping from 2 to
number
for(int
i=2;i<num;i++)
//checking if the number is not prime
if(num%i==0)
//returning false
return false;
//returning true
return true;
}
int main(){
//initializing count,A,B
int count=0,A=55000,B=A+5000;
//looping from A to B
for(int i=A;i<=B;i++)
//calling isPrime and
checking if it is prime
if(isPrime(i))
//incremenitng count
count++;
//printing count
cout<<count<<endl;
return 0;
}
Screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.