Please develop a Java program that is design implement and t

Please develop a Java program (that is design, implement and test), using division, that prints out the last 100 (the largest) prime numbers that are smaller than N, where N=1000.

Solution

Well, here is the code i will comment each line class PrimeNumbers //Create a class { public static void main(String args[]) //Create a public function, //no return paramater, take a string { int n, status = 1, num = 3; //create variables and initial vlaues Scanner in = new Scanner(System.in); //Here block is for asking the //quantity of numbers System.out.println(\"Enter the number of prime numbers you want\"); n = in.nextInt(); if (n >= 1) //thi block is just a fancy text to show the header of the result { System.out.println(\"First \"+n+\" prime numbers are :-\"); System.out.println(2); } for ( int count = 2 ; count <=n ; ) //from 2 to numbers of primer numbers we want do { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) //from j=2 to j<=square root of num // while increment j this for loop find the residue, if residue is zero there is not prime end of the loop beacuse of the break { if ( num%j == 0 ) //if the rest of division is zero { status = 0; //status change to zero break; //end the second for } } if ( status != 0 ) //if the status is difeerent from zero then the number is prime because we cant find the residue equals to zero before { System.out.println(num); //print the number count++; //increment counter } status = 1; //change status from zero to one num++; //increment num } } }
Please develop a Java program (that is design, implement and test), using division, that prints out the last 100 (the largest) prime numbers that are smaller th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site