Computer Science, asked by tigre, 1 year ago

write a program in Java to calculate and display 50 simultaneous 'prime number', staring from a given number entered by user. use util package.

Answers

Answered by QGP
19

Fifty Simultaneous Primes - Java

The problem statement is quite clear. First take the user input of a starting point (which we will do with java.util.Scanner) and then print the next 50 primes.

We create a function boolean isPrime(long n) to help us. The function returns true if the number n is prime, else false.

A prime number is a number with only two factors: 1 and itself. The simplest test to check if a number is prime, is to check if it is divisible by any integer from 2 to \sf \sqrt{n}. If a division is found, then the number is not prime. If all divisions fail, then the number is prime.

So, we maintain a counter, and check all numbers until the counter hits 50. That's it. Fifty primes.

 \rule{300}{1}

FiftyPrimes.java

import java.util.Scanner;

public class FiftyPrimes

{

   // Return true if a number n is prime

   public static boolean isPrime(long n)

   {

       // Check division from 2 to sqrt(n)

       for (long i = 2; i <= Math.sqrt(n); i++)

       {

           if (n % i == 0)

           {

               return false;

           }

       }

       

       // If no division is found, number is prime

       return true;

   }

   public static void main(String[] args)

   {

       // Create Scanner object

       Scanner sc = new Scanner(System.in);

       // Take user input

       System.out.print("Enter the starting point: ");

       long start = sc.nextInt();

       // Close Scanner object

       sc.close();

       // If start point is less than 2, make it 2

       if (start < 2)

       {

           start = 2;

       }

       int numberOfPrimes = 0;     // Number of primes printed

       long number = start;        // Number to be tested for prime

       // Iterate until 50 primes are printed

       while (numberOfPrimes < 50)

       {

           if (isPrime(number))

           {

               System.out.println(number);

               numberOfPrimes++;

           }

           number++;

       }

   }

}

Attachments:
Answered by harnathyadav2907
4

\huge\mathbb\pink{\underline {\underline{Answer}}}

import java.util.Scanner;

public class FiftyPrimes

{

// Return true if a number n is prime

public static boolean isPrime(long n)

{

// Check division from 2 to sqrt(n)

for (long i = 2; i <= Math.sqrt(n); i++)

{

if (n % i == 0)

{

return false;

}

}

// If no division is found, number is prime

return true;

}

public static void main(String[] args)

{

// Create Scanner object

Scanner sc = new Scanner(System.in);

// Take user input

System.out.print("Enter the starting point: ");

long start = sc.nextInt();

// Close Scanner object

sc.close();

// If start point is less than 2, make it 2

if (start < 2)

{

start = 2;

}

int numberOfPrimes = 0; // Number of primes printed

long number = start; // Number to be tested for prime

// Iterate until 50 primes are printed

while (numberOfPrimes < 50)

{

if (isPrime(number))

{

System.out.println(number);

numberOfPrimes++;

}

number++;

}

}

}

\huge\mathtt\purple{THANK\:YOU}

Similar questions