Find prime numbers in a given range in C, C++, Java and Python | faceprep

Program to find prime numbers in a given range is discussed here. A number is said to be prime if it is divisible by 1 and the number itself.

Program to find prime numbers in a given range using loops

// C program to find prime numbers in a given range

#include <stdio.h>
int main()
{
int a, b, i, flag;
printf(“\nEnter start value : “);
scanf(“%d”,&a);
printf(“\nEnter end value : “);
scanf(“%d”,&b);
printf(“\nPrime Numbers between %d and %d : “, a, b);
while (a < b)
{
flag = 0;
for(i = 2; i <= a/2; ++i)
{
if(a % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf(“%d “, a);
++a;
}
printf(“\n”);
return 0;
}

C++ program to find prime numbers in a given range

#include <iostream>
using namespace std;
int main()
{
int a, b, i, flag;
cout << “\nEnter start value : “;
cin >> a;
cout << “\nEnter end value : ” ;
cin >> b;
cout << “\nPrime Numbers between ” << a << ” and ” << b <<” : “;
while (a < b)
{
flag = 0;
for(i = 2; i <= a/2; ++i)
{
if(a % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << a << ” “;
++a;
}
cout << endl;
return 0;
}

// Java program to find prime numbers in a given range

import java.util.*;
public class Main
{
public static void main(String args[])
{
int a,b,flag,i;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter start value : “);
a = sc.nextInt();
System.out.print(“\nEnter end value : “);
b = sc.nextInt();
System.out.print(“\nPrime numbers between “+ a + ” and ” + b + ” are : “);
while (a < b)
{
flag = 0;
for(i = 2; i <= a/2; ++i)
{
if(a % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
System.out.print(a + ” “);
++a;
}
}
}

# Python program to find prime numbers in a given range

a = int(input(“Enter start value : “))
b = int(input(“Enter end value : “))
print(“Prime numbers between”,a,”and”,b,”are :”)
while (a < b):
flag = 0;
for i in range(2, int(a/2),1):
if(a % i == 0):
flag = 1;
break
if (flag == 0):
print(a, end = ” “)
a = a + 1

The range will be specified as command line parameters. The first command line parameter, N1 which is a positive integer, will contain the lower bound of the range. The second command line parameter N2, which is also a positive integer will contain the upper bound of the range. The program should consider all the prime numbers within the range, excluding the upper bound and lower bound. Print the output in integer format to stdout. Other than the integer number, no other extra information should be printed to stdout. Example Given inputs “7” and “24” here N1= 7 and N2=24, expected output as 83.

Solution:

#include<stdio.h>
int main(int argc, char *argv[])
{
int N1, N2, j, i, count, sum = 0;
N1 =atoi(argv[1]);
N2 =atoi(argv[2]);
for(i=N1+1; i<N2; ++i)
{
count = 0;
for(j=2; j<=(i/2); j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0)
sum = sum + i;
}
printf(“%d”,sum);
return 0;
}

Output:

prime numbers in a given range

Time complexity: O(n^2)

Program to find prime numbers in a given range using functions

// Prime numbers from 1 to n in C

#include <stdio.h>

bool is_prime_number(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;

if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

void print_prime_numbers(int n)
{
for (int i = 2; i <= n; i++)
{
if (is_prime_number(i))
printf(“%d “,i);
}
}
int main()
{
int n;
printf(“\nEnter the end value : “) ;
scanf(“%d”,&n);
printf(“\nThe Prime Numbers are : “);
print_prime_numbers(n);
printf(“\n”);
}

// Prime numbers from 1 to n in C++

#include <bits/stdc++.h>
using namespace std;

bool is_prime_number(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;

if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

void print_prime_numbers(int n)
{
for (int i = 2; i <= n; i++) {
if (is_prime_number(i))
cout << i << ” “;
}
}
int main()
{
int n;
cout << “\nEnter the end value : “;
cin >> n;
cout << “\nThe Prime Numbers are : “;
print_prime_numbers(n);
cout << endl;
}

// Prime numbers from 1 to n in java

 

import java.util.*;

public class Main
{
static boolean is_prime_number(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;

if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

static void print_prime_numbers(int n)
{
for (int i = 2; i <= n; i++)
{
if (is_prime_number(i))
System.out.print(i + ” “);
}
}

public static void main(String args[])
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter end value : “);
n = sc.nextInt();
System.out.print(“\nThe Prime numbers are “);
print_prime_numbers(n);
}
}

# Prime numbers from 1 to n in python

def is_prime_number(n):
if (n <= 1):
return False
if (n <= 3):
return True

if (n % 2 == 0 or n % 3 == 0):
return False;

i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6

return True

def print_prime_numbers(n):
for i in range (2,n+1,1):
if (is_prime_number(i)):
print(i, end = ” “);

n = int(input(“Enter end value : “))
print(“The Prime numbers are :”,end = ” “)
print_prime_numbers(n)

Time Complexity: O(n^3/2)