Saturday, 19 September 2015

Count number of time Digit Repeats

Count the Number of  time the Digit Repeats

Example:


from 1 to 20 the digit 2 repeats 11 times..


==============================


package com.nataraja.b;


import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class CountDigitRepeat {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number:");
int n=Integer.parseInt(br.readLine());
System.out.println("Enter a Limit:");
int m=Integer.parseInt(br.readLine());
int c=findDigit(n,m);
System.out.println("Count:"+c);
}

public static int findDigit(int n,int m){
int c=0;
for(int i=1;i<=m;i++){
int t=i,r;
while(t!=0){
r=t%10;
t=t/10;
if(r==n){
c++;
t=0;
}
}
}
return c;
}
}

No comments:

Post a Comment