Program to find occurence of digit 9 between 1 to 1000 | Selenium Forum
M
Posted on 12/09/2015
[b:3w42aekt]Question [/b:3w42aekt]
Hi Ashish /other folks ,

I was practicing my coding skills and was browsing interview questions and came across a very interesting question to print the occurrence of digit 9 between 1 to 1000

[b:3w42aekt]My Source Code[/b:3w42aekt]

public class numberofoccurrences {

public static void main(String[] args) {
int threes=0;
for(int i=1;i<1000;i++)
{
String x=String.valueOf(i);
System.out.println(x);
if(x.charAt(0)==3)
{
threes=threes+1;
}
if((x.charAt(0)==3 || x.charAt(1)==3) ||(x.charAt(0)==3 & x.charAt(1)==3))
{
threes=threes+1;
}
if((x.charAt(0)==3 || x.charAt(1)==3 || x.charAt(2)==3) ||(x.charAt(0)==3 & x.charAt(1)==3 & x.charAt(2)==3))
{
threes=threes+1;
}

}
System.out.println(threes);
}
}


[b:3w42aekt]Error observed[/b:3w42aekt]

[b:3w42aekt][u:3w42aekt]Question -1[/u:3w42aekt][/b:3w42aekt]

Can some one tell me how to resolve this
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at numberofoccurrences.main(numberofoccurrences.java:14)
[b:3w42aekt][u:3w42aekt]What am I doing wrong ?[/u:3w42aekt][/b:3w42aekt]

[b:3w42aekt][u:3w42aekt]Found on Google one more way to crack this[/u:3w42aekt][/b:3w42aekt]

1 of every 10 numbers contains a 9 ("9")
10 of every 100 numbers contain an additional 9 ("90", "91",...)
100 of every 1000 numbers contain an additional 9 ("900", "901",...)
Number of 9s in 1000

= (1000/10)*1 + (1000/100)*10 + (1000/1000)*100 = 100*1 + 10*10 + 100*1 = 300
Notably, this also gives you the per-digit counts, the first rule gives you the number of digits in the 1s place, the second rule gives you the number of digits in the 10s place, and the third rule gives you the number of digits in the 100s place.

This is the source code

int nines = 0;
for(int i = 1; i <= 1000; i++){
for(char c : String.valueOf(i).toCharArray()){
if(c == '9') nines++;
}
}

[b:3w42aekt][u:3w42aekt]Question 2 [/u:3w42aekt][/b:3w42aekt]
[b:3w42aekt][u:3w42aekt]Now here is what I am stuck @- what does the below for loop trying to signify ?[/u:3w42aekt][/b:3w42aekt]
for(char c : String.valueOf(i).toCharArray())

M
Replied on 14/09/2015

use this code
public class Numbers {

public static void main(String[] args) {

int firstDigit = 0;
int secondDigit = 0;
int thirdDigit = 0;
int count = 0;
for (int i = 0; i < 1000; i++) {

if (i == 9) {
count++;
}
if (i > 10 && i < 100) {
secondDigit = i % 10;
if (secondDigit == 9) {
count++;
}
firstDigit = i / 10;
if (firstDigit == 9) {
count++;
}
}

if (i > 100) {
thirdDigit = i % 10;
if (thirdDigit == 9) {
count++;
}
firstDigit = i / 100;
if (firstDigit == 9) {
count++;
}
secondDigit=i%100;
secondDigit=secondDigit/10;
if (secondDigit == 9) {
count++;
}
}

}
System.out.println(count);
}

}


"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1"
means that array does not go that far.

new array[10];
//contains 10 numbers.
array[15];
//calling 15th number will give exception.