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

Write two subroutines to perform the following (Using Java): 1. Determine if a n

ID: 3714894 • Letter: W

Question

Write two subroutines to perform the following (Using Java):

1. Determine if a number is prime

-Number is passed on the stack (as a parameter)

-If the number is prime, return a 1 on the stack (return value)

-If the number is not prime, return a 0 on the stack (return value)

2. Determine the next prime number in a sequence

-Number is passed on the stack (as a parameter)

-Place the smallest prime number greater than or equal to the number on the stack (return value)

*Write a main program to test your subroutines.

*Function 2 must call 1

Explanation / Answer

import java.util.Scanner;

/**
*
*/

/**
* @author yourname
*
*/
public class Prime {

   /**
   * @param args
   */
   public static void main(String[] args) {
      
       System.out.println("Check If the number Is prime ");
      
       System.out.println("Please enter a number ");
       Scanner sc=new Scanner(System.in);
       int number=sc.nextInt();
       int isPrime=isPrime(number);
       if(isPrime==1)
       {
           System.out.println("Number is Prime : ");
       }
       else
       {
           System.out.println("Number is not prime : ");
       }
      
       //Test smallest prime greater than n
      
       System.out.println("The smallest prime number greater than or equal to the number is "+findSmallestPrime(number));

   }
  
   /***
   *
   * @param n
   * @return 1, if number is prime otherwise return 0
   *
   *    */
   private static int isPrime(int n) throws IllegalArgumentException
    {
       //Check the argument value
       if (n < 2) throw new IllegalArgumentException("No Prime can be less than 2 ....");
       else { //Divide with each integer till n-1 and check the Remainder , if remainder is 0 , then number is divisible by some integer,
           //Hence not a prime , Otherwise return true
           for (int i = 2; i < n; i++) {
               if (n % i == 0)
               {
                   return 0;
               }
           }
       }
       return 1;
   }

   public static int findSmallestPrime(int number)
   {
       int prime=0;
       //First check if the passed number is Prime
       //if so return its value
       if(isPrime(number)==1)
       {
           return number;
       }
       else //Loop starting from number , untill we get a Prime
       {
           prime=number+1;
           while(true) //made loop condition always true , will come out of loop using break
           {
               if(isPrime(prime)==1)
               {
                   break;
               }
               else
               {
                   prime++;
               }
           }
       }
       return prime;
   }

}

Output:

Check If the number Is prime
Please enter a number
55
Number is not prime :
The smallest prime number greater than or equal to the number is 59