100% found this document useful (1 vote)
682 views

PIN Number Code

This Java program validates PIN numbers by checking that: - The first digit is odd and the second digit is even - The third digit is a prime number - The fourth digit is a composite number It takes user input of PIN numbers, checks each for validity, and outputs any valid PINs.

Uploaded by

Amulya Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
682 views

PIN Number Code

This Java program validates PIN numbers by checking that: - The first digit is odd and the second digit is even - The third digit is a prime number - The fourth digit is a composite number It takes user input of PIN numbers, checks each for validity, and outputs any valid PINs.

Uploaded by

Amulya Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.List;
import java.util.Scanner;

public class PinValidator {


private static boolean isPrime(int number) {
if (number < 2)
return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0)
return false;
}
return true;
}

private static boolean isComposite(int number) {


if (number < 4)
return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0)
return true;
}
return false;
}

private static boolean isValidPin(int pin) {


int firstDigit = pin / 1000;
int secondDigit = (pin / 100) % 10;
int thirdDigit = (pin / 10) % 10;
int fourthDigit = pin % 10;

return firstDigit % 2 != 0 && secondDigit % 2 == 0 && isPrime(thirdDigit)


&& isComposite(fourthDigit);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the total number of PIN numbers:");


int totalPins = scanner.nextInt();

if (totalPins <= 0) {
System.out.println(totalPins + " is an invalid number");
scanner.close();
return;
}

System.out.println("Enter the PIN numbers:");

List<Integer> validPins = new ArrayList<>();

for (int i = 0; i < totalPins; i++) {


int pin = scanner.nextInt();
if (pin < 1000 || pin > 9999) {
System.out.println(pin + " is an invalid PIN number");
} else if (isValidPin(pin)) {
validPins.add(pin);
}
}
scanner.close();

if (validPins.isEmpty()) {
System.out.println("All these " + totalPins + " numbers are not a valid
PIN number");
} else {
System.out.println("Valid PIN numbers are:");
for (int pin : validPins) {
System.out.println(pin);
}
}
}
}

You might also like