“Druckmaschinenzahlen in Java drucken” Code-Antworten

Druckmaschinenzahlen in Java drucken

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

Druckmaschinenzahlen in Java drucken

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

Ähnliche Antworten wie “Druckmaschinenzahlen in Java drucken”

Fragen ähnlich wie “Druckmaschinenzahlen in Java drucken”

Weitere verwandte Antworten zu “Druckmaschinenzahlen in Java drucken” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen