Results 1 to 8 of 8
Thread: Finding the highest number
- 11-04-2008, 06:36 AM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Finding the highest number
I am stooped, I can't figure out how to do this without totally exploding my code.
Here is what I have to do
Took me like 10 minutes to make it, except I can't think of how to get the highest number in here.Your job for this assignment is to write a one class program that generates a sequence of the so-called hailstone numbers, and report certain statistics on the sequence your program generates. The hailstone numbers can be described succinctly as follows: Start with a positive integer; if that number is even, divide it in half, and continue; if the number is odd, triple it and add 1, and continue. Proceed in this way until you arrive at the value 1.
For example, if you start with 20, this is the sequence you get: 20,10,5,16,8,4,2,1. If you start with 21, you get this sequence: 21, 64, 32,16, 8, 4, 2, 1.
Why are these called hailstone numbers? There's an explanation here, and also in the January 1984 issue of Scientific American.
Here are your precise requirements for the assignment: in one class, write a program that accepts a positive integer input from the keyboard. This is the starting value for your sequence. Then your code should generate and print out, in a column, hailstone numbers until the value 1 is reached. After 1 has been reached, your program should print out: 1) the largest value appearing in the sequence; and 2) the length of the sequence you generated.
For example, here is the output for the sequence that begins with the seed value 21:
Welcome to DrJava.
> java Hail
Enter a positive hailstone starting value
21
21
64
32
16
8
4
2
1
start: 21
term count: 8
biggest: 64
Here's what mine looks like, can anyone give suggestions or something? Thanks
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer");
int num10;
int m=1;
int num1 = scan.nextInt();
int numstart= num1;
while (num1!=1) {
m++;
if (num1 %2==0) {
num1=num1/2;
System.out.println(num1);
} else if(num1%2==1) {
num1=num1*3+1;
System.out.println(num1);
} else {
if(num1==1)
;
}
}
System.out.println("Start Number=" + numstart);
System.out.println("High Number=");
System.out.println("Loops=" + m);
}
- 11-04-2008, 06:48 AM #2
you can insert all got hailstone numbers in the array and then to figure out which is the highest among them.
- 11-04-2008, 06:51 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Use another dummy value and compare it with all calculated num1 to find the max.
- 11-04-2008, 06:54 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Here is a very easiest way. Think about the logic first lol, before implementing. Start from the very basis thing what you know.
Java Code:public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter an integer"); int num10; int m = 1; int max = 0; int num1 = scan.nextInt(); int numstart = num1; while (num1!=1) { m++; if (num1 %2==0) { num1=num1/2; System.out.println(num1); } else if(num1%2==1) { num1=num1*3+1; System.out.println(num1); } else { if(num1==1) ; } // Finding the max if(max < num1) max = num1; } System.out.println("Start Number=" + numstart); System.out.println("High Number=" + max); System.out.println("Loops=" + m); }
- 11-04-2008, 07:07 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Thank you, you are like the awesomest person ever, but I am still a tad unsure of what that just did, at the end of the while loop, you said that if 0<num1
the max = num1?
I am still a bit unclear, could you explain it a bit more, thanks again.
I have a feeling it's quite simple, but I always mess up on simple things.
- 11-04-2008, 07:31 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'll explain it bit more.
Your requirement is finding the maximum number out of few values/numbers.
So you need a variable to hold the maximum. Since you are looking an int value,
But you have to initialize a local variable before use, so the above line change as follows.Java Code:int max;
All the calculated values are positive and so the minimum value you can have is zero.Java Code:int max = 0;
Then in the if and if-else of the while loop you are calculate the result, num1. Then at the end what you have to do is, compare it with the max value. If the value max hold currently is greater than max not change, else max is the newly calculated value, num1.
- 11-04-2008, 08:10 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Alright I think I actually get it now, thanks for your help.
- 11-04-2008, 08:14 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Glad to help you lol. Mark the thread as solved if you really solve your question.
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
The highest number
By I-Shine in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:05 AM -
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks