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...
Python Programming An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also...
Python Programming An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 and 71 are prime numbers, so 17 and 71 are emirps. Write a program that displays the first 10 emirps.
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) -...
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...
i need this program to also print out the number of combinations #include <stdio.h> #include <string.h>...
i need this program to also print out the number of combinations #include <stdio.h> #include <string.h> #define N 10 void generate(char *array, int n) {    if (n==0)    {        printf("%s\n",array);        return;    }    for (int i = 0; i < n; ++i)    {        // generate all of the permutations that end with the last element        generate(array, n-1);        // swap the element        char temp = array[n-1];...
Write a C++ program to check whether a number is prime or not. A prime number...
Write a C++ program to check whether a number is prime or not. A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. Eg: 2, 3, 5, 7, 11, 13, 17, ... Sample Output: Input a number to check prime or not: 13 The entered number is a prime number. Input a number to check prime or not: 28 The entered number is not a prime number. Enhance the program to list all...
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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT