Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Write CLEARLY Create a program that uses functions and arrays that perfor

ID: 3590609 • Letter: P

Question


Please Write CLEARLY

Create a program that uses functions and arrays that performs the following: Creates a report to display to the user containing the following information: o Accept user input for a starting number o determine whether or not the entered number is prime or not o find the next 20 prime numbers after your selected numbers o display your results This can be created either using the console application as we've done in class or as a windows application if you're more comfortable withh that.

Explanation / Answer

import java.util.Arrays;
import java.util.Scanner;

public class CheckPrime {

   // isPrime method to check given number is prime or not.
   public static boolean isPrime(int n){
      
       // if n is less than or equal to 1 , it is not prime.
       if(n<=1) return false;
       // prime numbers start from 2.
       // using for checking parameter 'n' is divide or not.
       // if it not divide upto n-1, then it is prime return true or else not-prime return false.
       for(int i=2;i<n;i++){
           if(n%i==0) return false;
       }
       return true;
   }
  
   // printPrimes method, to calculate next 20 prime numbers based on given parameter 'num'.
  
   public static int[] printPrimes(int num){
       // count for checking prime or not.
       // inc for increment outerloop
       // k for using array index.
       int count=0,inc=1,k=0;
       // primeArray for store next 20 primes numbers, based on given parameter.
       int[] primeArray=new int[20];
      
       // outer loop iterates upto 20 times.
       for (int i = num; inc<=20; i++) {
               count=0;
               // inner for loop for checking prime or not.
               for (int j = 2; j <= num / 2; j++) {
                   // if num divisible by j , count value icrement to 1 and num is not prime.
                   // so, it is not store in array.
                if (num% j == 0) {
                 count++;
                 break;
                }
               }

               // if count is not increment upto inner for loop ends
               // So, num is prime. So, num is store in primeArray.
               if (count == 0) {
                //System.out.println(i);
                primeArray[k]=i;
                // index k increment
                k++;
                // SO, num is prime that's why we are incrementing inc also for one value.
                inc++;
               }
               //num increment one value for next number.
                num++;
      
       }
      
      
      
       return primeArray;
   }
  
  
   public static void main(String[] args) {
  
       Scanner sc=new Scanner(System.in);
      
       System.out.println("enter starting number: ");
      
       int n=sc.nextInt();
      
       boolean isPrime=isPrime(n);
       // if isPrime is true printing prime or else printing not-prime.
       if(isPrime) System.out.println("Enter number "+n +" is Prime:");
       else System.out.println("Enter number "+n +" is Not-Prime:");
      
       int[] arrayPrimes=printPrimes(n);
       // printing arrayPrime array.
       System.out.println(Arrays.toString(arrayPrimes));
      
   }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote