In this tutorial you will learn about the Program to check Sphenic Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Sphenic Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Sphenic Number ?
Sphenic numbers are numbers which have exactly 3 factors,and all are prime number.
Example :Some of the Sphenic Number are
Number => 30: has Factor’s 2,3,5 and product of 2*3*5= 30 So it is a Sphenic number.
and 42: has Factor’s 2,3,7 and product of 2*3*7= 42 So it is not a Sphenic number.
15: has Factor’s 3,5 and product of 3*5= 15 So it is not a Sphenic number.
Number > 70: has Factor’s 1,7,10 and product of 1*7*10= 70 So it is a Sphenic number.
Program to check Sphenic Number in Java.
In this program we will find given number is a Sphenic number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input a number.Later we find number is Sphenic number or not let’s have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
// Java program to find given number is a Sphenic number or not import java.util.Scanner; import java.util.*; public class Sphenic_Num{ // Creating a boolean array acessible to all functions static boolean arr[] = new boolean[10000]; public static void main(String args[]) { //declaring and assigning value to number Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); int no = sc.nextInt(); // Finding number are prime or not PrimeNumber(); if(sphenic_no(no)) { System.out.println(no+" is a Sphenic number"); } else { System.out.println(no+" is Not a Sphenic number"); } } static void PrimeNumber() { // Filling the boolean array with all values set to true Arrays.fill(arr,true); for(int i = 2; i*i<10000; i++) { if(arr[i]) { //Marking all multiples of i as non prime for(int j = i*2; j<10000; j= j+i) arr[j] = false; } } } //method to check sphenic number static boolean sphenic_no(int number) { int a[] = new int[8]; int count=0, j = 0,i; for(i =1; i<=number;i++) { // Counts the number of divisors if(number%i==0 && count< 8) { count++; a[j++]=i; } } // Returns if the number is Sphenic or not if(count==8 && (arr[a[1]] && arr[a[2]] && arr[a[3]])) return true; else return false; } } |
Output
Sphenic Number
Not a Sphenic Number
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number.
After declaring variables in the next statement user will be prompted to enter a value and which will be assigned to variable ‘number’.
So, first of all we will try to find the number is having three digits (except 1) are prime or not.and product of these number gives the number or not .
we can say that number n is a sphenic if n=a x b x c (a,b, and c are three distinct prime numbers and their product are n)
After all these condition we can say a number follow all this is a Sphenic number.