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.

b) Modify the application to prompt the user for two numbers and then display the prime numbers between those numbers.

( Java programming )

Solutions

Expert Solution

a)Java code:

import java.util.Scanner;
public class Main{
   public static void main(String[] args){
   Scanner input=new Scanner(System.in);
   //asking for a number
   System.out.print("Enter a number: ");
   //accepting it
   int num=input.nextInt();
   //initializing flag as 0
   int flag=0;
   //looping from 2 to number
   for(int i=2;i<num;i++)
   //checking if the number is divisible by any other number
   if(num%i==0){
   //ssetting flag as 1
   flag=1;
   //printing Not prime
   System.out.println("Not prime");
   //exiting out of loop
   break;
   }
   //checking if flag is 0
   if(flag==0)
   //printing Prime
   System.out.println("Prime");
   }
}


Screenshot:


Input and Output:

b)Java code:

import java.util.Scanner;
public class Main{
   public static void main(String[] args){
   Scanner input=new Scanner(System.in);
   //asking for a number
   System.out.print("Enter two numbers: ");
   //accepting first number
   int num1=input.nextInt();
   //accepting second number
   int num2=input.nextInt();
   //initializing flag as 0
   int flag;
   //looping from num1 to num2
   for(int num=num1;num<=num2;num++){
   //setting flag as 0
   flag=0;
   //looping from 2 to number
   for(int i=2;i<num;i++)
   //checking if the number is divisible by any other number
   if(num%i==0){
   //setting flag as 1
   flag=1;
   //breaking out of loop
   break;
   }
   //checking if flag is 0
   if(flag==0)
   //printing Prime number
   System.out.println(num);
   }
   }
}


Screenshot:


Input and Output:


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. Java
A prime number is a number that is only evenly divisible by itself and 1. For...
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. Design a Boolean function called isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the...
A prime number is a number that is only evenly divisible by itself and 1. For...
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 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. Use 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...
(PYTHON) A Prime number is an integer greater than 1 that cannot be formed by multiplying...
(PYTHON) A Prime number is an integer greater than 1 that cannot be formed by multiplying two smaller integer other than 1 and itself. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1. In this question you will write a program that takes a sequence of integers from the user and display all the prime numbers contained in that sequence. We will separate this question in 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT