Showing posts with label Sphenic Number in Java. Show all posts
Showing posts with label Sphenic Number in Java. Show all posts

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.");
 }
 }

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...