Skip to content

Commit

Permalink
Printing prime numbers in the given range
Browse files Browse the repository at this point in the history
'for' loop is used and logic
  • Loading branch information
suhanigupta23 authored Jan 24, 2024
1 parent ad1de18 commit 6a23205
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Printingprimenumbersintherange.java
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.
}
}

}
}

0 comments on commit 6a23205

Please sign in to comment.