Question

In: Computer Science

Prime Number Determines if a number is prime or not I also need the algorithm and...

Prime Number

Determines if a number is prime or not

I also need the algorithm and pseudocode in java.

Solutions

Expert Solution

ANSWER: Here I am giving you the algorithm and code please like it.

ALGORITHM:

START:

Declare variables n,i,flag
Intialize variables flag=1,i=2
read number n from user
if n<=1
   Display "n is not a prime number"

Repeat the steps using for loop until i<[(n/2)+1]
if   remainder of n divide i equals to 0,
       Set flag=0

if flag==0,
Display "n is not prime number"
else
Display "n is prime number"  

STOP

CODE:

import java.util.*;


public class Main {
  
     
public static void main(String [] args)
{
   Scanner sc = new Scanner(System.in);
     
   int n,i,flag;
   flag=1;
   i=2;
   System.out.println("Enter a number : ");
   n=sc.nextInt();
  
   if(n<=1)
       System.out.println(n+" is not a prime number.");
  
   while(i<((n/2)+1))
   {
       if(n%i==0)
           flag=0;
       i++;
   }
  
   if(flag==0)
       System.out.println(n+" is not a prime number.");
   else
       System.out.println(n+" is a prime number.");
     
}
}


OUTPUT:


Related Solutions

develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
A number is a palindromic prime if it is a prime number as well as a...
A number is a palindromic prime if it is a prime number as well as a palindromic number (ie. it is the same number when the digits are reversed). For example, 10301 is a palindromic prime. Write a Python program to ask the user how many palindromic primes they would like to compute, and output the values with a maximum of 10 values per line. Your program should include the following functions: isPrime(number) - returns True or False isPalindrome(number) -...
what is the junction low (i need traffic flow not current), plees i also need a...
what is the junction low (i need traffic flow not current), plees i also need a reference . thank you
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
I need to implement an algorithm based on local search that are in the following list...
I need to implement an algorithm based on local search that are in the following list in Python or Matlab( simulated annealing, variable neighborhood search, variable neighborhood descent) PLEASE HELP I am really stuck!
I need to implement an algorithm based on local search that are in the following list...
I need to implement an algorithm based on local search that are in the following list in Python or Matlab( tabu search, simulated annealing, iterated local search, evolutionary local search, variable neighborhood search, variable neighborhood descent) PLEASE HELP :)
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT