How to check if a number is Hoax or not in Java
What is a Hoax Number?
A Hoax number is defined as a composite number, whose sum of digits is equal to the sum of digits of its distinct prime factors.
Note: 1 is not considered a prime number, hence it is not included in the sum of digits of distinct prime numbers.
For example:
Input: 22 The sum of digits: 2+2=4 Prime factors: 2,11 The sum of digits of its distinct prime factors: 2+1+1=4 Output: it is a Hoax number
Input: 15 The sum of digits: 1+5=6 Prime factors: 3,5 The sum of digits of its distinct prime factors: 3+5=8 Output: it is not a Hoax number
Input: 84 The sum of digits: 8+4=12 Prime factors: 2,3,7,2 Distinct prime factors: 2,3,7 The sum of digits of its distinct prime factors: 2+3+7=12 Output: it is a Hoax number
Steps to find if Number is Hoax or not:
- Enter the number.
- Obtain prime factors of a number.
- Find the sum of digits of distinct prime factors.
- Find the sum of digits of the number.
- Check if the sum obtained from steps 3 and 4 are equal or not.
- If sums are equal then the number is a Hoax number.
How to check if a number is a Hoax or not in Java
Below is our given Java code that is checking a number if it is Hoax or not:
import java.util.*;
public class pfactors{
public static void main(String[] args){
int num,num1,sum=0,n,i,sum2=0,digit;
Scanner sc=new Scanner(System.in);
//enter the number
n=sc.nextInt();
int temp=n;
List<Integer> value = new ArrayList<Integer>();
List<Integer> value1 = new ArrayList<Integer>();
//check till number is greater than 1
while(n>1){
//find prime factors for a given number
//smallest prime number is 2
for(i=2;i<=n;i++){
if(n%i==0){
n=n/i;
value.add(i); //add in the arraylist
}
}
}
//to check duplicates in the arraylist
for(int x: value){
if(!value1.contains(x)){
value1.add(x); //add distinct values from arraylist in another arraylist
}
}
//find sum of digits in arraylist
for(int j=0;j<value1.size();j++){
num=value1.get(j);
while(num>0){
num1=num%10;
if(num1!=0){
sum=sum+num1;
}
num=num/10;
}
}
System.out.println("sum of digits of prime factors: "+sum);
//find sum of digits of the number entered
while(temp>0){
digit=temp%10;
sum2=sum2+digit;
temp=temp/10;
}
System.out.println("sum of digits of number entered: "+sum2);
//check if number is hoax or not
if(sum==sum2){
System.out.println("number is hoax");
}
else{
System.out.println("number is not hoax");
}
}
}
Output:
84 sum of digits of prime factors: 12 sum of digits of number entered: 12 number is hoax
15 sum of digits of prime factors: 8 sum of digits of number entered: 6 number is not hoax
Explanation of this program:
- Enter the number, n to be checked.
- If n is greater than 1, Iterate the loop. The prime number starts from 2, so initially, start the loop from 2, i.e. i=2.
- if n%i=0, add i in the ArrayList and return n=n/i for further iterations.
- To check distinct prime factors, check if the ArrayList contains any duplicate elements and add distinct elements in another ArrayList.
- Now, to find the sum of digits in ArrayList, iterate the loop till the size of ArrayList.
- Find the sum of each digit present in the array.
- Print the sum of digits of prime factors.
- Now, find the sum of digits of the number entered, and print.
- Finally, check the sums obtained from steps 7 and 8, if they are equal then the number is a Hoax else It is not a Hoax number.
This is how we can check if the number is a Hoax or not. I hope you find this tutorial useful.
Keep it up ishikaa , even I want to learn Coding