Friday, October 11, 2013

Digit available number of times in given range

package com.rrvtechdiamond;

import java.util.Scanner;

class NumberOfDigits
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner=new Scanner(System.in);
                System.out.println("Enter up to which range you want: "); 
int numberRange=scanner.nextInt();
System.out.println("Enter which digit you want");
int digit=scanner.nextInt();
int count=0;/*count for counting number of digit available within                     given range*/
//for loop start with 0 up to given range
for(int i=0;i<numberRange;i++){
int innerCount=0;/*this count for given digit is available or                                           not if given digit is find more than one time we                                                will print that i value*/
int j=i;
while(j>0)
{
int temp=j%10;
if(temp==digit)
{
innerCount++;
count++;
}
j=j/10;
}
if(innerCount>0){
System.out.println(i);
}
}
System.out.println("You enter digit comes in "+count+" times");
}
}

OUTPUT:

Enter up to which range you want:
100
Enter which digit you want
8
8
18
28
38
48
58
68
78
80
81
82
83
84
85
86
87
88
89
98
You enter digit comes in 20 times

No comments:

Post a Comment