Wednesday, 14 October 2020

WAP to print all prime numbers up to a range.

 All prime numbers up to a range.


import java.util.Scanner;
class Prime_range
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  int factors;
  System.out.println("Enter the range:");
  int range=sc.nextInt();
  for(int i = 1; i<=range; i++) {
   factors = 0;
   for(int j = 1; j<=i; j++) {
    if(i%j ==0)
     factors++;
   }
   if(factors ==2) {
    System.out.print(i+"  ");
   }  } }}




Output:

Enter the range:

30

2  3  5  7  11  13  17  19  23  29


No comments:

Post a Comment