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.

import java.io.*;
class Sphenic{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        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;
        outer:
        for(index = 0; index < 3;){
            while(num % prime == 0){
                a[index++] = prime;
                num /= prime;
                if(index == 3)
                    break;
                if(num == 1)
                    break outer;
            }
            prime++;
            if(prime > n)
                break;
        }
        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.");
                else
                System.out.println(n + " is not a sphenic number.");
            }
            else
                System.out.println(n + " is not a sphenic number.");
        }
        else
            System.out.println(n + " is not a sphenic number.");
    }
}

22 thoughts on “Sphenic Number in Java

  1. 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

  2. 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:
    N=2136
    Sample Output:
    Resultant number:46

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

    int no,c=0,p=1;
    System.out.print(“N=”);
    no=sc.nextInt();
    for (int i=2;i<=no;i++ ) {
    if (no%i==0 && isPrime(i)) {
    p=p*i;
    c++;
    }

    }
    if(p==no && c==3)
    System.out.println(no + " is a sphenic number.");
    else
    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)
    f++;
    return (f == 2);
    }
    }
    Sir,can you check and tell whether it is right or not?

      • 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.
        Eg:
        Sample Input:
        Enter number of sentences:1
        Enter sentences:TO BE OR NOT TO BE.
        Sample Output:
        Total number of words:6
        WORD FREQUENCY
        OR 1
        NOT 1
        TO 2
        BE 2

Leave a Reply

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