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 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.
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
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume that we are given a list of all the prime numbers from 1 to 10,000, in sorted order in a text file called primes.txt: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT