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
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