print all prime numbers in a given range

 print all prime numbers in a given range

Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).

Input Format :

Integer N

Output Format :

Prime numbers in different lines

Constraints :

1 <= N <= 100

Sample Input 1:

9

Sample Output 1:

2
3
5
7

Sample Input 2:

20

Sample Output 2:

2
3
5
7
11
13
17
19
C++ code :
#include <iostream>
using namespace std;

int main()
{
  int number;
  cin >> number;

  for(int n = 2; n<=number; n++)
  {
    bool divided=false;
    for(int i=2; i<n; i+=1) 
    {
      if(n%i == 0)
      {
        divided = true;
        break;
      }
    }
    if(divided == false)
    {
      cout << n << endl;
    }
  }
}
RELATED POST:

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment