Write your code in the file LuckyNinesjava Use the IO module
Solution
LuckyNines.java
import java.util.Scanner;
public class LuckyNines {
public static void main(String s[]){
Scanner scan = new Scanner(System.in);
System.out.print(\"The lower end of the range: \");
int lower = scan.nextInt();
System.out.print(\"The upper end of the range: \");
int upper = scan.nextInt();
int count = countLuckyNines(lower, upper);
System.out.println(\"RESULT: \"+count);
}
public static int countLuckyNines(int lowerEnd, int upperEnd){
if(upperEnd < lowerEnd){
return -1;
}
else{
int count = 0;
for(int i=lowerEnd; i<=upperEnd; i++){
for(int j=i; j>0; j=j/10){
if(j % 10 == 9){
count++;
}
}
}
return count;
}
}
}
Output:
The lower end of the range: 100
The upper end of the range: 150
RESULT: 5
