0

I want to print the output : 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 But I am getting this output: 1 34 67 910 1213 1516 1819 211 34 67 910 1213 1516 1819 21

Can someone please explain me the mistake I am making in the logic?

public class BadNews {

    public static final int MAX_ODD = 21;

    public static void writeOdds() {    
    // print each odd number

        for ( int count = 1; count <= (MAX_ODD - 2); count++) {
            System.out.print(count + " ");

            count = count + 2;
            
            // print the last odd number
                    System.out.print(count);
        }
    }

    public static void main(String[] args) {
        // write all odds up to 21
        writeOdds();

        // now, write all odds up to 11
        writeOdds();

    }
}

6 Answers 6

1

First of all, there's no need for the second System.out.print.

Second, I see that you go up to 21, but where in your code do you specify that your second call should go up to 11?

You can do this by making the upper limit a parameter of your writeOdds function:

public static void writeOdds(int upperLimit)
{
   for(int count = 1; count <= upperLimit; count += 2)
   ...

and then you can call it twice, as writeOdds(21) and writeOdds(11).

Oh, and, you can take out the count = count + 2, this is taken care of in the for loop.

2
  • Thank u... I have made the necessary changes but I am not getting 21 in my output chain. it is coming like 1 3 5 7 9...19 1 3 5 7 9... Am I making some mistake in defining the upperlimit? Nov 15, 2013 at 10:35
  • Right, the -2 must go! (modified my answer)
    – Roy Dictus
    Nov 15, 2013 at 10:43
1

Problem is that you are doing count++ and also count=count+2 so you will print once an odd number and once an even

UPDATE

The fastest way to solve the problem is change to while loop

int count = 1;
while( count <= (MAX_ODD - 2)) {
//rest of your code in loop
}

Also you only need one print

0

There are numerous problems I see in your code:

  • Updating the loop variable twice:

Instead update it once in the for statement as;

for ( int count = 1; count <= (MAX_ODD - 2); count+=2)

and delete the line

count = count + 2;
  • Why do you check for MAX_ODD-2 ?

Check for count <= MAX_ODD

  • You would like to have one print statement in the loop. Remove the line;

    // print the last odd number

    System.out.print(count);

  • AS @Roy Dictus suggests, change the writeOdds method to accept a max_odd parameter.

0

First problem is that you're updating your counter twice.
And secondly, on call, how would you precise the MAX_ODD, since it is a static variable. Try this;

public class BadNews {

public static void writeOdds(int MAX_ODD) {    
// print each odd number

    for ( int count = 1; count <= MAX_ODD; count+=2) {

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

        // print the last odd number
        System.out.print(count);
    }
}

public static void main(String[] args) {
    // write all odds up to 21
    writeOdds(21); <-- Pass the MAX_ODD by parameter

    // now, write all odds up to 11
    writeOdds(11);

}
}
0

They don't want you use parametric methods instead they want you use final int plus separate loop for printing odds to 11

public class BadNews {
    public static final int MAX_ODD = 21;

    public static void writeOdds() {
        // print each odd number
        for (int count = 1; count <= (MAX_ODD); count+=2) {
            System.out.print(count + " ");
        }

        // print the last odd number
    }

    public static void main(String[] args) {
        // write all odds up to 21
        writeOdds();
        System.out.println();
        // now, write all odds up to 11
        for (int count = 1; count <= 11; count+=2) {
            System.out.print(count + " ");
        }
    }
}
-1
public class BadNews {
    public static final int MAX_ODD = 21;
        public static void main(String[] args) {
       
        for (int count = 1; count <= MAX_ODD; count+=2) {
            System.out.print(count + " ");
                }
             System.out.println();

        for (int count = 1; count <=(MAX_ODD-10); count+=2) {
            System.out.print(count + " ");
             }
}
}
1
  • If you are solving this problem in practice-it , then it definetely works Jul 31, 2020 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.