Monday, July 24, 2023

Fascinating Number

 import java.util.Scanner;


public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

scanner.close();

if (isFascinatingNumber(number))

{

System.out.println(number + " is a fascinating number.");

}

else

{

System.out.println(number + " is not a fascinating number.");

}

}

private static boolean isFascinatingNumber(int number)

{

String concatenatedNumber = String.valueOf(number) + String.valueOf(number * 2) + String.valueOf(number * 3);

int digitCount = countDigits(concatenatedNumber);

int[] frequency = new int[10];

for (int i = 0; i < digitCount; i++)

{

char digitChar = concatenatedNumber.charAt(i);

if (!Character.isDigit(digitChar) || frequency[digitChar - '0'] > 0) 

{

return false;

}

frequency[digitChar - '0']++;

}

return true;

}

private static int countDigits(String number)

{

return number.length();

}

}

No comments:

Post a Comment