AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Strange Number Range

A positive whole number N that has D number of digits is squared and split into two pieces, a right-hand piece that has D digits and a left-hand piece that has remaining D or D-1 digits. If the sum of the two pieces is equal to the number, then N is a Strange number.

E.g: take number 45. 45 square = 2025. Since 45 is a 2 digit number split 2025 as 20 and 25. Now 20 + 25 = 45 which is the original number. Hence 45 is a Strange number.

Write a java program that will take two positive integers m and n (where m < = n) and print all the Strange numbers between m and n (inclusive).



Note : If m > n then print "Invalid Input".



Sample Input 1:

1 10

Sample Output 1:

Strange numbers between 1 and 10 are: 1,9

Sample Input 2:

10 1

Sample Output 2:

Invalid Input

corejava x 352
programming x 168
Posted On : 2017-02-11 21:22:47.0
profile Raksha - anyforum.in Raksha
2510
up-rate
3
down-rate

Answers


Hi Raksha,
I think you missed something in above problem. 10 is also a strange number which lies in the range from 1 to 10 which is missed in sample output 1. Please check below code:


StrangeNumbers.java:
------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StrangeNumbers {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Integer m=input.nextInt();
Integer n=input.nextInt();
Integer square=0;
String sqr=null;
String num=null;
List<Integer> list=new ArrayList<Integer>();
if(m<=n){
int left=0;
int right=0;
for(Integer i=m;i<=n;i++){
square=i*i;
sqr=square.toString();
num=i.toString();
right=Integer.parseInt(sqr.substring(0,num.length()));
if(sqr.length()<2){
left=0;
}else{
left=Integer.parseInt(sqr.substring(num.length(),sqr.length()));
}
if(left+right==i){
list.add(i);
}
}
System.out.println("Strange numbers between "+m+" and "+n+" are:"+list.toString().replace("[","").replace("]",""));
}else{
System.out.println("Invalid Input!!");
}
}
}

Posted On : 2017-02-13 01:38:50
Satisfied : 5 Yes  4 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221961
Reply This Thread
up-rate
7
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in