Facebook
From Andrzej Nurek, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 155
  1. import java.util.Scanner;
  2.  
  3. public class Main2 {
  4.  
  5.     public static void main(String[] args) {
  6.         System.out.println("Zadanie: ile jest liczb pierwszych w zakresie od a do b podanym jako parametry");
  7.         //ile jest liczb pierwszych w zakresie od a do b podanym jako parametry
  8.         int x = 0;
  9.         int n = 0;
  10.         int n1 = 0;
  11.         int amount = 0; //liczba liczb pierwszych w zadaniu
  12.         Scanner scanner = new Scanner(System.in);
  13.         System.out.println("Podaj liczbę...");
  14.         int t = scanner.nextInt();
  15.         System.out.println("Podaj liczbę...");
  16.         int t1 = scanner.nextInt();
  17.         boolean primeNumber = false;
  18.         n = t;
  19.         n1 = t1;
  20.         if (t < t1)
  21.         {
  22.             for (n = t; n <= t1; n++) {
  23.                 primeNumber = false;
  24.                 for (x = 1; x < n; x++) {
  25.                     if (x != 1 && x != n && n % x == 0) {
  26.                         primeNumber = true;
  27.                         break;
  28.                     }
  29.                 }
  30.                 if (primeNumber == false) {
  31.                     amount++;
  32.                 }
  33.             }
  34.         }
  35.            
  36.         System.out.println(amount);
  37.  
  38.     }
  39. }