Write a program that asks the user for two non-negative integers and then displays a message indicating the number of primes found between the two numbers entered by the user (including those two numbers).
You will count the original numbers in your consideration.
You may assume the following: first number < second number.
A prime number is an integer that has no factors other than one and itself.
Output should look similar to:
Enter starting number: 50
Enter ending number: 59
Number of primes in range: 2
Hint: You cannot determine that a number is prime until you have evaluated all the potential factors, but the moment you find a factor, you can say, for certain, that it is NOT prime.
My program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
using namespace std;
int main()
{
int a, b, i, j, flag;
cout << "Enter starting number: ";
cin >> a;
cout << "Enter ending number: ";
cin >> b;
cout << "Numbers of prime in range: "
<< a << " and " << b << " are: ";
for (i = a; i <= b; i++) {
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
cout << i << " ";
}
return 0;
}
| |
When it says "Number of primes in range:" The output does not give how many numbers there are between the 2, it just gives the numbers. For ex. If I enter 50 and 59, the output doesnt give "Number of primes in range: 2" it gives "Number of primes in range: 53 and 59. How would I change it so it would look like "Number of primes in range: 2"