In the previous article, we have seen Java Program to Check Lucky Number
In this article we are going to understand what Insolite number is and how we can check whether a number is Lucky or not in Java with examples.
Java Program to Check Insolite Number
Insolite number is a number which is divisible by the sum and product of squares of the digits of number.
Example: Number = 1122112 Sum of Square of its digits = 1^2 + 1^2 + 2^2 + 2^2 + 1^2 + 1^2 + 2^2 = 16 Product of Square of its digits = (1*1*2*2*1*1*2)^2 = 64 Here we observed that the number 1122112 is divisible by both 16(Sum of Square of its digit) and 64(Product of Square of its digits). Hence it is a insolite number. Other examples of Insolite Number: 111 122121216 111111111 etc.
Let’s see different ways to check Insolite number.
Approach:
- Enter/Declare a number.
- Then find the sum and product of square of it’s digits.
- Divide the number by sum and product of square of it’s digits.
- If it is divisible by both then it is Insolite number else it is not an Insolite number.
Method-1: Java Program to Check Insolite Number By Using Static Value
public class Main
{
// Main method
public static void main (String[] args)
{
int number = 111;
int No = number;
// 'sumValue' variable to store sum of squares of digits
int sumValue = 0;
// 'productValue' variable to store product of squares of digits
int productValue = 1;
while (number != 0)
{
//Getting each digit of number
int digit = number % 10;
//Calculating sumValue
sumValue = sumValue + digit * digit;
//calculating productValue
productValue = productValue * digit * digit;
number = number / 10;
}
//Cheks if the number is divisible by sumValue and productValue
//then it is Insolite number
if ((No % sumValue == 0) && (No % productValue == 0))
System.out.print(No+" is an Insolite Number");
//else it is not an insolite number
else
System.out.print(No+" is not an Insolite Number");
}
}
Output: 111 is an Insolite Number
Method-2: Java Program to Check Insolite Number By User Input Value
import java.util.*;
public class Main
{
// Main method
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number : ");
int number = sc.nextInt();
int No = number;
// 'sumValue' variable to store sum of squares of digits
int sumValue = 0;
// 'productValue' variable to store product of squares of digits
int productValue = 1;
while (number != 0)
{
//Getting each digit of number
int digit = number % 10;
//Calculating sumValue
sumValue = sumValue + digit * digit;
//calculating productValue
productValue = productValue * digit * digit;
number = number / 10;
}
//Cheks if the number is divisible by sumValue and productValue
//then it is Insolite number
if ((No % sumValue == 0) && (No % productValue == 0))
System.out.print(No+" is an Insolite Number");
//else it is not an insolite number
else
System.out.print(No+" is not an Insolite Number");
}
}
Output: Case-1 Enter a number : 111 111 is an Insolite Number Case-2 Enter a number : 11121 11121 is not an Insolite Number
Method-3: Java Program to Check Insolite Number By User Defined Method
public class Main
{
// Main method
public static void main (String[] args)
{
int number = 122121216;
int temp=number;
//Calling checkNumber() method within the if condition
//if itcheckNumber() method returns true value then it is Insolite number
if (checkNumber(number))
System.out.print(temp+" is an Insolite Number");
//else it is not an insolite number
else
System.out.print(temp+" is not an Insolite Number");
}
// checkNumber() user defined method to check if a number
// is an Insolite number or not
static boolean checkNumber(int number)
{
int No = number;
// 'sumValue' variable to store sum of squares of digits
int sumValue = 0;
// 'productValue' variable to store product of squares of digits
int productValue = 1;
while (number != 0)
{
//Getting each digit of number
int digit = number % 10;
//Calculating sumValue
sumValue = sumValue + digit * digit;
//calculating productValue
productValue = productValue * digit * digit;
number = number / 10;
}
if ((No % sumValue == 0) && (No % productValue == 0))
return true;
else
return false;
}
}
Output: 122121216 is an Insolite Number
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: