Class 10 Class 11 Class 12

Sphenic Number in Java

A sphenic number is a positive integer which has exactly three distinct prime factors.

For example, 30 is a sphenic number because 30 = 2 × 3 × 5.

66, 70, 78, 102, 114 are some more examples of sphenic numbers.

Write a program in Java to input a positive integer from the user and check whether it is a sphenic number or not.

class Sphenic{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        System.out.print("N = ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        int a[] = {1, 1, 1};
        int prime = 2;
        int num = n;
        int index = 0;
        for(index = 0; index < 3;){
            while(num % prime == 0){
                a[index++] = prime;
                num /= prime;
                if(index == 3)
                if(num == 1)
                    break outer;
            if(prime > n)
        System.out.println("Factors: " + a[0] + ", " + a[1] + ", " + a[2]);
        int product = a[0] * a[1] * a[2];
        if(product == n){
            if(a[0] != a[1] && a[1] != a[2] && a[2] != a[0]){
                if(a[0] > 1 && a[1] > 1 && a[2] > 1)
                    System.out.println(n + " is a sphenic number.");
                System.out.println(n + " is not a sphenic number.");
                System.out.println(n + " is not a sphenic number.");
            System.out.println(n + " is not a sphenic number.");

22 replies on “Sphenic Number in Java”

Sir,in the above sphenic number program when N=18,it is showing that 18 is a sphenic number.But as you have said 18 is not a sphenic number,why is it so?
As 18=2x3x3
but the prime factors are not distinct

Sir,a program is given which states that:
WAP in Java to accept a set of n integers(n>0) in a single dimensional array.Arrange the elements of the array such that the lowest number appears in the centre of the array,next lower number in the right cell of the centre,next lower in the left cell of the centre and so on… .The process will stop when the highest number will set in its appropriate cell.Finally,display the array elements.
1 2 3 4 5
5 3 1 2 4

Sir,in the above program when N=31 and when N=62,it is not showing any output,why is it so?Can you please explain me?

I should have exited the loop when prime gets equal to n. I have made the necessary changes to the program. Please check it and let me know if it is working fine now. Thanks for your feedback.

Sir,a program is given which states that:
WAP in Java to accept a positive number and determine the number of digits. Now form an integer that has the number of digits and most significant digit at one’s place. Display the new number.
Sample Input:
Sample Output:
Resultant number:46

Sir,can we do the program in this way?
import java .util.*;
class Sphenic2
public static void main(String args[]){
Scanner sc=new Scanner(;

int no,c=0,p=1;
for (int i=2;i<=no;i++ ) {
if (no%i==0 && isPrime(i)) {

if(p==no && c==3)
System.out.println(no + " is a sphenic number.");
System.out.println(no + " is not a sphenic number.");
public static boolean isPrime(int num){
int f = 0;
for(int i = 1; i <= num; i++)
if(num % i == 0)
return (f == 2);
Sir,can you check and tell whether it is right or not?

Check the program by executing it. If it gives correct output for a variety of inputs, then it should work.

Sir,a program is given which states that:
WAP in Java to accept a paragraph containing n no of sentences where n>=1&& n<4. The words are to be separated with a single blank space and are in upper case.A sentence may be terminated either with a '.', '?' ,'!' only. Any other character may be ignored. Perform the following operations:
1) Accept the no.of sentences. If the number of sentences exceeds the limit, an appropriate error message must be displayed.
2)Find the number of words in the whole paragraph.
3) Display the words in ascending order of their frequency. Words with same frequency may appear in any order.
Sample Input:
Enter number of sentences:1
Enter sentences:TO BE OR NOT TO BE.
Sample Output:
Total number of words:6
OR 1
TO 2
BE 2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.