A Sphenic number is a product of three distinct prime numbers. For example, 66 is a sphenic number as it is a product of 2, 3, 11 and all these numbers are prime.
![Sphenic Number in Java](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-Number.png)
Numbers such as 30, 42, 66, 70, 78 etc are all Sphenic Numbers.
Java Program to Check Sphenic Number
import java.util.*; public class JavaExample { //create an array to mark all prime numbers //up to that range. static boolean arr[] = new boolean[10000]; static void markPrime() { //At the end of the execution of this method, this array //will contain true for prime numbers and false for non-prime numbers. //for example: arr[7] will contain true as 7 is prime and arr[10] will be //false as 10 is not a prime number Arrays.fill(arr, true); //iterating array and marking non-prime false for(int i = 2; i*i < 10000; i++) { if(arr[i]) { for(int j = i*2; j < 10000; j = j+i) arr[j] = false; } } } //Checks if the number num is Sphenic or not static int checkSphenic(int num) { int[] divisors = new int[8]; int count = 0; int j = 0; for(int i = 1; i <= num; i++) { if(num%i == 0 && count < 8) { count++; divisors[j++] = i; } } if(count == 8 && (arr[divisors[0]] && arr[divisors[1]] && arr[divisors[2]])) return 1; return 0; } public static void main(String args[]) { markPrime(); //find all prime numbers Scanner scan = new Scanner(System.in); System.out.print("Enter a number to check: "); int num = scan.nextInt(); int isSphenic = checkSphenic(num); if(isSphenic == 1) System.out.print("Entered number is Sphenic."); else System.out.print("Entered number is not Sphenic."); } }
Output 1:
![Java Check Sphenic Number Output1](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-op1.png)
Output 2:
![Java Check Sphenic Number Output2](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-op2.png)
Output 3:
![Java Check Sphenic Number Output3](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-op3.png)
Java Program to print all Sphenic Numbers in a given range
In this example, we are taking input from the user. The program prints all the Sphenic numbers between the entered range by user.
import java.util.*; public class JavaExample { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int num,lowerRange,upperRange,temp,count; System.out.print("Enter the lower range: "); lowerRange = scan.nextInt(); System.out.print("Enter the upper range: "); upperRange = scan.nextInt(); for(num = lowerRange; num <= upperRange; num++) { int c, f=1; temp = num; count=0; for(int i=2;i<=temp;i++) { c=0; while((temp%i)==0) { temp=temp/i; c++; } if(c==1) { f=f*i; count++; } } if(f==num && count==3) System.out.print(num+"\t"); } } }
Output 1: First 100 Sphenic Numbers
![First 100 Sphenic Numbers](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-op4.png)
Output 2: First 200 Sphenic Numbers
![First 200 Sphenic Numbers](https://beginnersbook.com/wp-content/uploads/2022/11/Sphenic-op5-1024x180.png)