0
/**
 * Created by abdul on 10/17/2016.
*/
import java.util.Scanner;
public class CollatzSequence {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter Cases: ");
    int cases = in.nextInt();
    int count = 0;
    for (int i = 0; i <= cases; i++) {
        int a = in.nextInt();
        do {
            if (a % 2 == 1) {
                count++;
                break;
            } else if (a % 2 == 0){
                a = a / 2;
                a = a;
                count++;
            } else {
                a = 3 * (a + 1);
                a = a;
                count++;
            }
        }while (a != 1) ;
    } System.out.println(count);
  }
}

this is not homework this is from code abbey http://www.codeabbey.com/index/task_view/collatz-sequence X is the initial number For example if X is even (i.e. X modulo 2 = 0) then Xnext = X / 2 else Xnext = 3 * X + 1 When x = 15 It takes 17 times for the sequence for it to reach 1. Please help me what I am doing wrong my loop will not even give an output anymore and before that wouldn't stop adding number

AAnwar007
  • 43
  • 2
  • 6
  • Move `System.out.println(count)` into your for loop. – Jonny Henly Oct 17 '16 at 21:16
  • 1
    3 conditions for a is odd, a is even and the rest?? do you think the third condition will ever be reached? – Jean-François Fabre Oct 17 '16 at 21:16
  • `a = a` does absolutely nothing. `a % 2` can only be `0` or `1`. If `a` is odd your code will break out of the while loop, remove your first `if` condition. – Jonny Henly Oct 17 '16 at 21:17
  • A lot of people helped you on the issue, you received two answers with solutions. It is expected from you to choose an answer as correct or state why it did not solve your issue. This is how SO works, please follow the rules. – thst Oct 22 '16 at 21:42

2 Answers2

0

You have an a%2==1 condition, and an a%2==0 condition, and then a condition for when neither of those are true. So you have a condition for even numbers, a condition for odd numbers, and a condition for numbers neither even nor odd. Since there are no numbers that are neither even nor odd, this makes no sense.

Try:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter Cases: ");
    int cases = in.nextInt();

    for (int i = 0; i <= cases; i++) {
        int count = 0;
        int a = in.nextInt();
        do {
            if (a % 2 == 1) {
                a = (3 * a) + 1;
                count++;
            } else if (a % 2 == 0){
                a = a / 2;
                count++;
            }
        } while (a != 1);
        System.out.println(count);
    }
}

I also removed the a=a statements because they don't do anything.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
0

Your first mistake is, that you do not read 3 numbers, but you read the amount of expected test-numbers and after reading a line, you immediately start your calculation.

  1. Separate the calculation from the input gathering.
  2. Move the calculation into it's own class, so you have it really separate.

Understand break:

The code breaks inside the if, that will not exit the if, it will exit the do ... while on the first odd result. So entering 17 will immediately read the next integer.

nohouser9 already gave you a correct working sample, so I will leave it with that.

Update: No, I don't :-)

The count will be output in your current code after the for-loop, so you will only see the last result calculated. After the do ... while has finished, you read the next integer. Since your code does not output any result after the do, it looks like it has frozen (for cases > 1).

And finally: You are calculating wrong. The sequence is

3 * x + 1 

for odd numbers. But you are using the formula:

3 * ( x + 1 )

One more: You are running one number too many: In the for loop, you are running from 0 ... cases which is cases + 1 loops. You need to fix i < cases in the for loop.

I have rewritten your code:

public class CollatzMain {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Cases: ");
        int cases = in.nextInt();

        for (int i = 1; i <= cases; i++) {
            System.out.printf("Enter Sample (%d): ", i);
            int a = in.nextInt();
            int count = collatzSequenceLength(a);
            System.out.printf("Input %d -> %d steps to 1.%n", a, count);
        }
    }

    private static int collatzSequenceLength(int a) {
        int count = 0;

        do {
            count++;
            // alternative to if:
            // a = ((a % 2) == 0) ? (a >> 1) : (a * 3) + 1

            if ((a % 2) == 0) {
                a = (a >> 1); // since we do integer math, we can shift bits to divide by 2. a = a/2 is fine, though...
            }
            else {
                a = a * 3 + 1;
            }
        }
        while(a!=1);

        return count;
    }
}

For the fun of it, the code for the sequence can be written in a body-less for-loop. Bad style, though, but fun :-)

for(count = 0; a!=1; count++, a = ((a % 2) == 0) ? (a >> 1) : (a * 3) + 1 );
thst
  • 4,592
  • 1
  • 26
  • 40