Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: answer my code!!!

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default answer my code!!!

    hello im a begginer in java and im trying to "If n is even, divide it in half. If it is odd, multiply it by 3 and add 1. Repeat this operation until reaching 1."

    but my output becomes like this

    13 40 121 60 181 544 1633 4900 14701 44104 132313 396940 1190821 3572464 10717393 32152180 96456541 289369624 868108873 -1690640676 -776954731 1964103104 1597342017 497058756 1491176269 745588134 -2058202893 -1879641382 -1343956849 263096750 789290251 -1927096542 -1486322329 -163999690 -491999069 -1475997206 -133024321 -399072962 -1197218885 703310642 2109931927 2034828486 1809518163 1133587194 -894205713 1612350158 542083179 271041589 813124768 -1855592991 -1271811676 479532269 1438596808 719298404 359649202 179824601 89912300 269736901 809210704 -1867335183 -1307038252 373852541 1121557624 560778812 280389406 140194703 70097351 210292054 630876163 1892628490 1382918175 -146212770

    instead of

    13 40 20 10 5 16 8 4 2 1 (yes the output has to be single line with spaces)

    what do I change in my code to get the output above??
    (here's my code)

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int number = scan.nextInt();
    int i = number;
    System.out.print(i + " ");
    do {
    if (i % 11 == 0){
    do {
    i /= 2;
    System.out.print(i + " ");
    } while (i % 11 == 0);
    }
    else if (i % 11 != 0) {
    do {
    i = i*3+1;
    System.out.print(i + " ");
    } while (i % 11 != 0);
    }
    else {
    System.out.print("Invalid");
    }
    } while (i >= 1);
    }

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: answer my code!!!

        public static void main(String[] args) {
            System.out.print("Enter a number: ");
            Scanner scan = new Scanner(System.in);
            int number = scan.nextInt();
            do{
                System.out.print(number + " ");
                number = number % 2 == 0 ? number / 2 : number * 3 + 1;
            } while(number > 1);
            System.out.println(number);
        }

  3. The Following User Says Thank You to andreaita44 For This Useful Post:

    poppoff (April 22nd, 2022)

  4. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: answer my code!!!

    It works thanks but I see my code and your codes logic is the same but why does yours work but mine doesn't?

  5. #4
    Junior Member
    Join Date
    Dec 2020
    Location
    Gwalior
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: answer my code!!!

    Hey, You can try your code to run from here - https://www.interview.com/online-java-compiler/ I have checked total 13 errors are showing.

    Main.java:1: error: class, interface, or enum expected

    public static void main(String[] args) {

    ^

    Main.java:3: error: class, interface, or enum expected

    int number = scan.nextInt();

    ^

    Main.java:4: error: class, interface, or enum expected

    int i = number;

    ^

    Main.java:5: error: class, interface, or enum expected

    System.out.print(i + " ");

    ^

    Main.java:6: error: class, interface, or enum expected

    do {

    ^

    Main.java:10: error: class, interface, or enum expected

    System.out.print(i + " ");

    ^

    Main.java:11: error: class, interface, or enum expected

    } while (i % 11 == 0);

    ^

    Main.java:12: error: class, interface, or enum expected

    }

    ^

    Main.java:16: error: class, interface, or enum expected

    System.out.print(i + " ");

    ^

    Main.java:17: error: class, interface, or enum expected

    } while (i % 11 != 0);

    ^

    Main.java:18: error: class, interface, or enum expected

    }

    ^

    Main.java:21: error: class, interface, or enum expected

    }

    ^

    Main.java:23: error: class, interface, or enum expected

    }

  6. #5
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: answer my code!!!

    i % 11 is wrong. It's i % 2

Similar Threads

  1. [SOLVED] My answer comes out a +0.020 more than the answer in the book
    By UniverseCloud in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2019, 08:05 PM
  2. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM
  3. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM
  4. how to make a java code of a shaded answer sheet..?
    By wap in forum Java Theory & Questions
    Replies: 4
    Last Post: June 28th, 2012, 06:21 AM
  5. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM