60 472
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #69803 in Java | JSP | JSF for srevarun

Question #69803
Tyomitch calls the number with 2N digits (without leading zeroes) "interesting", if it's divisible
by both the number formed from its first N digits and the number formed from its last N digits.
For example, 1020 is "interesting" (divisible by 10 and 20) and 2005 is not. Tyomitch wants to
know how many "interesting" 2N-digit numbers exist. You are to help him.
Input
Input contains an integer N (1 ≤ N ≤ 10000).
Output
Output the number of "interesting" 2N-digit numbers.
Expert's answer

public class InterestingNumbers {

public static void main(String[] args) {

int numbers [] = new int[100000];
int count = 0;

for (int i = 1; i <= numbers.length; i++) {
numbers[i - 1] = i;
if((Integer.toString(numbers[i - 1]).length() % 2) == 0) {
String numberToStr = Integer.toString(numbers[i - 1]);

String firstNToStr;
String lastToStr;
if (numberToStr.length() == 2) {
firstNToStr = numberToStr.substring(0, 1);
lastToStr = numberToStr.substring(1, 2);
}
else {
firstNToStr = numberToStr.substring(0, (numberToStr.length() / 2));
lastToStr = numberToStr.substring(numberToStr.length() / 2, numberToStr.length() - 1);
}



if ((firstNToStr.charAt(0) == '0') || (lastToStr.charAt(0) == '0'))
continue;

int firstN = Integer.parseInt(firstNToStr);
int lastN = Integer.parseInt(lastToStr);

if(((numbers[i - 1] % firstN) == 0) && ((numbers[i - 1] % lastN) == 0)) {
count++;
System.out.println(numbers[i - 1]);
}
}
}

System.out.println("==========\n" + count);
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions