Tag: java special program

  • Bouncy Number Program in Java

    In his tutorial, we will learn how to write a java program to check bouncy number. Before we code, let us understand what is a bouncy number.

    Bouncy Number

    A number is said to be a bouncy number if the digits of that number are in random order that is not sorted. Example: 121575, 521634, etc.

    In other words, a number that is neither an increasing number nor a decreasing number is called a bouncy number.

    • Increasing number: The number whose current digit is greater than or equal to the previous digit is called an increasing number. Example: 12345, 45678, etc.
    • Decreasing number: The number whose current digit is less than the previous digit is called an decreasing number. Example: 54321, 98765, etc.

    Bouncy Number Program in Java

    import java.util.*;
    
    public class Main
    {
      public static void main(String args[])
      {
        int num;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the number: ");
        num = scan.nextInt();
    
        //calling function and checking condition
        if (isIncreasing(num) || isDecreasing(num) || num < 101)
          System.out.println(num + " is NOT a Bouncy number");
        else
          System.out.println(num + " is a Bouncy number");
      }
    
      //user-defined function to check for incresing number
      public static boolean isIncreasing(int num)
      {
        String str = Integer.toString(num);
        char digit;
        boolean flag = true;
    
        for (int i = 0; i < str.length() - 1; i++)
        {
          digit = str.charAt(i);
          if (digit > str.charAt(i + 1))
          {
            flag = false;
            break;
          }
        }
    
        return flag;
      }
    
      //user-defined function to check for decreasing number
      public static boolean isDecreasing(int num)
      {
        String str = Integer.toString(num);
        char digit;
        boolean flag = true;
        for (int i = 0; i < str.length() - 1; i++)
        {
          digit = str.charAt(i);
          if (digit < str.charAt(i + 1))
          {
            flag = false;
            break;
          }
        }
    
        return flag;
      }
    }

    Output:

    //Run 1
    Enter the number: 12345
    12345 is NOT a Bouncy number

    //Run2
    Enter the number: 23561
    23561 is a Bouncy number


  • Java Program to check Krishnamurthy Number

    In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number in java.

    Krishnamurthy Number

    A number is said to be a Krishnamurthy Number if the sum of the factorial of all digits of a number is equal to the original number itself. Krishnamurthy Number is also known as Strong number and is frequently asked questions in interviews.

    Example:

    Input: 145
    Output: Krishnamurthy Number
    = 1! + 4! + 5!
    = 1 + 24 + 120
    = 145
    = Original Number


    Java Program to check Krishnamurthy Number

    import java.util.Scanner;
    
    public class KrishnamurthyProgram
    {
       // boolean function to check Krishnamurthy Number
       public static boolean isKrishnamurthy(int number)
       {
          int sum = 0, lastDigit = 0;
          int temp = number;
    
          // iterating through all the digits
          while (temp != 0)
          {
             lastDigit = temp % 10;
             sum += factFunc(lastDigit); //calling factFunc func for every digit
             temp /= 10;
          }
    
          // returns true if number and sum are equal
          if (sum == number)
             return true;
          return false;
       }
    
       //user defined function to calculate the factorial
       public static long factFunc(int n)
       {
          long fact = 1;
          for (int i = 1; i <= n; i++)
             fact *= i;
    
          return fact;
       }
    
       // main function
       public static void main(String[] args)
       {
          int num = 0;
          boolean checkResult = false;
    
    
          Scanner scan = new Scanner(System.in);
    
          // user input
          System.out.print("Enter an integer: ");
          num = scan.nextInt();
    
          // calling function by passing the entered number
          checkResult = isKrishnamurthy(num);
    
          //Check and display the result
          if (checkResult)
             System.out.println(num + " is a Krishnamurthy number");
          else
             System.out.println(num + " is not a Krishnamurthy number");
    
       }
    }

    Output:

    //Run: 1
    Enter an integer: 145
    145 is a Krishnamurthy number

    //Run: 2
    Enter an integer: 123
    123 is not a Krishnamurthy number

    We have created two different user-defined functions, one to calculate the factorial of a number and another to check the number for Krishnamurthy which is a boolean function.


    Java Program to check Krishnamurthy number within the given Range

    import java.util.Scanner;
    
    public class KrishnamurthyProgram
    {
       // main function
       public static void main(String[] args)
       {
          int lrRange, upRange;
    
          //create Scanner class object to take input
          Scanner scan = new Scanner(System.in);
    
          // take input from end-user
          System.out.print("Enter lower Range value: ");
          lrRange = scan.nextInt();
          System.out.print("Enter Upper Range value: ");
          upRange = scan.nextInt();
    
          System.out.println("The Krishnamurthy number between " + lrRange + " and " + upRange + " are: ");
    
          for (int i = lrRange; i <= upRange; i++)
          {
             if (isKrishnamurthy(i))
                System.out.print(i + " ");
          }
       }
    
       // boolean function to check Krishnamurthy Number
       public static boolean isKrishnamurthy(int num)
       {
          int sum = 0, lastDigit = 0;
          int temp = num;
    
          // iterating through all the digits
          while (temp != 0)
          {
             lastDigit = temp % 10;
             sum += factFunc(lastDigit);	//calling factFunc func for every digit
             temp /= 10;
          }
    
          // compare sum and number
          if (sum == num)
             return true;
          return false;
       }
    
       //user defined function to calculate the factorial
       public static long factFunc(int n)
       {
          long fact = 1;
          for (int i = 1; i <= n; i++)
             fact *= i;
    
          return fact;
       }
    }

    Output:

    Enter lower Range value: 1
    Enter Upper Range value: 1000
    The Krishnamurthy number between 1 and 1000 are: 
    1 2 145

    As you can see, we did the same way by creating separate functions but here we took the user input for both the lower and upper range and called the function within those ranges to find all the possible Krishnamurthy number java.


  • Java Program for ISBN Number

    In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java

    ISBN number program in java.

    ISBN is a 10-digit unique number that is is carried by almost each and every book. It is used to identify the book uniquely. With the help of ISBN, we can easily find a book. The ISBN is legal if 1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 + 5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10 is divisible by 11.

    The first nine digits of the ISBN number are used to represent the Title, Publisher, and Group of the book, and the last digit is used for checking whether ISBN is correct or not.

    Example:

    Let us consider a number: 1259060977
    Check:
    Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
    Now divide the sum by 11 and see if the remainder obtained is zero or not.
    rem = 209%11 = 0
    Since, rem = 0, therefore 1259060977 is a legal ISBN number.


    Steps to write Program for ISBN number Java

    • First, take a 10-digit number input from the user.
    • The second step is to check if the entered number is 10-digits or not because a number cannot be ISBN if it is not a 10-digit number. If it is not a 10-digit number then we will display an invalid message otherwise follow the steps below.
    • For the sum, we need to multiply each digit by 1 to 10 from left to right.
    • Lastly, divide the sum and check if the remainder is zero or not, if it is zero then the entered number is an ISBN number else it is not.

    Now let us implement the above logic to check whether the number is an ISBN or not in java.


    Java Program to check for ISBN Number

    //Program to check the ISBN number in java
    
    import java.io.*;
    
    public class ISBNumberProgram
    {
       public static void main(String[] args) throws IOException
       {
          long num;
          int sum = 0, i, t, d, dNumber;
          String numString;
         
          // User input
          InputStreamReader in = new InputStreamReader(System.in);
          BufferedReader br = new BufferedReader(in);
          
          System.out.print("Enter 10-digit ISBN number: ");
          num = Long.parseLong(br.readLine());
    
          //check if the entered digits are total 10 digits or not
          numString = "" + num;
          if (numString.length() != 10)
          {
             System.out.println("Invalid Number");
             return; //the program will terminate
          }
    
          // computing sum by iterating each digits
          for (i = 0; i < numString.length(); i++)
          {
             d = Integer.parseInt(numString.substring(i, i + 1));
             dNumber = i + 1;
             t = dNumber * d;
             sum += t;
          }
    
          //lastly check and display the result
          if ((sum % 11) != 0)
          {
             System.out.println(num+ " is an Illegal ISBN Number");
          }
          else
          {
             System.out.println(num+ " is a Legal ISBN Number");
          }
       }
    }

    Output: After execution following result will be displayed.

    //Run: 1
    Enter 10-digit ISBN number: 1259060977
    1259060977 is a Legal ISBN Number

    //Run: 2
    Enter 10-digit ISBN number: 8417652579
    8417562579 is an Illegal ISBN Number

    You may create a separate user define function where you can pass the entered number and do the necessary calculation and at the end return the result to the main function. This is how you calculate the ISBN number java.


  • Evil Number in Java

    In this section, we will learn about the evil number and the java program on evil numbers. We will write java code to check the given number for evil number.

    What is Evil Number?

    A number is said to be an evil number if it is a positive whole number that has an even number of 1’s in its binary expansion. Binary expansion refers to the binary representation of a number that is with 0(zero) and 1(one).

    Odious numbers are opposite to evil numbers.

    Example of evil number:

    Input = 3
    The binary value of 3 = 11. Since the 1’s are even, therefore 3 is an evil number.

    Input = 23
    The binary value of 23 = 10111. Since the 1’s are even, therefore 23 is an evil number.

    Input = 4
    The binary value of 4 = 100. Since the 1’s are odd, therefore 23 is not an evil number. But it is an odious number.

    Now we know what is Evil number is, let us see Evil Number Program in Java with source code.


    Java program to check whether the given number is an evil number or not.

    We will take a number and find its binary equivalent and store the binary value in a variable. Then we will check the number of 1’s present in that binary variable. If the number of 1’s is even then it is an evil number else the given number is not an evil number.

    import java.util.Scanner;
    
    public class EvilNumberProgram
    {
       // user-defined method to check evil number
       public static boolean isEvil(int number)
       {
          // calling binaryConversion function by passing the number
          long binValue = binConversion(number);
          int count = 0;
    
          // iteration to increase the count value on every 1's
          while (binValue != 0)
          {
             if (binValue % 10 == 1)
                count++;
    
             binValue /= 10;
          }
    
         	// check for count for even value
          if (count % 2 == 0)
             return true;
    
          // if odd value than return false
          return false;
       }
    
       private static long binConversion(int num)
       {
          long binValue = 0;
          int remainder = 0;
          int i = 1;
    
          //iteration to convert the number into binary
          while (num != 0)
          {
             remainder = num % 2;
             binValue += remainder * i;
    
             num /= 2;
             i *= 10;
          }
    
          return binValue;
       }
    
       public static void main(String[] args)
       {
          // declaration of variables
          int num = 0;
    
          Scanner scan = new Scanner(System.in);
          System.out.print("Enter the positive number: ");
          num = scan.nextInt();
    
          // check the condition of returned value for evil number
          if (isEvil(num))
             System.out.println(num + " is an evil number");
          else
             System.out.println(num + " is not an evil number");
    
       }
    }

    Output: Evil Number Java.

    //Run 1
    Enter the positive number: 23
    23 is an evil number

    //Run 2
    Enter the positive number: 4
    4 is not an evil number

    In the above java evil program, we created two user-defined functions, one for the conversion of an integer to binary and another one that checks the number of 1’s present in the binary number and returns the true-false value to the main function accordingly.


  • Duck Number Program in Java

    In this tutorial, we will learn about the Duck number with examples and write a java program for duck numbers. Before that, you may go through the following topics in java.

    What is a Duck Number?

    A number is said to be a duck number if it is a positive integer number with zeroes in it. The digit zero (0) must not be present at the beginning of the number but can be present anywhere except the initial digit.

    Example of duck number:

    • 3210 is a duck number.
    • 0183 is not a duck number because there is a zero present at the beginning of the number.
    • 00132 is also not a dick number.

    Now you have understood the Duck number, let us do a program to check duck numbers in java.


    Java program to check whether a number is duck number or not

    //Java Duck Number
    
    import java.util.Scanner;
    
    public class DuckNumberProgram
    {
       public static void main(String[] args)
       {
          // variables
          int num, temp;
          boolean flag = false;
    
          Scanner sc = new Scanner(System.in);
    
          //user input
          System.out.print("Enter a positive integer: ");
          num = sc.nextInt();
    
          temp = num;
    
          //checking condition
          while (temp > 0)
          {
             if (temp % 10 == 0)
                 flag = true;
    
             temp = temp / 10;
          }
    
          //checking for flag 
          if (flag)
    
             System.out.println(num + " is a Duck Number");
    
          else      
             System.out.println(num + " is not a Duck Number");
    
       }
    }

    Output:

    //Run: 1
    Enter a positive integer: 2015
    2015 is a Duck Number

    //Run: 2
    Enter a positive integer: 00124
    124 is not a Duck Number

    //Run: 3
    Enter a positive integer: 11245
    11245 is not a Duck Number

    You can create a separate Boolean function that will return true or false like the flag that is working in this program. Hope this article was helpful on Duck Number Program in Java


  • Magic Number Program in Java

    In this section, we are going to learn about the magic number in Java and also how to find magic numbers in java.

    What is Magic Number?

    A number is said to be a magic number if the sum of its digits is calculated till a single digit is obtained by recursively adding the sum of its digits. And the single-digit which is obtained at the end must be 1, then the number is a magic number, otherwise, it is not a magic number.

    Example: 1729 is a magic number.

    Explanation:
    1+7+2+9 = 19, the sum of the digit is 19, again
    1+9 = 10
    1+0 = 1, Therefore after the addition of digits, the last number we got is 1. Hence 1729 is a Magic Number.


    Magic Number in Java

    Let us see an example in java to check whether the number is a magic number or not. We will use a nested while loop in Java.

    import java.util.Scanner;
    
    public class MagicNumberProgram
    {
       public static void main(String[] args)
       {
          int num, rem = 1, temp, sum = 0;
          Scanner sc = new Scanner(System.in);
          System.out.print("Enter number: ");
          num = sc.nextInt();
          temp = num;
    
          //checking the condition with nested while loop
          while (temp > 9)
          {
             while (temp > 0)
             {
                rem = temp % 10;
                sum = sum + rem;
                temp = temp / 10;
             }
    
             temp = sum;
             sum = 0;
          }
    
          if (temp == 1)
          {
             System.out.println(num + " is a Magic Number");
          }
          else
          {
             System.out.println(num + " is not Magic Number");
          }
       }
    }

    Output:

    //Run: 1
    Enter number: 1729
    1729 is a Magic Number

    //Run: 2 Enter number: 123
    123 is not Magic Number

    You can also create a separate Boolean function and return true or false to the main function after checking the number for magic in Java.


  • Buzz Number Program in Java

    In this tutorial, we will start with what is Buzz Number and Write a Java Program to check whether the number is Buzz Number or not.

    Buzz Number

    A Buzz number is one of the special numbers that end with 7 or is divisible by 7.

    For example: 7, 17, 27, 37, etc are buzz numbers because they end with 7, and the numbers 7, 14, 21, 28, etc are also buzzed numbers because they are divisible by 7.


    Program Approach to check the Buzz Number.

    1. First take a number that needs to be checked for Buzz.

    2. We then apply a condition to check the two conditions needed to check for the Buzz number.

    • We check if the last digit is 7 or not with % operator (number % 10 == 7), which gives us the remainder.
    • Then, we check the divisibility of a number by 7 with / operator (num / 7 == 0).

    3. Both of these conditions are checked together in with || operator, in which if either one is true then || (or) operator returns a true value. Indicating the number is Buzz number.

    4. Else the number is not a Buzz number.


    Java Program to check whether the number is Buzz Number or not

    import java.util.Scanner;
    
    public class BuzzNumber
    {
       public static void main(String args[])
       {
          Scanner sc = new Scanner(System.in);
    
          System.out.print("Enter a number to check for Buzz: ");
          int num = sc.nextInt();
    
          //Condition for Buzz number
          if (num % 10 == 7 || num % 7 == 0)
             System.out.println(num + " is a Buzz number.");
          else
             System.out.println(num + " is a not Buzz number.");
       }
    }

    Output:

    //Run: 1
    Enter a number to check for Buzz: 17
    17 is a Buzz number.

    //Run: 2
    Enter a number to check for Buzz: 32
    32 is a not Buzz number.


    Java program to display all the Buzz number in a given Range

    This is another example where we will print all the Buzz numbers in a given range in Java.

    import java.util.Scanner;
    
    public class BuzzNumber
    {
       public static void main(String args[])
       {
          int lRange, uRange;
    
          Scanner sc = new Scanner(System.in);
    
          System.out.print("Enter the lower range: ");
          lRange = sc.nextInt();
    
          System.out.print("Enter the Upper range: ");
          uRange = sc.nextInt();
    
          System.out.print("List of all the buzz number between " + lRange + " and " + uRange);
          for (int i = lRange; i <= uRange; i++)
          {
             //Condition for Buzz number
             if (i % 10 == 7 || i % 7 == 0)
                System.out.println(i);
          }
       }
    }

    Output:

    Enter the lower range: 0
    Enter the Upper range: 50
    List of all the buzz number between 0 and 50
    7
    14
    17
    21
    27
    28
    35
    37
    42
    47
    49

    You may create a separate boolean function to check for the Buzz number and return the number to the main function. Call the boolean function inside the for loop in the main function.


  • Sphenic Number in Java

    In this tutorial, we will start by learning what is Sphenic Number and write a Java Program to check for the Sphenic Number.

    Sphenic Number

    A Sphenic Number is a positive integer n which is a product of exactly three distinct prime numbers. Or we can say that n is a sphenic integer if n = a * b * c (a, b, and c are three distinct prime numbers and their product is equal to n).

    Example: 30, 42, 66, 70, 78, 102, 105, etc.

    Explanation:

    Input : 110
    Output : Yes
    Explanation : Factors of 110 are = 1, 2, 5, 10, 11, 22,55 and 110
                  Take the smallest three prime factors: 2, 5, 11
                   Product of these smallest prime factors:  2*5*11
                                                                                            = 110
                                                                                            = Input =110
                   Hence the number is Sphenic Number.

    Java Program to Check if the Number is Sphenic Number or not

    import java.util.*;
    
    public class SphenicNumberExample1
    {
       static boolean arr[] = new boolean[10000];
    
       static void findPrime()
       {
          Arrays.fill(arr, true);
    
          for (int p = 2; p * p < 10000; p++)
          {
             //if p is not changed, then it is a prime  
             if (arr[p])
             {
                for (int i = p * 2; i < 10000; i = i + p)
                   arr[i] = false;
             }
          }
       }
     
       //checks if the given number is sphenic or not  
       static int isSphenic(int N)
       {
          int[] arr1 = new int[8];
          int count = 0, j = 0;
    
          for (int i = 1; i <= N; i++)
          {
             if (N % i == 0 && count < 8)
             {
                //increments the count by 1      
                count++;
                arr1[j++] = i;
             }
          }
    
          if (count == 8 && (arr[arr1[1]] && arr[arr1[2]] && arr[arr1[3]]))
             return 1;
          return 0;
       }
    
       //main function
       public static void main(String args[])
       {
          int n, result;
    
          findPrime();
          Scanner sc = new Scanner(System.in);
    
          System.out.print("Enter a number: ");
          n = sc.nextInt();
    
          result = isSphenic(n);
    
          if (result == 1)
             System.out.print(n + " is a sphenic Number.");
          else
             System.out.print(n + " is not a sphenic Number.");
       }
    }

    Output:

    //First Execution
    Enter a number: 30
    30 is a sphenic Number.

    //Second Execution
    Enter a number: 165
    165 is a sphenic Number.


  • Java Program to Check if the given number is Emirp Number or not

    In this tutorial of Emirp Number in Java, we will write a program to check for the Emirp number in Java. Let us start by understanding, what is Emirp number.

    Emirp Number

    A number is said to be an Emirp number if it is a prime number and when reversed, the result formed is also a Prime Number. Emirp is backward for Prime, which indicates that a number, when formed backward, is also a prime number. Hence also known as twisted prime numbers.

    Example: 13, 79, 199, 107 etc.

    Explanation: Consider a Prime Number, 13
    The reverse of 13: 31, which is also a prime number. Therefore, 13 is an Emirp Number.


    Java Program to check for Emirp Number Using Function.

    import java.io.*;
    import java.util.*;
    
    public class EmirpNumber
    {
       //function to check for prime, returns boolean
       public static boolean isPrime(int n)
       {
          if (n <= 1)
             return false;
    
          for (int i = 2; i < n; i++)
          {
             if (n % i == 0)
                return false;
          }
    
          return true;
       }
    
       //function to check for emirp number, returns boolean
       public static boolean isEmirp(int n)
       {
          //calls isPrime function and checks for prime
          if (isPrime(n) == false)
             return false;
    
          int rev = 0;
          while (n != 0)
          {
             int digit = n % 10;
             rev = rev *10 + digit;
    
             n = n / 10;
          }
    
          //after reverse again checks for prime  
          return isPrime(rev);
       }
    
       //main function
       public static void main(String args[])
       {
          Scanner sc = new Scanner(System.in);
    
          System.out.print("Enter a number: ");
          int n = sc.nextInt();
    
          //Display accordingly
          if (isEmirp(n) == true)
             System.out.println(n + " is an Emirp number.");
          else
             System.out.println(n + " is not an Emirp number.");
       }
    }

    Output:

    //First Run
    Enter a number: 13
    13 is an Emirp number.

    //Second Run
    Enter a number: 107
    107 is an Emirp number.

    //Third Run
    Enter a number: 23
    23 is not an Emirp number.


  • Autobiographical Number in Java

    In this tutorial, we will write an Autobiographical Number Program in Java and also learn what is Autobiographical Number.

    Autobiographical Number

    An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there, and so on. Basically, it counts the frequency of digits from 0 to 9 in order which they occur in a number.

    Example: 1210 has 1 zero, 2 ones, 1 two, and 0 threes.
    Some other examples include 2020, 21200, 3211000, 42101000, etc.

    Explanation: Consider a number, 21200.

    First, the sum of the digits: 2+1+2+0+0 = 5
    Second, count the number of digits: 5
    Since the sum of the digits is equal to the number of digits present in a number. Therefore, 21200 is an autobiographical number.


    Java Program to check if the given number is Autobiographical Number or not

    import java.util.*;
    
    public class AutobiographicalNumber
    {
       public static void main(String args[])
       {
          int num, n;
          String str;
          boolean flag;
          Scanner sc = new Scanner(System.in);
          
          System.out.print("Enter the number: ");
          num = sc.nextInt();
          
          num = Math.abs(num);
          n = num;
          
          str = String.valueOf(num);
    
          int digitarray[] = new int[str.length()];
          for (int i = digitarray.length - 1; i >= 0; i--)
          {
             digitarray[i] = n % 10;
             n = n / 10;
          }
    
          flag = true;
          
          for (int i = 0; i < digitarray.length; i++)
          {
             int count = 0;
             for (int j = 0; j < digitarray.length; j++)
             {
                if (i == digitarray[j])
                   count++;
             }
    
             if (count != digitarray[i])
             {
                flag = false;
                break;
             }
          }
    
          if (flag)
             System.out.println(num + " is an Autobiographical number.");
          else
             System.out.println(num + " is not an Autobiographical number.");
       }
    }

    Output:

    //First Run
    Enter the number: 1210
    1210 is an Autobiographical number.

    \\Second Run
    Enter the number: 1410
    1410 is not an Autobiographical number.