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
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 :)
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.
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...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT