Java Program to Check Sphenic Number

In the previous article, we have discussed Java Program to Check Emirp Number

In this article we are going to understand what Sphenic number, Sphenic number list, Sphenic number in c is and how we can check whether a number is Sphenic number or not in Java with examples and mischievous numbers program in java.

Also Read: Java Program to Check Autobiographical Number

Program to Check Sphenic Number

Sphenic numbers are numbers which have exactly 3 factors, all prime. A Sphenic number has exactly 8 divisors.

 Example :

30: 2*3*5, Sphenic number
66: 2*3*11, Sphenic number
15: 3*5, Not a Sphenic number

In the above examples the numbers 30 and 66 are Sphenic numbers as they both have 3 prime factors exactly. However 15 is not a Sphenic number.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach :

  1. We ask the user to enter/declare a number and store it.
  2. We check whether the number has 8 divisors or not and store them.
  3. Then we check if the first three divisors are prime or not.
  4. If they are prime then the number is a Sphenic Number.

Method-1: Java Program to Check Sphenic Number By Using Static Value

import java.util.*;

public class SphenicNumber
{
    // Creating a boolean array acessible to all functions
    static boolean arr[] = new boolean[10000];

    public static void main(String args[])
    {
        //A number declared;
        int num = 30;
        Prime();
        if(sphenicCheck(num))
        {
            System.out.println(num+" is a Sphenic number");
        }
        else
        {
            System.out.println(num+" is Not a Sphenic number");
        }
    }

    static void Prime()
    {
        // Filling the boolean array with all values set to true
        Arrays.fill(arr,true);
        for(int i = 2; i*i<10000; i++)
        {
            if(arr[i])
            {   
                //Marking all multiples of i as non prime
                for(int j = i*2; j<10000; j= j+i)
                    arr[j] = false;
            }
        }
    }

    //method to check sphenic number
    static boolean sphenicCheck(int num)
    {
        int arr1[] = new int[8];
        int count=0, j = 0,i;
        for(i =1; i<=num;i++)
        {
            // Counts the number of divisors
            if(num%i==0 && count< 8)
            {
                count++;
                arr1[j++]=i;
            }
        }
        // Returns if the number is Sphenic or not
        if(count==8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
            return true;
        else
            return false;
    }
}
Output:

30 is a Sphenic number

Method-2: Java Program to Check Sphenic Number By Using User Input Value

import java.util.*;

public class SphenicNumber
{
    // Creating a boolean array acessible to all functions
    static boolean arr[] = new boolean[10000];

    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        Prime();
        if(sphenicCheck(num))
        {
            System.out.println(num+" is a Sphenic number");
        }
        else
        {
            System.out.println(num+" is Not a Sphenic number");
        }
    }

    static void Prime()
    {
        // Filling the boolean array with all values set to true
        Arrays.fill(arr,true);
        for(int i = 2; i*i<10000; i++)
        {
            if(arr[i])
            {   
                //Marking all multiples of i as non prime
                for(int j = i*2; j<10000; j= j+i)
                    arr[j] = false;
            }
        }
    }

    //method to check sphenic number
    static boolean sphenicCheck(int num)
    {
        int arr1[] = new int[8];
        int count=0, j = 0,i;
        for(i =1; i<=num;i++)
        {
            // Counts the number of divisors
            if(num%i==0 && count< 8)
            {
                count++;
                arr1[j++]=i;
            }
        }
        // Returns if the number is Sphenic or not
        if(count==8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))  
            return true;
        else
            return false;
    }
}
Output:
Case-1
Enter a number : 30 
30 is Not a Sphenic number

Case-2
Enter a number : 23
23 is Not a Sphenic number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Try these:

  1. Write a program in java to check whether a number is a buzz number or not?
  2. How to find a duck number in java?
  3. Mischievous numbers program in java?
  4. Sphenic number in c++?
  5. What are the decreasing numbers code in java?
  6. Sphenic number c++?
  7. Odious number program in java?
  8. Java program to check special number?
  9. Java program to check spy number?
  10. Java program to check special characters?
  11. Java program to check strong number?
  12. What is sphenic number c?

Related Java Programs: