 Explore
Placement Prep Edit Edit

# Find prime numbers in a given range in C, C++, Java and Python | FACE Prep

Published on 09 Mar 2020

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 loop

C
C++
Java
Python 3

Output
Input- Enter start value :10 Enter end value :50 Output- Prime Numbers between 10 and 50: 11 13 17 19 23 29 31 37 41 43 47

### Command line

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);
N2 =atoi(argv);
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;
}


Time complexity: O(n^2)

## Program to find prime numbers in a given range using functions

C
C++
Java
Python 3

Output
Input- Enter start value :10 Enter end value :50 Output- Prime Numbers between 10 and 50: 11 13 17 19 23 29 31 37 41 43 47

Time Complexity: O(n^3/2)

Recommended Programs  