Thursday, December 26, 2013

Pan Digit Number program

/** Program Name PanDigit.java
     Author :RamRajVasavi

*/
package blog.rrvTechDiamond;
import java.util.Scanner;
public class PanDigit {
             //Writing codefor main() method
public static void main(String[] args) {
//creating object for Scanner Class
Scanner scannerInput=new Scanner(System.in);
                          //Printing Message
System.out.println("Enter No of Digit PanNumber want");
                           //Taking input from endUser
int noOfDigits=scannerInput.nextInt();
                          //Declaring variables
                          boolean check=true;
int panIndex;
int endNo=1;
int index=1;
int integer=10;
int panIf=0;
//creating start and end range
while(index<=noOfDigits)
{
endNo=endNo*integer;
index++;
}
int limit=endNo-1;
int startNo=(endNo/10)+1;
endNo=endNo-(endNo/10)*(10-(noOfDigits+1));
panIndex=startNo;
                          //checking the number is panDigit or not
while(panIndex<=endNo){
                                        //calling panNumber() method
check=panNumber(panIndex,noOfDigits);
if(check==true){
panIf++;
System.out.println(panIndex+" is PanDigit Number");
}
else{
    System.out.println(panIndex+" is not PanDigit Number");
}
panIndex++;
}
System.out.println("The PanDigitNumber from "+startNo+"---"+limit+" is: "+panIf);
}
             //Writing code for panNumber()method 
public static boolean panNumber(int panIndex,int noOfDigits){
                          //creating array object
int arr[]=new int[noOfDigits];
int index=-1;
                          //Number is convert to array
while(panIndex>0){
index++;
int digit=panIndex%10;
arr[index]=digit;
panIndex=panIndex/10;
}
                          //checking for duplicate numbers
for(int digitIndex=0;digitIndex<noOfDigits;digitIndex++){
for(int lowIndex=0;lowIndex<noOfDigits;lowIndex++){
if((digitIndex!=lowIndex)&&(arr[digitIndex]==arr[lowIndex]))
return false;
if(arr[digitIndex]==0)
return false;
}
}
int count=0;
                          //Number contains digits less than that noOfDigits we entered
for(int idex=0;idex<noOfDigits;idex++){
if(arr[idex]<=noOfDigits)
count++;
}
if(count==noOfDigits)
return true;
return false;
}

}


Output:

D:\>java PanDigit
Enter No of Digit PanNumber want
5

10001 is not PanDigit Number
10002 is not PanDigit Number
10003 is not PanDigit Number
10004 is not PanDigit Number
10005 is not PanDigit Number
10006 is not PanDigit Number
10007 is not PanDigit Number
10008 is not PanDigit Number
10009 is not PanDigit Number
10010 is not PanDigit Number
10011 is not PanDigit Number
10012 is not PanDigit Number
10013 is not PanDigit Number
----
----
----
----
----

No comments:

Post a Comment