Question

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 weve done in class or as a windows application if youre more comfortable withh that.


Please Write CLEARLY

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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));
      
   }
  
  
}

Add a comment
Know the answer?
Add Answer to:
Please Write CLEARLY Create a program that uses functions and arrays that performs the following: Creates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem #1 Create a program that performs the following functions: Uses character arrays to read a...

    Problem #1 Create a program that performs the following functions: Uses character arrays to read a user's name from standard input Tells the user how many characters are in her name Displays the user's name in uppercase Create a program that uses the strstr() function to search the string "When the going gets tough, the tough stay put! for the following occurrences (display each occurrence found to standard output): "Going" "tou" "ay put!" Build a program that uses an array...

  • Create a C# console application that performs the following: 1) Build a method which reads 3...

    Create a C# console application that performs the following: 1) Build a method which reads 3 integers from the console and returns them as an int array (type a number, then hit enter, 3 times). This needs to be its own method. 2) Call this method twice to store 2 local int array variables. 3) Once both arrays are built, write another method which accepts the two arrays and compares the contents to see if they match. If the method...

  • Create a C# console application that performs the following: 1) Build a method which reads 3...

    Create a C# console application that performs the following: 1) Build a method which reads 3 integers from the console and returns them as an int array (type a number, then hit enter, 3 times). This needs to be its own method. 2) Call this method twice to store 2 local int array variables. 3) Once both arrays are built, write another method which accepts the two arrays and compares the contents to see if they match. If the method...

  • Using Java Create an application that creates and displays a list of names using an array....

    Using Java Create an application that creates and displays a list of names using an array. Then modify the application to create and use a list of numbers. Console for the names application Please enter a name or type “exit” to quit: Diane Please enter a name or type “exit” to quit: Joe Please enter a name or type “exit” to quit: Greta Please enter a name or type “exit” to quit: Henry Please enter a name or type “exit”...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Question 7 (a) Write a C program that performs the following tasks: 1. Create a text...

    Question 7 (a) Write a C program that performs the following tasks: 1. Create a text file to store a list of numbers. 2. Read a number entered by a user and store it into the text file until a negative number is entered. (b) Write a C program that performs the following tasks: 1. Open the text file that contains a list of numbers. 2. Print the list of numbers stored in the text file on the screen.

  • C++ please no arrays! Create a program that will allow the user to enter up to...

    C++ please no arrays! Create a program that will allow the user to enter up to 50 whole values and determine the number of values entered, how many of the values were odd and how many of the values were even. Your code should contain 2 functions, a main function and a function to enter the numbers enter and count the values. Your main function should be a driver program that will call another function to count the total number...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT