Write a Program to find Prime Numbers Between given Interval

Write a Program to find Prime Numbers Between given Interval

Java Program
Prime numbers are natural numbers that are divisible by only 1 and the number itself. In other words, prime numbers are positive integers greater than 1 with exactly two factors, 1 and the number itself. Some of the prime numbers include 2, 3, 5, 7, 11, 13, etc.
import java.util.Scanner;
public class Demo 
{
    public static void main(String[] args) 
    {
        int x, y,n1,n2, flag;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter first value: ");
        n1 = s.nextInt(); 
        System.out.println("Enter second value:");
        n2 = s.nextInt(); 
        System.out.println("Prime numbers between "+n1+" and " +n2+" are: ");
  
        for (x = n1; x <= n2; x++) 
        {
            if (x == 1 || x == 0)
            {
                continue;
            }
            flag = 1;
            
            for (y = 2; y <= x / 2; ++y) 
            {
                if (x % y == 0) 
                {
                    flag = 0;
                    break;
                }
            }
  
            if (flag == 1)
            {
                System.out.println(x);
            }
        }
    }
}


Output:

Enter first value: 1
Enter second value: 20
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19

Python Program

def myfun():
    n1=int(input("Enter first value: "))
    n2=int(input("Enter second value:"))
    print("Prime numbers between ",n1," and ",n2," are: ");
    
    for x in range(n1,n2+1,1):
        if(x == 1 or x == 0):
            continue
        flag = 1
        for y in range(2,int(x/2)+1,1):
            if (x % y == 0):
                flag = 0;
                break;
        if (flag == 1):
            print(x);
            
if __name__=="__main__":
    myfun()



Output:

Enter first value: 1
Enter second value: 20
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19

C Program
#include <stdio.h>

int main() 
{
    int x, y,n1,n2, flag;
    printf("Enter first value: ");
    scanf("%d",&n1);
    printf("Enter second value:");
    scanf("%d",&n2);
    printf("Prime numbers between %d and %d are: ",n1,n2);
    
    for (x = n1; x <= n2; x++) 
    {
        if (x == 1 || x == 0)
        {
            continue;
        }
        flag = 1;
        
        for (y = 2; y <= x / 2; ++y) 
        {
            if (x % y == 0) 
            {
                flag = 0;
                break;
            }
        }
        
        if (flag == 1)
        {
            printf("\n %d",x);
        }
    }

    return 0;
}

Output:

Enter first value: 1
Enter second value: 20
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19





More Questions


4 . Write a Program to find Prime Numbers Between given Interval