-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path65thProgram_Example_21.cpp
62 lines (59 loc) · 1.29 KB
/
65thProgram_Example_21.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************
* Prime Numbers Check Using Boolean Flag
* ************************************************/
#include <iostream>
using namespace std;
int main()
{
int range;
bool t;
cout << "Enter a range:"
<< "\n";
cin >> range;
cout << "Prime Numbers Upto a given range:" << range <<"\n";
if(range>1){
for (int i = 2; i <= range; i++)
{
t = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
t = false;
break;
}
}
if (t == true)
{
cout << i << "\n";
}
}
}
else{
cout << "Invalid range";
}
return 0;
}
/*********************************
* Workings
* --------------
* int i = 2 to range say : 4
*
* i= 2
* hence j will go to 2 - 1(Not in range)=> t is true
* i=3
* hence j will go to 2 - 2(only 1 loop)
* j =2:
* 3%2 !=0 (false) => Hence t is true
* i=4
* hence j will go to 2 - 3(only 2 loop)
* j =2:
* 4%2 ==0 (false) => Hence t is false
* break;
*
* Hence we will print out : 2, and 3 upto range 4
*
*
*
*
* ****************************************/