I need a program that prints all the prime numbers between any two intervals and than prints how many prime numbers there were between the two intervals.
So I have a running code but it won't print the number 2 and I know 2 is a prime number. It's doing everything else correctly. I tried some other codes that would print 2 but it would also give negatives if I put in a negative number.
import java.util.Scanner;
class Main {
public static void main(String args[]) {
int first, last, flag = 0, i, j;
Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter the lower bound : ");
first = scanner.nextInt();
System.out.print("\nEnter the upper bound : ");
last = scanner.nextInt();
System.out.println("The prime numbers in between the entered limits are :");
int x = 0;
for (i = first; i <= last; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 1) {
x++;
System.out.println(i + " ");
}
}
System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);
}
}
So if I was to put in -5 (upperbound) and 10 (lowerbound) It should print: 2 3 5 7 Total number of prime numbers between -5 and 10 are 4
But instead it prints 3 5 7 Total number of prime numbers between -5 and 10 are 3
inner loop ignore number 2. j < i => 2 < 2 is false
Just add the following lines before your for
loop and it will give your the expected output:
int x = 0;
if (first<3) {
System.out.println(2);
x++;
}
Your updated program will be:
import java.util.Scanner;
class Main {
public static void main(String args[]) {
int first, last, flag = 0, i, j;
Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter the lower bound : ");
first = scanner.nextInt();
System.out.print("\nEnter the upper bound : ");
last = scanner.nextInt();
System.out.println("The prime numbers in between the entered limits are :");
int x = 0;
if (first<3) {
System.out.println(2);
x++;
}
for (i = first; i <= last; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 1) {
x++;
System.out.println(i + " ");
}
}
System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);
}
}
You can check this code (optimized as it will run faster for big ranges. Loop returns before iterating all the numbers)
public static void main(String args[]) {
int first;
int last;
Scanner scanner = new Scanner(System.in);
System.out.print("\nEnter the lower bound : ");
first = scanner.nextInt();
System.out.print("\nEnter the upper bound : ");
last = scanner.nextInt();
System.out.println("The prime numbers in between the entered limits are :");
int x = 0;
for (int i = first; i <= last; i++) {
if (isPrime(i)) {
x++;
System.out.println(i + " ");
}
}
System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);
}
static boolean isPrime(int n)
{
if (n <= 1) //less than 2 are not
return false;
if (n <=3) // 2 and 3 are prime
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
You do not need a flag.
Try this:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, n;
int num;
int maxCheck;
boolean isPrime = true;
String primeNumbersFound = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number: ");
num = sc.nextInt();
System.out.println("Enter the second number: ");
maxCheck= sc.nextInt();
for (i = num; i <= maxCheck; i++) {
isPrime = CheckPrime(i);
if (isPrime) {
primeNumbersFound = primeNumbersFound + i + " ";
}
}
System.out.println("Prime numbers from " + num + " to " + maxCheck + " are:");
System.out.println(primeNumbersFound);
}
public static boolean CheckPrime(int n) {
int remainder;
for (int i = 2; i <= n / 2; i++) {
remainder = n % i;
if (remainder == 0) {
return false;
}
}
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.