“Cetak bilangan prima di java” Kode Jawaban

Cetak bilangan prima di java

import java.util.Scanner;
 
public class PrimeNumberChecker {
    public static void main(String[] args) {
        System.out.println("Enter a number to check Prime or Not");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        int i = 2, count = 0;
        while (i <= number / 2) {
            if (number % i == 0) {
                count++;
                break;
            }
            i++;
        }
        if (count == 0) {
            System.out.println(number + " is prime number");
        } else {
            System.out.println(number + " is not a prime number");
        }
    }
}
Amused Anaconda

Cetak bilangan prima di java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class PrimeNumberChecker3 {
 
    public static List<Integer> generatePrimeInRange(int startRange, int endRange) {
        if (startRange >= endRange) {
            System.out.println("endRange must be greater then startRange");
            System.exit(0);
        }
        List<Integer> primeList = new ArrayList<>();
        while (startRange <= endRange) {
            int i = 2, count = 0;
            while (i <= startRange / 2) {
                if (startRange % i == 0) {
                    count++;
                    break;
                }
                i++;
 
            }
            if (count == 0) {
                primeList.add(startRange);
            }
            startRange++;
        }
        return primeList;
    }
 
    public static void main(String[] args) {
        System.out.println("Enter a number range to generate prime numbers in between");
        Scanner scanner = new Scanner(System.in);
        int number1 = scanner.nextInt();
        int number2 = scanner.nextInt();
        List<Integer> primeNumbers = generatePrimeInRange(number1, number2);
        System.out.println("Prime Numbers are");
        System.out.println(primeNumbers);
 
    }
}
Amused Anaconda

Jawaban yang mirip dengan “Cetak bilangan prima di java”

Pertanyaan yang mirip dengan “Cetak bilangan prima di java”

Lebih banyak jawaban terkait untuk “Cetak bilangan prima di java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya