java program to display program of prime number between two number in java:

Learn how to display program of the prime number between two number in java:

 

 

In this article, you’ll learn how to print all prime number between two given number in java language without giving input from the keyboard or giving input from the keyboard.

Q: what is the prime number

Ans: A natural number which only divided by itself and 1 is called a prime number and you also say that a prime number is a natural number which is greater than 1 and cannot be formed by multiplying two natural number.

  • 1 is not a prime number.
  • 2 is the smallest prime number and only even prime number.
  • Example of prime number 2,3,5,7,11,13 etc.

To print all prime number between two given number we generally use the following steps:

  • First, we take two number then add 1 in the first number and subtract 1 in the second number to check prime number between two intervals number.
  • We use two for loop first, for loop for checking all number between the intervals and second for loop to check prime number.
  • If second for loop is true, then it means the number is not a prime number and increments the number check again
  • If second for loop is false, then it checks again
  • if(i==x) then it a prime number and check again for the first for loop.

Example 1: write a program in java language to print all prime number between two given number, without input from the keyboard

public class primeNum {

public static void main(String args[]) {

int x,i,f1=2,f2=40;

System.out.print(“Prime number are : “);

for(x=f1+1;x<=f2-1;x++)

{

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

if(x%i==0)

break;

if(i==x)

System.out.print(” “+x);

}}}

 

Prime number are :  3 5 7 11 13 17 19 23 29 31 37

 

Example 2: write a program in java language to print all prime number including two given number, without input from keyboard Solution:

Solution:

public class primeNum {

public static void main(String args[]) {

int x,i,f1=2,f2=40;

System.out.print(“Prime number are : “);

for(x=f1;x<=f2;x++)

{

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

if(x%i==0)

break;

if(i==x)

System.out.print(” “+x);

}}}

 

Prime number are :  2 3 5 7 11 13 17 19 23 29 31 37

Note: in this program, the prime number printed including 2 and check up to 40.

 

Example 3: write a program in java language to print all prime number between two given number, with input from the keyboard

Solution:

import java.util.Scanner;

public class primeNum {

public static void main(String args[]) {

int x,i;

System.out.print(“Enter two number: “);

Scanner sc=new Scanner(System.in);

int f1=sc.nextInt();

int f2=sc.nextInt();

sc.close();

for(x=f1+1;x<=f2-1;x++)

{

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

if(x%i==0)

break;

if(i==x)

System.out.print(” “+x);

}}}

 

The Output of this program:

Enter two number: 10

100

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

Example 4: write a program in C++ language to print all prime number including two given number, with input from keyboard Solution:

Solution:

import java.util.Scanner;

public class primeNum {

public static void main(String args[]) {

int x,i;

System.out.print(“Enter two number: “);

Scanner sc=new Scanner(System.in);

int f1=sc.nextInt();

int f2=sc.nextInt();

sc.close();

for(x=f1;x<=f2;x++)

{

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

if(x%i==0)

break;

if(i==x)

System.out.print(” “+x);

}}}

 

The output of this program:

Enter two number: 11

100

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

You may also learn the following programming:

 

Leave a Reply

Your email address will not be published. Required fields are marked *