-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Printing prime numbers in the given range
'for' loop is used and logic
- Loading branch information
1 parent
ad1de18
commit 6a23205
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//In a range of input two numbers we have to print all the prime numbers | ||
import java.util.*; | ||
public class Program | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner scn=new Scanner(System.in); | ||
System.out.println("Enter the lower number of range: "); | ||
int n1=scn.nextInt(); | ||
System.out.println("Enter the upper number of range: "); | ||
int n2=scn.nextInt(); | ||
for (int i=n1;i<=n2;i++)//starting from the numbers range | ||
{ | ||
int count=0; | ||
for (int j=2;j*j<=i;j++) | ||
{ | ||
if(i%j==0) | ||
{ | ||
count++; | ||
break; | ||
} | ||
} | ||
if(count==0) | ||
{ | ||
System.out.println(i); | ||
//printing the prime number in the range input and moving to the next number. | ||
} | ||
} | ||
|
||
} | ||
} |