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 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
Let me check.
Sir,it’s working.Thank you sir.
I have updated the sphenic number program now. Please check it. Thanks for your feedback.
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.
Eg:
N=5
1 2 3 4 5
output:
5 3 1 2 4
Find the solution of this question here: https://www.happycompiler.com/arrange-elements-center-array/
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,in the above program for N=62,33,…,it’s not showing any output,whyis it so?
Let me check it once again.
Now check. I think the program works fine now.
In the above program if we input N=18,what output will it show?
18 isn’t a sphenic number.
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.
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.
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