Tag: java special program

  • Java Program to Display the ATM Transaction

    In this tutorial, we will write an ATM program in Java. The program will represent the ATM transaction executed.

    Operation available in the ATM Transaction are:

    1. Withdraw
    2. Deposit
    3. Check Balance
    4. Exit

    The user will choose one of the above operations:

    • Withdraw is to withdraw the amount from an ATM. The user is asked to enter the amount and after the withdrawal process is complete, we need to remove that amount from the total balance.
    • Deposit is to add an amount to the total balance, here also the user enters the amount to be added to the total balance.
    • Check Balance means simply display the total balance available in the user’s account.
    • Exit is to return the user to the main page from the current transaction mode. For that, we will use exit(0).

    ATM program Java

    The following program uses the switch statement in java to create a case for each transaction or option.

    import java.util.Scanner;
    
    public class ATMProgram
    {
      public static void main(String args[])
      {
        int balance = 5000, withdraw, deposit;
        Scanner sc = new Scanner(System.in);
    
        while (true)
        {
          System.out.println("ATM (Automated Teller Machine)");
          System.out.println("Choose 1 for Withdraw");
          System.out.println("Choose 2 for Deposit");
          System.out.println("Choose 3 for Check Balance");
          System.out.println("Choose 4 for EXIT");
          System.out.print("Choose the operation you want to perform: ");
    
          //user choice input 
          int choice = sc.nextInt();
          switch (choice)
          {
            case 1:
              System.out.print("Enter the amount to be withdrawn: ");
              withdraw = sc.nextInt();
    
              //balance need to be with the withdrawn amount  
              if (balance >= withdraw)  //for successful transaction 
              {
                balance = balance - withdraw;
                System.out.println("Withdrawal successful! Please collect your money.");
              }
              else	//not enough balance
              {
                System.out.println("Insufficient Balance");
              }
    
              System.out.println("");
              break;
    
            case 2:
              System.out.print("Enter amount to be deposited: ");
              deposit = sc.nextInt();
    
              //adding to the total balance 
              balance = balance + deposit;
              System.out.println("Your Money has been successfully depsited:");
              System.out.println("");
              break;
    
            case 3:
              //balance check 
              System.out.println("Available Balance: " + balance);
              System.out.println("");
              break;
    
            case 4:
              //for exit 
              System.out.println("Successfully EXIT.");
              System.exit(0);
          }
        }
      }
    }

    Output:

    ATM Program Java

  • Spy Number in Java

    In this tutorial, we will write java programs to check for Spy Numbers. We will look at two different java programs son Spy Number

    1. Java Program to Check If a Number is Spy number or not.
    2. Java Program to print all the Spy Number within a given Range.

    What is Spy Number?

    A number is said to be a spy number if the sum of all its digits is equal to the product of all its digits.
    Example:

    Number: 123 
    Sum of its digits: 1+2+3 = 6
    Product of its digits: 1*2*3 = 6  
    Sum and Product are equal, hence, 123 is a Spy number.

    Similarly, you may check for other numbers such as 22, 132, 1124, etc.


    Spy Number in Java Program

    1. Java Program to Check If a Number is Spy number or not

    import java.util.Scanner;
    
    public class SpyNumber
    {
       public static void main(String args[])
       {
          int num, product = 1, sum = 0, lastdigit;
          Scanner sc = new Scanner(System.in);
    
          //read from user
          System.out.print("Enter the number: ");
          num = sc.nextInt();
    
          int number = num;
    
          //calculation for sum and product 
          while (num > 0)
          {
             lastdigit = num % 10;
    
            	//calculating sum
             sum = sum + lastdigit;
            	//calculating product
             product = product * lastdigit;
    
             num = num / 10;
          }
    
          //compares the sum and product and print the result
          if (sum == product)
             System.out.println(number + " is a spy number.");
          else
             System.out.println(number + " not a spy number.");
       }
    }

    Output:

    //First Run
    Enter the number: 1124
    1124 is a spy number.

    //Second Run
    Enter the number: 166
    166 not a spy number.


    2. Java Program to print all the Spy Number within a given Range.

    We will create a separate Boolean function (isSpyNumber()) that will return true if the sum of the digits and the product of the digits are equal else will return false and print them accordingly.

    The program takes the input for the lower range and upper range value and will pass those numbers between this range to the function as an argument.

    import java.util.Scanner;
    
    public class SpyNumber
    {
       private static boolean isSpyNumber(int number)
       {
          int lastdigit = 0;
          int sum = 0, product = 1;
    
          //calculation  
          while (number != 0)
          {
             lastdigit = number % 10;
    
            	//calculating sum
             sum = sum + lastdigit;
            	//calculating product
             product = product * lastdigit;
    
             number = number / 10;
          }
    
          //comapre and return true value if found equal  
          if (sum == product)
             return true;
    
          return false;
       }
    
       public static void main(String args[])
       {
          int lwRange, upRange;
          Scanner sc = new Scanner(System.in);
    
          //read from user
          System.out.print("Enter the Lower Range: ");
          lwRange = sc.nextInt();
          System.out.print("Enter the Upper Range: ");
          upRange = sc.nextInt();
    
          System.out.println("The Spy numbers between " + lwRange + " and " + upRange + " are: ");
          for (int i = lwRange; i <= upRange; i++)
          {
             //calling function at each iteration, that is for each number within the range
             if (isSpyNumber(i))
                System.out.print(i + " ");
          }
       }
    }

    Output:

    Enter the Lower Range: 1
    Enter the Upper Range: 1000
    The Spy numbers between 1 and 1000 are:
    1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321


  • Neon Number in Java

    In this tutorial, we will write two programs for the Neon number in Java.

    1. Program to check whether the nuber neon number or not in Java
    2. Display all the neon numbers within the specified range in java.

    Neon Numbers

    A number is said to be a neon number if the sum of the digits of a square of the number is equal t the original number.
    Example: 9 is a Neon number (see explanation in the diagram).

    Procedure to Find Neon Number

    • Read an integer (n), user input.
    • Find the square of that number and store it in a variable.
    • After that, sum each of the digits of the squared number and again store it in some variable.
    • Lastly, compare the sum number with the original number (n). If both are equal then the given number is a neon number, else it is not a neon number.

    Java Program to Check given number is Neon number or not

    Based on the above procedure, let us apply it in a java program to check for neon numbers.

    import java.util.*;
    
    public class NeonNumberExample1
    {
       public static void main(String args[])
       {
          int sum = 0, num;
    
          Scanner sc = new Scanner(System.in);
    
          System.out.print("Enter the Number: ");
          num = sc.nextInt();
    
          //square of a number
          int square = num * num;
    
          while (square != 0)
          {
             int digit = square % 10;
             sum = sum + digit;
    
             square = square / 10;
          }
    
          //compare and display 
          if (num == sum)
             System.out.println(num + " is a Neon Number.");
          else
             System.out.println(num + " is not a Neon Number.");
       }
    }

    Output:

    Enter the Number: 9
    9 is a Neon Number.

    //Another Execution
    Enter the Number: 45
    45 is not a Neon Number.


    Java Program to find Neon numbers between a specified range

    The following java program finds all the neon numbers present in the specified range. The range will be specified by the user during runtime. Using loops the program will find all the Neon numbers within that range.

    import java.util.Scanner;
    
    public class NeonNumber 
    {
    
      public static boolean isNeon(int num) 
      {
         int sum = 0;
         int square = num * num;
    
         while(square != 0)
         {
            sum += square % 10;
            square /= 10;
         }
         return (sum == num);
      }
    
     
      public static void main(String[] args) 
      {
         int lrRange = 0, upRange = 0;
    
    
         Scanner scan = new Scanner(System.in);
    
         //user inputs for max and min value
         System.out.print("Enter lower range: ");
         lrRange = scan.nextInt();
         System.out.print("Enter the upper range: ");
         upRange = scan.nextInt();
    
         // check number 
         System.out.println("\nAll the Neon numbers between "+lrRange+" to "+ upRange+" are: ");
    
         for(int i=lrRange; i<=upRange; i++) 
         {
             //calling function by passing i
            if(isNeon(i))
                System.out.print(i+" ");
         }
         
         scan.close();
      }
    }

    Output:

    Enter lower range: 0
    Enter the upper range: 10000

    All the Neon numbers between 0 to 10000 are:
    0 1 9


  • Keith Number in Java

    In this java programming tutorial, you will learn about Keith Number and its implementation in java. It is frequently asked in Java coding.

    What is Keith Number?

    A positive n digit number x is called a Keith number or repfigit number (repetitive Fibonacci-like digit) if it is arranged in a special number sequence generated using its digits. This sequence has n terms as digit of x and the next term is determined by the sum of previous n terms and so on. And if the number x falls under this sequence then x is a Keith Number. Example: 19, 742, etc

    Explanation: Consider the number 742. Go through the following steps:

    First separate the digit, 7, 4, 2.

    To find the next term, add the digits, 7+4+2 = 13
    New Series: 7, 4, 2, 13.

    To find the next term, add the last three digits of the above sequence, 13+2+4=19
    New Series: 7, 4, 2, 13, 19.

    To find the next term, add the last three digits of the above sequence, 19+13+2=34
    New Series: 7, 4, 2, 13, 19, 34.

    To find the next term, add the last three digits of the above sequence, 34+19+13=66
    New Series: 7, 4, 2, 13, 19, 34, 66.

    To find the next term, add the last three digits of the above sequence, 66+34+19=119
    New Series: 7, 4, 2, 13, 19, 34, 66, 119.

    To find the next term, add the last three digits of the above sequence, 119+66+34=219
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219.

    To find the next term, add the last three digits of the above sequence, 219+119+66=404
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404.

    To find the next term, add the last three digits of the above sequence, 404+219+119=742
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404, 742.

    Now we will stop as we got the number 742 as the term of the series. Hence 742 is a Keith Number.


    What are the Steps to Find Keith Number with programming

    1. Read a number (x).
    2. Separate each digit from the number (x).
    3. Add all the separated n-digits and that will give the next term of the series.
    4. Again, add the last n-terms of the series to find the next term.
    5. Repeat step 4 until the term which the same as the number(x).

    Java Program to check whether the Number is Keith Number or not

    import java.util.Scanner;
    
    public class KeithNumber
    {
      public static void main(String[] args)
      {
        int i, sum = 0, n, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a number: ");
        num = sc.nextInt();
    
        n = num;
    
        //string to get the length
        String s = Integer.toString(num);
        int d = s.length();
        int arr[] = new int[num];
    
        //separating the digit and storing it in array
        for (i = d - 1; i >= 0; i--)
        {
          arr[i] = n % 10;
          n = n / 10;
    
        }
    
        i = d;
    
        /*once the sum value becomes equal or greater than
        that of the entered number, it stops */
        while (sum < num)
        {
          sum = 0;
          for (int j = 1; j <= d; j++)
          {
            sum = sum + arr[i - j];
          }
    
          arr[i] = sum;
          i++;
        }
    
        //compares the sum with the entered number 
        if (sum == num)
          System.out.println(num + " is a Keith Number.");
        else
          System.out.println(num + " is not a Keith Number.");
      }
    }

    Output:

    Enter a number: 742
    742 is a Keith Number.

    //Another output
    Enter a number: 456
    456 is not a Keith Number.


    Another way to do it, by creating a function that will pass Boolean value to the main function.

    import java.util.*;
    
    public class KeithNumber
    {
      //function that checks if the given number is Keith or not  
      static boolean isKeithNum(int num)
      {
        //array list to store the separated digit 
        ArrayList<Integer> terms = new ArrayList<Integer> ();
    
        int temp = num, n = 0;	//n = no. of digits  
    
        //condition to separate the digit 
        while (temp > 0)
        {
          terms.add(temp % 10);
          temp = temp / 10;
    
          n++;
        }
    
        //reverse the List  
        Collections.reverse(terms);
        int next = 0, i = n;
    
        //calculation of next terms
        while (next < num)
        {
          next = 0;
    
          //condition for adding the terms to get new term
          for (int j = 1; j <= n; j++)
            next = next + terms.get(i - j);
    
          terms.add(next);
          i++;
        }
    
        /*now the next is either equal or greater than entered number
        if equal then it is true else false*/
        return (next == num);
      }
    
      //main 
      public static void main(String[] args)
      {
        int number;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a number: ");
        number = sc.nextInt();
    
        //passing the number as an argument to the function  
        if (isKeithNum(number))
          System.out.println(number + " is a Keith number.");
        else
          System.out.println(number + " is not a Keith number.");
    
      }
    }

    Output:

    Enter a number: 19
    19 is a Keith number.


  • Fascinating Number in Java

    In this java program tutorial, we will learn about Fascinating Number and its implementation through Java. We will check whether the number Fascinating Number or not and also another program to find the find Fascinating Number within a given range in java.

    What is Fascinating Number?

    A number is said to be a fascinating number if it is (having at least 3 digits) multiplied by 2 and 3, and then both these product’s results are concatenated with the original number, then the new number contains all the digits from 1 to 9 exactly once. There could be any number of zeros and are ignored.

    Example:
    Consider a Number = 192
    Then multiply it by two and 3
    192 × 2 = 384
    192 × 3 = 576
    Concatenating the above two numbers with the original number, we get:
    “192” + “384” + “576” = 192384576 = result.
    Now the final result contains all the digits from 1 to 9. Hence it is a Fascinating Number.


    Procedure for Fascinating Number

    1. First, check if the entered/given number has three digits or not. If not, print “cannot be a Fascinating Number”.
    2. Else, Multiply the given number with 2 and 3.
    3. Then convert the product result into a string and Concatenate those strings along with the original number.
    4. Iterate the string that we get after concatenation and also keep the frequency count of the digits.
    5. Print “Not a Fascinating Number” if any of the digits (1 to 9) is missing or repeated.
    6. Else, print “It is a Fascinating Number”.

    Java Program for Fascinating Number

    1. Write a Java program to check whether a number is fascinating or not.

    import java.util.*;
    
    public class FascinatingNumber
    {
      public static void main(String args[])
      {
        int num, product2, product3;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter 3 digit Number: ");
        num = sc.nextInt();
    
        //multiplying with 2 and 3
        product2 = num * 2;
        product3 = num * 3;
       
        //concatenating all three numbers 
        String concatstr = num + "" + product2 + product3;
        boolean isfound = true;
    
        //checking for all the digits 
        for (char c = '1'; c <= '9'; c++)
        {
          int count = 0;
    
          //counting the frequency
          for (int i = 0; i < concatstr.length(); i++)
          {
            char ch = concatstr.charAt(i);
           	//comparing 
            if (ch == c)
              count++;
          }
    
          //returns true if any of the condition returns true  
          if (count > 1 || count == 0)
          {
            isfound = false;
            break;
          }
        }
    
        if (isfound)
          System.out.println(num + " is a fascinating number.");
        else
          System.out.println(num + " is not a fascinating number.");
      }
    }

    Output:

    Enter 3 digit Number: 327
    327 is a fascinating number.

    //Another Output
    Enter 3 digit Number: 145
    145 is not a fascinating number.


    2. Java Program to find all the fascinating numbers between the given Range.

    import java.util.Scanner;
    
    public class FascinatingNumberForRange
    {
      //separate method to do the calculation
      public static boolean isFascinatingFunc(int number)
      {
        int digit = 0;
    
        //multiplying and concatinating the numbers
        String str = "" + number + number *2 + number * 3;
    
        // declare an array of size 10
        int arr[] = new int[10];
    
        // comparing array elements with the string Characters
        for (int i = 0; i < str.length(); i++)
        {
          digit = str.charAt(i) - '0';
    
          // ignoring the 0 character
          if (digit == 0 || arr[digit] == 0)
            arr[digit]++;
    
          else return false;
        }
    
        // check for the missing number, ignore 0
        for (int i = 1; i < arr.length; i++)
        {
          if (arr[i] == 0)
            return false;
        }
    
        return true;
      }
    
      public static void main(String[] args)
      {
        // declare variables
        int lrRange = 0, upRange = 0;
        Scanner scan = new Scanner(System.in);
    
        //user Input
        System.out.print("Enter the Lower range: ");
        lrRange = scan.nextInt();
        System.out.print("Enter the Upper range: ");
        upRange = scan.nextInt();
    
        System.out.println("The Fascinating number between " + lrRange + " to " + upRange + " are: ");
    
        // looping for the range
        for (int i = lrRange; i <= upRange; i++)
        {
         	// check number
          if (isFascinatingFunc(i))
            System.out.print(i + " ");
        }
      }
    }

    Output:

    Enter the Lower range: 1
    Enter the Upper Range: 1000
    The Fascinating number between 1 to 1000 are:
    192 219 273 327


  • Tech Number Program in Java

    In this tutorial, we will learn about Tech numbers and create a Java program to check for Tech numbers. We will see two java examples on Tech Numbers with Java Programming.

    What is Tech Number?

    A number is said to be Tech Number if the number is an even digit number and is equally divided into two halves. After that add the equally divided two halves and find the sum of that number. If we get the original number itself then it is a Tech Number.

    Example: 3025, 2025, etc.
    2025 => 20 + 25 => (55)2 => 3025, hence Tech Number.

    Tech Number

    Let us go through the program, we will write two different programs:

    1. Check for the Tech number in java.
    2. print all the tech numbers within a given range.

    Java program and check whether the Number is Tech or not

    import java.util.Scanner;
    
    public class TechNumber
    {
      public static void main(String[] args)
      {
        int n, num, leftNumber, rightNumber, digits = 0, sumSquare = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number: ");
        n = sc.nextInt();
    
        num = n;
        while (num > 0)
        {
          digits++;
          num = num / 10;
        }
    
        //check for even or odd
        if (digits % 2 == 0)
        {
          num = n;
          leftNumber = num % (int) Math.pow(10, digits / 2);
          num = num / (int) Math.pow(10, digits / 2);
          rightNumber = num;
          sumSquare = (leftNumber + rightNumber) *(leftNumber + rightNumber);
    
          //cheks with original Number and print the result
          if (n == sumSquare)
          {
            System.out.println(n + " is a Tech Number");
          }
          else
          {
            System.out.println(n + " is not a Tech Number");
          }
        }
        else //by default Not Tech because the digits are odd
        {
          System.out.println(n + " Not Tech Number");
        }
      }
    }

    Output:

    Enter the number: 3025
    3025 is a Tech Number

    //Another Output
    Enter the number: 1045
    1045 is not a Tech Number


    Java Program to find all Tech Number in a given Range

    import java.util.Scanner;
    
    public class TechNumberEg
    {
      private static boolean isTechFunc(int number)
      {
        // declare variables
        int n = number, count = 0, firstHalf = 0;
        int lastHalf = 0, sum = 0;
    
        //counting number of digits
        while (n != 0)
        {
          n /= 10;
          count++;
        }
    
        // for the digit to be odd
        if (count % 2 != 0) return false;
    
        //dividing in two
        firstHalf = number / (int) Math.pow(10, count / 2);
        lastHalf = number % (int) Math.pow(10, count / 2);
    
        //sum of the two halves
        sum = firstHalf + lastHalf;
    
        // squaring the sum and check with original
        if (sum *sum == number)
          return true;
    
        return false;
      }
    
      public static void main(String[] args)
      {
        int n, num, lrRange, upRange, digits = 0, sumSquare = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the lower range: ");
        lrRange = sc.nextInt();
        System.out.print("Enter the uppper range: ");
        upRange = sc.nextInt();
    
        System.out.println("The Tech Numbers between " + lrRange + " and " + upRange + " are: ");
    
        for (int i = lrRange; i <= upRange; i++)
        {
          if (isTechFunc(i))
            System.out.print(i + " ");
        }
      }
    }

    Output:

    Enter the lower range:: 1
    Enter the uppper range:: 10000
    The Tech Numbers between 1 and 10000 are:
    81 2025 3025 9801


  • Sunny Number in JAVA

    In this tutorial, we will learn about Sunny numbers and create a Java program to check for sunny numbers. We will see two java examples on Sunny Number.

    What is Sunny Number?

    A number N is said to be a sunny number if the number next to the given number (N+1) is a perfect square.

    Example: Let us take a Number 8, then the next number is 8+1=9 and as 3 is a square root of 9, hence 8 is a sunny Number.

    Another example, let the number be 5, then 5+1=6, that has no square roots, hence 5 is not a sunny Number.


    Java Program to Check Whether the Number is Sunny Number or Not.

    The program uses Math.sqrt() function that is an inbuilt function in Java.

    import java.util.Scanner;
    
    public class SunnyNumber
    {
      // Method to check for Sunny
      public static boolean isSunnyFunc(int n)
      {
        if (Math.sqrt(n + 1) % 1 == 0)
          return true;
        else return false;
      }
    
      // main method
      public static void main(String[] args)
      {
        int num = 0;
        boolean result = false;
    
        Scanner scan = new Scanner(System.in);
    
        //taking input
        System.out.print("Enter the number: ");
        num = scan.nextInt();
    
        // calling calculation Function by passing number
        result = isSunnyFunc(num);
    
        //print the result
        if (result)
          System.out.println(num + " is a Sunny number");
        else
          System.out.println(num + " is not a Sunny number");
    
      }
    }

    Output:

    Enter the number: 80
    80 is a Sunny number

    //Another Execution
    Enter the number: 6
    6 is not a Sunny number


    Java Program to find all the Sunny Number between a given Range

    import java.util.Scanner;
    
    public class SunnyNumber
    {
      // Method to check for Sunny
      public static boolean isSunnyFunc(int n)
      {
        if (Math.sqrt(n + 1) % 1 == 0)
          return true;
        else return false;
      }
    
      // main method
      public static void main(String[] args)
      {
        int lrNum, upNum;
        boolean result = false;
    
        Scanner scan = new Scanner(System.in);
    
        // take input from end-user
        System.out.print("Enter the lower range: ");
        lrNum = scan.nextInt();
        System.out.print("Enter the upper range: ");
        upNum = scan.nextInt();
    
        System.out.println("The Sunny number between " + lrNum + " and " + upNum + " are: ");
    
        for (int i = lrNum; i <= upNum; i++)
        {
          if (isSunnyFunc(i))
            System.out.print(i + " ");
        }
      }
    }

    Output:

    Enter the lower range: 1
    Enter the upper range: 100
    The Sunny number between 1 and 100 are:
    3 8 15 24 35 48 63 80 99

    Hope this article on the java program for the sunny number was helpful.


  • Peterson Number in Java

    In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in java.

    What is Peterson Number?

    A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself.

    Example:

    Number = 145
    145 = !1 + !4 + !5
    = 1 + 4*3*2*1 + 5*4*3*2*1
    = 1+24+120
    = 145 =145

    Therefore, we can see that after the sum of the factorial of 145 is equal to the number 145 itself. Hence the Peterson Number.


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

    import java.io.*;
    import java.util.*;
    
    public class GFG
    {
      // find factorial of digits
      static int[] fact = new int[]
      { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
    
      //function
      static boolean peterson(int num)
      {
        int n = num;
        int sum = 0;
    
        while (num > 0)
        {
          int digit = num % 10;
          sum += fact[digit];
          num = num / 10;
        }
    
        return (sum == n);
      }
    
      // main Program
      static public void main(String[] args)
      {
        int num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a number: ");
        num = sc.nextInt();
    
        if (peterson(num))
          System.out.println(num + " is a Peterson number");
        else
          System.out.println(num + " is not a Peterson number");
      }
    }

    Output 1:

    Enter a number: 145
    145 is a Peterson number

    Output 2:

    Enter a number: 55
    55 is not a Peterson number


  • Automorphic Number Program in Java

    In this tutorial, you will learn about the Automorphic Number along with an example and also code java to check whether the number is automorphic or not.

    What is an Automorphic number?

    A number is said to be an automorphic number if the square of the given number ends with the same digits as the number itself. Example: 25, 76, 376, etc.

    Automorphic Number

    Check Whether a number is an Automorphic Number in Java

    import java.util.Scanner;
    
    public class Automorphic
    {
      public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the Number: ");
        int num = in .nextInt();
    
        int count = 0, sqr = num * num;
        int temp = num;	//copying num into temp
    
        //condition applied
        while (temp > 0)
        {
          count++;
          temp = temp / 10;
        }
    
        int lastSquareDigits = (int)(sqr % (Math.pow(10, count)));
    
        //comparing to check Automorphic
        if (num == lastSquareDigits)
          System.out.println(num + " is an Automorphic number");
        else
          System.out.println(num + " is not an Automorphic number");
      }
    }

    Output:

    Enter the Number:
    25
    25 is an Automorphic number

    //Another execution
    Enter the Number:
    23
    23 is not an Automorphic number


    Java Program to Find all Automorphic Numbers in the Interval

    Now what if we need to find Automorphic Numbers between the range of a given number, the above example will not work as it is applied only for a single number.

    To check all the numbers within some given range of numbers, we will create a separate function that will return a true or false value. In the main function, we will call the created function in a loop so that it can check/call for every number.

    The loop will run from the starting number to the range mentioned in the program.

    import java.util.Scanner;
    
    public class AutomorphicClass
    {
      public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
    
        int lower, upper;
    
        System.out.println("Enter the lower Range: ");
        lower = in .nextInt();
        System.out.println("Enter the Upper Range: ");
        upper = in .nextInt();
    
        System.out.println("Automorphic numbers between " + lower + " and " + upper);
        for (int i = lower; i <= upper; i++)
        {
          if (checkForAutomorphic(i))
            System.out.print(i + " ");
        }
      }
    
      //funcion
      private static boolean checkForAutomorphic(int num)
      {
        int count = 0, sqr = num * num;
        int temp = num;	//copying num
    
        //condition check
        while (temp > 0)
        {
          count++;
          temp = temp / 10;
        }
    
        int lastSquareDigits = (int)(sqr % (Math.pow(10, count)));
    
        return num == lastSquareDigits;
      }
    }

    Output:

    Enter the lower Range:
    1
    Enter the Upper Range:
    100
    Automorphic numbers between 1 and 100
    1 5 6 25 76


  • Special Number Program in Java

    Here we will learn how to do programming on Special Number. We will start by understanding, what is Special Numbers and solve the following two questions on a special number in java.

    Question:
    1. Write a program in Java to check whether a number is Special or not.
    2. Write a Java Program to find all special numbers between the interval.

    Also before we begin, if you do not know the working idea of for, while loop and if-else statement in java then click the link below and learn about them as this program uses them.

    What is a Special Number?

    A special number is a number whose sum of the factorial of its digits is equal to the original number. Example: 145 is a special number.

    Java Special Number

    Java Program to check whether a number is Special or not

    To check for a special number, we take the number from the user, then separate the digits using the % operator, and then calculating the factorial of that individual digits. At last, add the digits and check if it’s equal to the original number and print the result accordingly as shown in the following program.

    Source Code:

    import java.util.Scanner;
    
    class Main
    {
      public static void main(String[] args)
      {
        int digit, num, sum = 0, temp;
    
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number you want to check for special number:");
        num = in .nextInt();
    
        temp = num;
    
       // calculating sum of the factorial of its digit
        while (temp != 0)
        {
          digit = temp % 10;
    
         //calculating factorial of a number(digit)
          int fact = 1;
          for (int i = 2; i <= digit; i++)
          {
            fact = fact * i;
          }
    
          sum += fact;	//calling factorial function
          temp = temp / 10;
        }
    
       //check and display the result
        if (sum == num)
          System.out.println(num + " is a Special number");
        else
          System.out.println(num + " is Not a Special Number");
    
      }
    }

    The output of a special numbers in Java.


    Java Program to find all the special numbers between the interval

    In this program, we have created a different function to calculate the sum of the digit and factorial of a number. The program takes two inputs from the user Lower Value and Upper-Value inputs and runs the loop for those values. Also if no special number is found between the upper and lower value, the program prints no value as shown in the program.

    We have used boolean values to check if at least one value is found or not and perform accordingly.

    Source Code:

    import java.util.Scanner;
    
    class Main
    {
      public static void main(String[] args)
      {
        int lowerValue, upperValue;
        boolean checkSpecial = false;
    
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the lowerValue and upperValue Number:");
        lowerValue = in .nextInt();
        upperValue = in .nextInt();
    
        for (int i = lowerValue; i <= upperValue; i++)
        {
          //calling function called specialFunction()
          if (specialFunction(i))
          {
            System.out.print(i + " ");
            checkSpecial = true;
          }
        }
    
        //display this if no special number are found
        if (!checkSpecial)
          System.out.println("Interval between " + lowerValue + " and " + upperValue + " has no special number");
      }
    
      private static boolean specialFunction(int num)
      {
        int digit, sum = 0, temp;
    
        temp = num;
    
        // calculating sum of the factorial of its digit
        while (temp != 0)
        {
          digit = temp % 10;
    
          //calculating factorial of a number(digit)
          int fact = 1;
          for (int i = 2; i <= digit; i++)
          {
            fact = fact * i;
          }
    
          sum += fact;  //calling factorial function
          temp = temp / 10;
        }
    
        return sum == num;
      }
    }

    The output of a special number.