BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Sphenic Number in Java – Check and Print all numbers in a range

By Chaitanya Singh | Filed Under: Java Examples

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

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

Output 2:

Java Check Sphenic Number Output2

Output 3:

Java Check Sphenic Number Output3

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

Output 2: First 200 Sphenic Numbers

First 200 Sphenic Numbers

Recommended Posts

  • Java Program to check Prime Number
  • Java Program to print Prime numbers in a given range
  • Automorphic Number in Java
  • Emirp Number in Java

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap