Question

In: Computer Science

A prime number is an integer greater than 1 that is evenly divisible by only 1...

A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java

Solutions

Expert Solution

Program:

import java.util.Scanner;

class primeno

{

public static void main(String ags[ ] )

{

int t;

boolean prime = true;

Scanner scan = new Scanner(system.in);

System.out.println("Enter a number");

int n= scan.nextInt();

scan.close();

for(int i=2; i <=n/2; i++)

{

t = n% i;

if(t==0)

{

prime = false;

break;

}

}

if(prime)

System.out.println(n + " is a prime number");

else

System.out.println(n + "is not a prime number");

}

}

Output 1:

Enter a number

7

7 is a prime number

Output 2:

Enter a number

8

8 is not a prime number

Working:

System.out.println ("enter a number"); -> In the output screen it will ask to enter a number

int n = scan.nextInt(); -> inputting a number n as datatype int

for( int i=2; i<=n/2;i++) -> initialize variable i by 2, condition is i<=n/2 and i++ is incrementing value of i by 1.

t = n%i; -> t is a variable which stores n% i, that is the remainder when n is divided by i

if (t==0)

{

prime= false

break;

} -> These statements first check whether the value of t = 0 . if t = 0 then prime will become false and comes out of condition.

if(prime)

System.out.println(n + " is a prime number");

else

System.out.println(n + "is not a prime number");

if prime is true then outpur the number is a prime number, otherwise output the number is not a prime number.


Related Solutions

A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
5. A prime number is a number that is only evenly divisible by itself and 1....
5. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, how‐ ever, is not prime because it can be divided evenly by 1, 2, 3, and 6.   Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise....
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime number.
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
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.
All years that are evenly divisible by 400 or are evenly divisible by four and not...
All years that are evenly divisible by 400 or are evenly divisible by four and not evenly divisible 100 are leap years. For example, since 1600 is evenly divisible by 400, the year 1600 was a leap year. Similarly, since 1988 is evenly divisible by four but not 100, the year 1988 was also a leap year. Using this information, write a C++ program that accepts the year as user input, determines if the year is a leap year, and...
Show that an integer is divisible by 17 if and only if the integer formed by...
Show that an integer is divisible by 17 if and only if the integer formed by subtracting 5 times the last digit from the integer formed from the remaining digits is divisible by 17. [For example 442 is divisible by 17 since 44 – 5*2 = 34 is divisible by 17] course: discrete structure
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT