-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_User_Input.cpp
More file actions
36 lines (33 loc) · 873 Bytes
/
Copy pathPrime_User_Input.cpp
File metadata and controls
36 lines (33 loc) · 873 Bytes
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
// 8. Write a program to print all prime numbers between 1 to n
#include <iostream>
using namespace std;
int main()
{
int first_number, last_number;
cout << "Enter the first number in the range = ";
cin >> first_number;
cout << "Enter the last number in the range = ";
cin >> last_number;
for (; first_number <= last_number; first_number++)
{
bool isPrime = true;
for (int divisor = 2; divisor < first_number; divisor++)
{
if (first_number % divisor == 0)
{
isPrime = false;
break;
}
else
{
isPrime = true;
}
}
if (isPrime)
cout << first_number << " is prime" << endl;
else
{
cout << first_number << " is not prime" << endl;
}
}
}