# Write a program to print prime numbers in a given range

16 May 2022

Write a program to print prime numbers in a given range

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

# Description

Get the starting and ending number from the user and then print all the prime numbers within the range.

Input

4

10

Output

5  7

# C Program

#include<stdio.h>

int Prime(int num)

{

if(num < 2){

return 0;

}

else

{

int x = num/2;

for(int i = 2; i <= x; i++)

{

if(num % i == 0)

{

return 0;

}

}

}

return 1;

}

int main()

{

int n1, n2;

printf("Enter the lower and upper values: ");

scanf("%d %d",&n1, &n2);

for(int i=n1;i <= n2;i++){

if(Prime(i))

printf("%d ",i);

}

return 0;

}

# C++ Program

#include <iostream>

using namespace std;

int Prime(int num)

{

if(num < 2){

return 0;

}

else

{

int x = num/2;

for(int i = 2; i <= x; i++)

{

if(num % i == 0)

{

return 0;

}

}

}

return 1;

}

int main()

{

int n1, n2;

cout<<"Enter the lower and upper values: ";

cin>>n1>>n2;

for(int i=n1;i <= n2;i++){

if(Prime(i))

cout<<i<<" ";

}

return 0;

}

# Java Program

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.print("Enter lower and upper bounds: ");

int n1 = sc.nextInt();

int n2 = sc.nextInt();

int count;

for(int i = n1 ; i <= n2 ; i++)

{

count = 0;

for(int j = 1 ; j <= i ; j++)

{

if(i % j == 0)

count = count+1;

}

if(count == 2)

System.out.print(i+" ");

}

}

}

# Python

lower = int(input("Enter the lower bound: "))

upper = int(input("Enter the upper bound: "))

for i in range(lower, upper+1):

flag=1

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

if i % j == 0:

flag=0

break

if flag==1 and i!=1:

print(i,end=" ")

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023