Java Program to Check Katadrome Number

In the previous article, we have discussed Java Program to Check Tcefrep Number

In this article we will see how to check a number is a Katadrome number or not in Java programming language.

Java Program to Check Katadrome Number

Before going to the actual program, let’s first know what is a Katadrome Number.

Katadrome Number is a number whose digits are in decreasing digits.

Example:

Katadrome numbers:
4321 
432
21
210
2
1
3
Not Katadrome numbers:
123
6710
13
890

Let’s see different ways to check katadrome number.

Approach:

  1. Declare or take a number input.
  2. Traverse all digits from right to left and check if any digit is smaller than previous. If smaller found then it is not katadrome number else it is a katadrome.

Method-1: Java Program to Check Katadrome Number By Using Static Value

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {

        int number = 6541;
        // Method Called
        if (checkNumber(number))
            System.out.print(number+" is a Katadrome number");
        else
            System.out.print(number+" is not a Katadrome number");
    }
    
    // Method to check if a number is a Katadrome number or not
    static boolean checkNumber(int number)
    {
         
        // To store previous digit
        int previous = -1;
     
        // Traversing all digits from right to left 
        // and checking if any digit is smaller than previous.
        while (number > 0)
        {
            int digit = number % 10;
            number /= 10;
            if (digit < previous)
                return false;
            previous = digit;
        }
        return true;
    }
}
Output:

6541 is a Katadrome number

Method-2: Java Program to Check Katadrome Number By User Input Value

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        //a number declared
        System.out.println("Enter a number:");
        int number = sc.nextInt();
        //Actualnumber assigned to atemp variable
        int temp=number;
        //flag valueinitialized as 1
        int flag=1;
            
         
        // To store previous digit
        int previous = -1;
        
        // Traversing all digits from right to left 
        // and checking if any digit is smaller than previous.
        while (number > 0)
        {
            int digit = number % 10;
            number /= 10;
            if (digit < previous)
                {
                flag=0;
                System.out.print(temp+" is not a Katadrome number");
                break;
                }
            previous = digit;
        }
        if(flag==1)
            System.out.print(temp+" is a Katadrome number");
    }
}
Output:

Case-1

Enter a number:65 
65 is a Katadrome number

Case-2

Enter a number:659
659 is not a Katadrome number

Method-3: Java Program to Check Katadrome Number By User Defined Method

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int number = sc.nextInt();
        // Method Called
        if (checkNumber(number))
            System.out.print(number+" is a Katadrome number");
        else
            System.out.print(number+" is not a Katadrome number");
    }
    
    // Method to check if a number is a Katadrome number or not
    static boolean checkNumber(int number)
    {
         
        // To store previous digit
        int previous = -1;
     
        // Traversing all digits from right to left 
        // and checking if any digit is smaller than previous.
        while (number > 0)
        {
            int digit = number % 10;
            number /= 10;
            if (digit < previous)
                return false;
            previous = digit;
        }
        return true;
    }
}
Output:

Case-1

421 is a Katadrome number

Case-2

4261 is not a Katadrome number

Are you wondering how to seek help from subject matter experts and learn the Java language ? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: