Question

In: Computer Science

Write a c++ program that prints the count of all prime numbers between A and B...

Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows:

A = Any 5 digit unique number

B = A + 1000

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:

  1. 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.

  2. 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.

  3. Your program SHOULD ONLY PRINT the answer -- which is a number.

Solutions

Expert Solution

I WROTE THE CODE ALONG WITH THE COMMENTS

CODE:

#include <iostream>

using namespace std;
bool isPrime(int num)
{
//variables declaration.
int i=2,count=0;
  
for(i=2;i<num;i++)
{
if(num%i==0) //condition number is divisible or not
count++;
}
  
  
if(count==0) //count == 0 means number is prime.
return true;
else
return false;
}

int main()
{
  
//variables declaration.
int A,B,i,count=0;
  
  
cout<<"Enter five digit number: ";
cin>>A; //scan A.
  
B=A+1000;
  
  
for(i=A;i<=B;i++) //for loop used to take each number form A to B.
{
if(isPrime(i)) //calling the function isPrime it returns true
{
count++; //increment count value
}
}
  
cout<<count<<" prime numbers between "<<A<<" and "<<B;

return 0;
}

OUTPUT:

SCREENSHOT OF THE CODE:


Related Solutions

Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the while loop. question2: Write a C++ program that prints all numbers between 10 to 1000 that are divisible by 5
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
Use "do...while" loops to write a program that finds all prime numbers less than a specified...
Use "do...while" loops to write a program that finds all prime numbers less than a specified value.
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT