A Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n). The Task You are given the two positive integers p and q, where p is lower than q. Write a program to determine how many Kaprekar numbers are there in the range between p and q (both inclusive) and display them all.
Input Format
There will be two lines of input: p, lowest value q, highest value
Constraints:
0<p<q<100000
Output Format
Output each Kaprekar number in the given range, space-separated on a single line. If no Kaprekar numbers exist in the given range, print INVALID RANGE.
22223
99999
In the above range the follwoing numbers should have been generated :-
77778 82656 95121 99999
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int p = scan.nextInt();
int q = scan.nextInt();
boolean exist = false;
if(q <= p){
System.out.println("INVALID RANGE");
return;
}
int m = 0,n = 0;
long sqr = 0;
String numb = "";
String[] digits = new String[2];
for(long i = p; i <= q; i++){
if(i == 1)System.out.print(1 + " ");
else{
sqr = i*i;
numb = String.valueOf(sqr);
if(numb.length() % 2 == 0){
digits[0] = numb.substring(0, numb.length()/2);
digits[1] = numb.substring(numb.length()/2);
}else{
digits[0] = numb.substring(0, (numb.length() - 1)/2);
digits[1] = numb.substring((numb.length() -1)/2);
}
if(digits[0] == "" )
m = 0;
if(digits[1] == "")
n = 0;
if(!digits[1].equals("") && !digits[0].equals("")){
m = Integer.parseInt(digits[0]);
n = Integer.parseInt(digits[1]);
}
if(i == (m + n) ){
System.out.print(i + " ");
exist = true;
}
}
}
if(exist == false){
System.out.println("INVALID RANGE");
}
}
}
Input Format
There will be two lines of input: p, lowest value q, highest value
Constraints:
0<p<q<100000
Output Format
Output each Kaprekar number in the given range, space-separated on a single line. If no Kaprekar numbers exist in the given range, print INVALID RANGE.
22223
99999
In the above range the follwoing numbers should have been generated :-
77778 82656 95121 99999
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int p = scan.nextInt();
int q = scan.nextInt();
boolean exist = false;
if(q <= p){
System.out.println("INVALID RANGE");
return;
}
int m = 0,n = 0;
long sqr = 0;
String numb = "";
String[] digits = new String[2];
for(long i = p; i <= q; i++){
if(i == 1)System.out.print(1 + " ");
else{
sqr = i*i;
numb = String.valueOf(sqr);
if(numb.length() % 2 == 0){
digits[0] = numb.substring(0, numb.length()/2);
digits[1] = numb.substring(numb.length()/2);
}else{
digits[0] = numb.substring(0, (numb.length() - 1)/2);
digits[1] = numb.substring((numb.length() -1)/2);
}
if(digits[0] == "" )
m = 0;
if(digits[1] == "")
n = 0;
if(!digits[1].equals("") && !digits[0].equals("")){
m = Integer.parseInt(digits[0]);
n = Integer.parseInt(digits[1]);
}
if(i == (m + n) ){
System.out.print(i + " ");
exist = true;
}
}
}
if(exist == false){
System.out.println("INVALID RANGE");
}
}
}
No comments:
Post a Comment