Java program to find repeated digits in a number

In our earlier post we learn about redundant digits in an array.Today we shall how to find the redundant digits in any given number.This program is bit complicated since we use lot of if,for,while conditions.
The process of finding the number involves the below stated stages.

1. Input
2 .Getting the list of digits present in the given number
3. Picking up each element in the list and then checking the number whether it is present in the list minimum of two times
4. If in the 3 stage the number is present for a minimum of two times then we shall add that number to an array of redundant elements.
5. If in 3 stage the number is present only once in list then we need not to add that number to the redundant elements array.
6. While adding the elements to the redundant elements we need to check whether the element is already present in the array,if not we can add it else we shall skip.

Terminology used in the program:
1. INPUT:The variable number is used to store the input from the user.
2. CLASS OBJECTS:in,ob are used as objects for the class Scanner and finding respectively.
3. ARRAYS:listofdigits is an array used to store the digits present in the program.redunele is an array used to store the redundant elements in the number.
4. Miscellaneous variables:k,i,r,flag,temp.
5. Methods: listingofdigits is a method which we use to find the digits present in the number,finding is a method where we check for the redundant elements in the listofdigits and add them to the array.
The code of the problem is:

import java.util.*;
class finding
{
 int len,a=0;
 int listofdigits[] = new int[10];
 int redunele[] = new int[10];
 int k,i,r=0,flag=0;
 void listingofdigits(int number)
  {
    int temp=number;
    for(i=0;i<len;i++)
     {
       while(temp>0)
        {
          r = temp%10;
          temp = temp/10;
          break;
        } //while end
      listofdigits[i]=r;
     } //for end
   finding(number);  
  } // function close
 void finding(int number)
  {
    int temp=number;
    while(temp>0)
      {
        flag=0;
        r = temp%10;
        temp = temp/10;
        for(i=0;i<len;i++)
         {
           if(listofdigits[i] == r)
            flag++;
          } //for end
        if(flag>1)
         {
          for(int j=0;j<len;j++)
            {
              if(redunele[j]==r)
                break; 
              else
               {
                 redunele[a]=r;
                 a++;
               } // else close 
             break;
            } //for close
         } // if close
    } // while end
  display();
 } // function end
void display()
 {
    if(a==0)
        System.out.println("No redundant elements:");
     else
      {
       System.out.println("Redundant elements are:");
       for(int i=0;i<a;i++)
        System.out.print(redunele[i]+" ");
      } //else close
 } //function close        
} //class end
class numberredun2
{
   public static void main(String arg[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter any number:");
        int n = in.nextInt();
        finding ob = new finding();
        ob.len = String.valueOf(n).length();
        System.out.println("Lengtht is " + ob.len);
        ob.listingofdigits(n);    
     }
}

1.In the main method we get a number as input from the user.
2.Then we call the method listingofdigits by passing the number as parameter.
3.In this method we list out what are the digits present in the given number. Eg:If the input is 132 then the digits in it are 1,3,2.
4.Then we invoke the method findig.In it select a number in the array of listofdigits and search for the same number.If the number is present more than one then add it to redunele array.If the element is not present then ignore it.
5.Finally display the elements present in the redunele array.

The output of the program is.

numberredund
You can download the code.
You can like,follow our blog.

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s