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{
System.out.print("N = ");
int n = Math.abs(Integer.parseInt(br.readLine()));
int p = 2;
int count = 0;
int num = n;
outer:
while(true){
int c = 0;
while(num % p == 0){
num /= p;
count++;
c++;
if(c == 2 || count > 3)
break outer;
}
p++;
if(p > num)
break;
}
if(count == 3)
System.out.println(n + " is a sphenic number.");
else
System.out.println(n + " is not a sphenic number.");
}
}

Algorithm

STEP 1: READ N
STEP 2: P = 2
STEP 3: COUNT = 0
STEP 4: NUM = N
STEP 5: REPEAT 6 THROUGH 13 WHILE TRUE:
STEP 6: C = 0
STEP 7: REPEAT 8 THROUGH 11 WHILE NUM % P = 0:
STEP 8: NUM = NUM / P
STEP 9: COUNT = COUNT + 1
STEP 10: C = C + 1
STEP 11: IF C = 2 OR COUNT > 3 THEN BREAK OUTER LOOP
STEP 12: P = P + 1
STEP 13: IF P > NUM THEN BREAK
STEP 14: IF COUNT = 3 THEN PRINT "SPHENIC" ELSE PRINT "NOT SPHENIC"

9 thoughts on “Sphenic Number in Java”

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

• In the above example, how the output is 46? It is unclear. The MSD is 2, and not 6.

2. 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?

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

• Sir,how to write the algorithm for the above sphenic number program?

• I have now added the algorithm in this post. See and find out.

3. Sir,how to do the above program using for-loop technique?

• The for loop is not suitable for this program.

• 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