Write a Program That Generates All Prime Numbers Between 2 and 1000

Given two numbers  a and b as interval range, the task is to find the prime numbers in between this interval.

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out ourself-paced courses designed for students of grades I-XII.

Start with topics likePython, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, becauseGeeksforGeeks School is now here!


Examples:

          Input          : a = 1, b = 10          Output          : 2, 3, 5, 7          Input          : a = 10, b = 20          Output          : 11, 13, 17, 19

In the below program, the range of numbers is taken as input and stored in the variables 'a' and 'b'. Then using for-loop, the numbers between the interval of a and b are traversed. For each number in the for loop, it is checked if this number is prime or not. If found prime, print the number. Then the next number in the loop is checked, till all numbers are checked.

Program:

C++

#include <bits/stdc++.h>

using namespace std;

int main()

{

int a, b, i, j, flag;

cout << "Enter lower bound of the interval: " ;

cin >> a;

cout << "\nEnter upper bound of the interval: " ;

cin >> b;

cout << "\nPrime numbers between "

<< 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;

}

C

#include <stdio.h>

int main()

{

int a, b, i, j, flag;

printf ( "Enter lower bound of the interval: " );

scanf ( "%d" , &a);

printf ( "\nEnter upper bound of the interval: " );

scanf ( "%d" , &b);

printf ( "\nPrime numbers between %d and %d are: " , a, b);

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)

printf ( "%d " , i);

}

return 0;

}

Java

import java.util.Scanner;

public class GFG {

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int a, b, i, j, flag;

System.out.printf( "Enter lower bound of the interval: " );

a = sc.nextInt();

System.out.printf( "\nEnter upper bound of the interval: " );

b = sc.nextInt();

System.out.printf( "\nPrime numbers between %d and %d are: " , a, b);

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 )

System.out.println(i);

}

}

}

Python3

if __name__ = = '__main__' :

a, b, i, j, flag = 0 , 0 , 0 , 0 , 0

print ( "Enter lower bound of the interval:" ,

end = "")

a = int ( input ())

print (a)

print ( "Enter upper bound of the interval:" ,

end = "")

b = int ( input ())

print (b)

print ( "Prime numbers between" , a, "and" ,

b, "are:" , end = "")

for i in range (a, b + 1 ):

if (i = = 1 ):

continue

flag = 1

for j in range ( 2 , i / / 2 + 1 ):

if (i % j = = 0 ):

flag = 0

break

if (flag = = 1 ):

print (i, end = " " )

C#

using System;

class GFG{

public static void Main( string [] args)

{

int a, b, i, j, flag;

Console.WriteLine( "Enter lower bound of " +

"the interval: " );

a = int .Parse(Console.ReadLine());

Console.WriteLine( "\nEnter upper bound " +

"of the interval: " );

b = int .Parse(Console.ReadLine());

Console.WriteLine( "\nPrime numbers between " +

"{0} and {1} are: " , a, b);

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)

Console.WriteLine(i);

}

}

}

Output:

Enter lower bound of the interval: 1 Enter upper bound of the interval: 10 Prime numbers between 1 and 10 are: 2 3 5 7        

Optimized Solution :
The idea is to use the fact that even numbers (except 2) are not primes.

C++

#include <bits/stdc++.h>

using namespace std;

int main()

{

int a, b, i, j;

cout << "Enter lower bound of the interval: " ;

cin >> a;

cout << "\nEnter upper bound of the interval: " ;

cin >> b;

cout << "\nPrime numbers between " << a << " and " << b

<< " are: " ;

if (a <= 2) {

a = 2;

if (b >= 2) {

cout << a << " " ;

a++;

}

}

if (a % 2 == 0)

a++;

for (i = a; i <= b; i = i + 2) {

bool flag = 1;

for (j = 2; j * j <= i; ++j) {

if (i % j == 0) {

flag = 0;

break ;

}

}

if (flag == 1)

cout << i << " " ;

}

return 0;

}

Java

import java.util.Scanner;

class GFG {

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int a, b, i, j,flag;

System.out.printf( "Enter lower bound of the interval: " );

a = sc.nextInt();

System.out.printf( "\nEnter upper bound of the interval: " );

b = sc.nextInt();

System.out.printf( "\nPrime numbers between %d and %d are: " , a, b);

if (a == 1 ) {

System.out.println(a);

a++;

if (b >= 2 ) {

System.out.println(a);

a++;

}

}

if (a == 2 )

System.out.println(a);

if (a % 2 == 0 )

a++;

for (i = a; i <= b; i = i + 2 ) {

flag = 1 ;

for (j = 2 ; j * j <= i; ++j) {

if (i % j == 0 ) {

flag = 0 ;

break ;

}

}

if (flag == 1 )

System.out.println(i);

}

}

}

Python3

if __name__ = = '__main__' :

a, b, i, j = 0 , 0 , 0 , 0

print ( "Enter lower bound of the interval:" ,end = "")

a = int ( input ())

print (a)

print ( "Enter upper bound of the interval:" ,end = "")

b = int ( input ())

print (b)

print ( "Prime numbers between" , a, "and" ,b, "are:" , end = "")

if (a = = 1 ):

print (a,end = " " )

a + = 1

if (b > = 2 ):

print (a,end = " " )

a + = 1

if (a = = 2 ):

print (a,end = " " )

if (a % 2 = = 0 ):

a + = 1

for i in range (a,b + 1 , 2 ):

flag = 1

j = 2

while (j * j < = i):

if (i % j = = 0 ):

flag = 0

break

j + = 1

if (flag = = 1 ):

print (i,end = " " )

C#

using System;

class GFG

{

static public void Main()

{

int a, b, i, j, flag;

Console.Write(

"Enter lower bound of the interval: " );

a = Convert.ToInt32(

Console.ReadLine());

Console.Write(

"\nEnter upper bound of the interval: " );

b = Convert.ToInt32(

Console.ReadLine());

Console.Write( "\nPrime numbers between " + a

+ " and " + b + " are: " );

if (a == 1)

{

Console.Write(a + " " );

a++;

if (b >= 2)

{

Console.Write(a + " " );

a++;

}

}

if (a == 2)

{

Console.Write(a + " " );

}

if (a % 2 == 0)

{

a++;

}

for (i = a; i <= b; i = i + 2)

{

flag = 1;

for (j = 2; j * j <= i; ++j)

{

if (i % j == 0)

{

flag = 0;

break ;

}

}

if (flag == 1)

{

Console.Write(i + " " );

}

}

}

}

Output:

Enter lower bound of the interval: 1 Enter upper bound of the interval: 10 Prime numbers between 1 and 10 are: 1 2 3 5 7        

Write a Program That Generates All Prime Numbers Between 2 and 1000

Source: https://www.geeksforgeeks.org/program-to-find-prime-numbers-between-given-interval/

0 Response to "Write a Program That Generates All Prime Numbers Between 2 and 1000"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel