Table of Contents
In this article, we will discuss the concept of Java program to Display all prime numbers in an intervals
In this program, we are going to learn how to write the code to display prime numbers between two intervals using different methods in Java language.
This is done using for loop , while loop , do-while loop and method in Java language
In this program, we will print prime numbers between two intervals using a for loop in Java language
Program 1
import java.util.Scanner; public class DisplayPrimeTwoIntervals{ public static void main(String args[]){ int i,count; Scanner sc=new Scanner(System.in); //scanner class for input System.out.println("Please Enter minimum value : "); //Ask input from user int minValue=sc.nextInt(); System.out.println("Please Enter maximum value: "); int maxValue=sc.nextInt(); System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : "); //forloop for finding and printing all prime numbers in given range for(i=minValue; i<=maxValue; i++){ count=0; for(int j=1; j<=i; j++){ if(i%j==0){ count++; } } if(count==2) System.out.print(i+" "); } } }
When the above code is executed, it produces the following result
Please enter minimum value: 10 Please enter maximum value: 100 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
In this program, we will print prime numbers between two intervals using a while loop in Java language
Program 2
import java.util.Scanner; public class DisplayPrimeTwoIntervalsWhile{ public static void main(String args[]){ int i,count; Scanner sc=new Scanner(System.in); //scanner class for input System.out.println("Please Enter minimum value : "); //Ask input from user int minValue=sc.nextInt(); System.out.println("Please Enter maximum value: "); int maxValue=sc.nextInt(); System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : "); //forloop for finding and printing all prime numbers in given range i=minValue; while(i<=maxValue){ count=0; int j=1; while(j<=i){ if(i%j==0){ count++; } j++; } if(count==2) System.out.print(i+" "); i++; } } }
When the above code is executed, it produces the following result
Please enter minimum value: 30 Please enter maximum value: 80 31 37 41 43 47 53 59 61 67 71 73 79
In this program, we will print prime numbers between two intervals using a do- while loop in Java language
Program 3
import java.util.Scanner; public class DisplayPrimeTwoIntervalsDoWhile{ public static void main(String args[]){ int i,count; Scanner sc=new Scanner(System.in); //scanner class for input System.out.println("Please Enter minimum value : "); //Ask input from user int minValue=sc.nextInt(); System.out.println("Please Enter maximum value: "); int maxValue=sc.nextInt(); System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : "); //forloop for finding and printing all prime numbers in given range i=minValue; while(i<=maxValue){ count=0; int j=1; while(j<=i){ if(i%j==0){ count++; } j++; } if(count==2) System.out.print(i+" "); i++; } } }
When the above code is executed, it produces the following result
Please enter minimum value: 25 Please enter maximum value: 75 29 31 37 41 43 47 53 59 61 67 71 73
In this program, we will print prime numbers between two intervals using a method in Java language
Program 4
import java.util.Scanner; public class DisplayPrimemethod2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Please Enter minimum value : "); //Ask input from user int minValue=sc.nextInt(); System.out.println("Please Enter maximum value: "); int maxValue=sc.nextInt(); System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : "); while(minValue<maxValue){ if(printPrime(minValue)) System.out.print(minValue+" "); minValue++; } } public static boolean printPrime(int num) { boolean flag=true; for(int i=2; i<=num/2; ++i){ if(num%i==0){ flag=false; break; } } return flag; } }
When the above code is executed, it produces the following result
Please enter minimum value: 25 Please enter maximum value: 90 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
Suggested for you
if statements in Java language
Data type and variable in Java
Do-while loop in Java language
Similar post
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Code to print prime numbers from 1 to 100 or 1 to n in Java
Code to print prime numbers from 1 to 100 or 1 to n in C
Code to print prime numbers from 1 to 100 or 1 to n in C++
Code to print prime numbers from 1 to 100 or 1 to n in Python
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.