There is series of 20 numbers composed by two numbers is given like 9876,7698,7676,9898,987698,769876
Develop a logic in java to find out these two numbers series composed by
First number is - 98 Second is-76
How to develop a logic
Another series - 1213,121312,1313,1212,12131213 First Number-12 Second Number-13
anita_2
- 3 Contributors
- forum8 Replies
- 29 Views
- 4 Years Discussion Span
- comment Latest Post by JamesCherrill
JamesCherrill 3,661
There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
Post what you have done so far and someone will help you from there.
milil 24
If it is a array it will be something like this.
int array [] = {9876,7698,7676,9898,987698,769876};
for (int x = 0; x < array.length; x++) {
String number = array[x] + "";
String firstTwoNumbers = number.substring(0,2);
int numberLength = number.length();
String lastTwoNumbers = number.substring(numberLength - 2, numberLength);
if (firstTwoNumbers.equals("98")) {
// it starts with 98...
}
if (lastTwoNumbers.equals("76")) {
// it ends with...
}
}
anita_2
But these number can be anything its not defined that series is made by which number than how to do it.
Or we dont know that number is 2 digit number it can be 1 digit or can be 3 digit.
Like series can be like this-79,97,77,99,777999,999777 Than
milil 24
It can be anything you want it don't need to be array, and you don't need to worry about digits beucause this work like this:
String number = array[x] + "";
It makes string from number.
String firstTwoNumbers = number.substring(0,2);
It will get first two letters from that string, for example:
String number is "97112681919"
firstTwoNumbers will be: 9 (position 0) 7 (position 1) so you will get: 97
int numberLength = number.length();
This will calculate how much digits you have it can be 0 od 10000...0000 .
String lastTwoNumbers = number.substring(numberLength - 2, numberLength);
For last example "97112681919" it has 11 digits so what will this line code do. It will make substring from numberLength that is 11 - 2 so you get 9 and numberLength that is 11 so you will get string from 9 to 11 and that is digit on position 9 and position 10 so you will get: 19
If it can be anything you will need scaner to get input from your keybord and you need to define numbers and "compose numbers" so you can calculate.
anita_2
Problem is-
An Input series has 20 numbers composed by basic two numbers
1> which are these two numbers
2> how many times number1 and numbers2 has occured.
3> how many occurences they have in given 20 numbers.
4> in one occurence max time each one has occured .
5> which one is occured more number of times.
6> which one is occured maximum number of times in one occurence and how many times
milil 24
I give you something to work with, when you write something your own and find a problem i will help you.
anita_2
I wrote this but not working
int num=121121;
int i=1,Fnum,UpNum;
int LastDigit=num%10; //6
UpNum=LastDigit;
Fnum=UpNum;
System.out.println("First Number is"+Fnum+"LastDigit"+LastDigit);
int RDigit=num/10; //98987
System.out.println("LastDigit = "+LastDigit+" RDigit="+RDigit);
String str=Integer.toString(num);
System.out.println("str length"+str.length());
while(i<str.length())
{
String substr=Integer.toString(LastDigit);
if(str.contains(substr))
{
int SecLastDigit=(int) (RDigit%Math.pow(10, i));
UpNum=LastDigit+(int) (SecLastDigit*Math.pow(10, i));
RDigit=RDigit/10;
System.out.println("UpNum"+UpNum);
System.out.println("RDigit"+RDigit);
Fnum=UpNum;
i++;
System.out.println("First Number is"+Fnum);
}
else
{
Fnum=UpNum;
}
break;
}
JamesCherrill 3,661
This looks like a case of premature coding.
There's no way you will magically come up with the right code if you don't understand the algorithm it implements.
The way to understand (or create) an algorithm is to work through it on paper.
I would get some paper and solve one or two actual problems by hand. Then look back and see how you did that... now you have a starting point for the algorithm.
ps: It's the first time I've seen this particular problem. It's very interesting. What are the constraints on the basic 2 numbers...
are they the same length or could they be (eg) 123 and 45?
can one be a subset of the other, eg 1234 and 23? (tricky, that one)
is there any reason why they are numbers not just arbitrary Strings?