0

I have two while loops where one is taking the odd numbers between 50 to 100 and the other is taking the even numbers from the same between 50 to 100. It is printing out well and it works but my professor wants me to transform it into one while loop instead of two. I am having trouble with it because I am using the print statement before in order so it fits into when the numbers go in.

 int e = 50;
 int o = 51;
 System.out.print("Even numbers between 50 and 100: 50,");
 while (e <= 98){
     e += 2;
     if (e%2 == 0){
        System.out.print(e + ",");
     }
 }
 System.out.print("\nOdd numbers between 50 and 100: 51,");
 while (o <= 97){
     o+= 2;

     if (e%1 == 0) { 
         System.out.print(o + ",");
     }
}


Even numbers between 50 and 100: 50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,
Odd numbers between 50 and 100: 51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,

The output looks like this right now, but I need to be able to do it with just one loop instead

2048290348089
  • 17
  • 1
  • 8
  • 3
    `e%1 == 0` - you didn't mean to type that – harold Sep 23 '16 at 13:06
  • 1
    Store the even and odd into arrays or strings and then print at the end – Taylor Sep 23 '16 at 13:11
  • e%1 == 0 doesn't give you any extra information. a%b == 0 means that a is divisible by b. So, saying e%1 == 0 is saying that e is divisible by 1, which will always be true. What you meant to use here is e%2 == 0 (which means that e is divisible by 2 which means e is even) and e%2 == 1 (which means that e is not divisible by 2 which means e is odd). – Nathan M. Sep 23 '16 at 13:15

6 Answers6

3

If you only want one while loop, and separate lines, you should build your strings and print them afterwards. Something like:

StringBuilder odds = new StringBuilder();
StringBuilder evens = new StringBuilder();
i = 50;
while(i<100) {
    if(i%2 == 1) {
        odds.append(i).append(",");
    }
    else {
        evens.append(i).append(",");
    }
    i++;
}
// remember that odds and evens now probably ends with a ','which you should remove
odds.removeCharAt(odds.size());
evens.removeCharAt(evens.size());
// or do something like the following:
//odds.append(" and nothing more!");
//evens.append(" and nothing more!");

System.out.println("Odd numbers are: ");
System.out.println(odds);
System.out.println("Even numbers are" ");
System.out.println(evens)
Koos Gadellaa
  • 1,220
  • 7
  • 17
  • I always wonder if it actually helps, and not already optimized to stringbuilders by the compiler. But yes, you're right! What do I know... http://stackoverflow.com/questions/11942368/why-use-stringbuilder-explicitly-if-the-compiler-converts-string-concatenation-t – Koos Gadellaa Sep 23 '16 at 13:15
  • If I can find the link I'll add it to this comment, but iirc adding to a string using + does in fact use string builders, but it's a new object every iteration of the loop. So it's definitely slower. – jonhopkins Sep 23 '16 at 13:17
  • Yup, I found it too :) – Koos Gadellaa Sep 23 '16 at 13:17
  • In case anyone is curious, I did a quick mock up using repl.it. I wrote two loops that append the numbers 0 to 99999 into a string, one using string and one using StringBuilder. The implementation using string took 32.395368s to concatenate the numbers, while StringBuilder took 0.003023s to concatenate the numbers and run it's toString() method. On a smaller scale of just 0 to 99, string takes 0.000337 while StringBuilder took 0.000060s. – Nathan M. Sep 23 '16 at 14:34
1

Try to increment a counter variable by 1 in each step and use an if statement to check the modulus (%). If it equals 0, then the number is even; if it equals 1, then the number is odd. For printing the separately, you can use two lists or arrays.

int count=50;
StringBuilder odds = new StringBuilder();
StringBuilder evens = new StringBuilder();

while(count<=100)
{
if(count%2==0) // here, the number is even, append this number to even string
   evens.append(count).append(",");

else // here, the number is odd, append this number to odds string
   odds.append(count).append(",");
count++;
}

// when everything is done, print the strings
// before doing that, you should remove the last comma at the end of these Strings. 
// Otherwise, you will have something like this: 93,95,97,

evens.setLength(evens.length() - 1); // to remove the comma at the end
odds.setLength(odds.length() - 1); // to remove the comma at the end

System.out.println("The even numbers are: " + evens.toString());
System.out.println("The odd numbers are: " + odds.toString());

It is much more efficient to use a StringBuilder rather than using += kind of appending for Strings. Take a look at this link:

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

codemirel
  • 160
  • 10
0

You can use single while loop like this for printing odd and even number.

int e = 50;
    while (e <= 98)
    {       
        if (e%2 == 0)
        {
            System.out.println("even :"+e);
        }
        else
        {
            System.out.println("odd :"+e);
        }
        e++;
    }
Dinesh Pun
  • 28
  • 6
0

Just create 2 string output variables and fill them in the loop based on condition

if (i%2==1)
  odd_output = odd_output + "," + i.toString 
else
  even_output = even_output + "," + i.toString 

(I might be wrong in Java syntax)

And then just print both resulting strings with any additional info you like

0

Here You don't need to maintain Any separate Arrays Or Strings, but a complex conditions

public class EvenOddTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int num = 50;
        System.out.println("Even numbers between 50 and 100: ");
        while (num < 100){
            if (num%2 == 0){
                System.out.print(num + ",");
            } else {
                System.out.print(num + ",");
            }
            if( num == 98 ) {
                num = 49;
                System.out.println();
                System.out.println("Odd numbers between 50 and 100: ");
            }
            num += 2;
        }
    }
}
Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42
0

I know it doesn't answer directly the question (although the output is the same) but in 2016 when one want to print a sequence of numbers we do something like that:

public static void main(String[] args) {
    int[] evenNumbers = IntStream.rangeClosed(50, 100).filter(i -> i % 2 == 0).toArray();
    int[] oddNumbers = IntStream.rangeClosed(50, 100).filter(i -> i % 2 != 0).toArray();

    System.out.println("Even numbers between 50 and 100: " + Arrays.toString(evenNumbers));
    System.out.println("Odd numbers between 50 and 100: " + Arrays.toString(oddNumbers));
}

If the goal of your exercice is to learn how to use a while loop, my solution is useless. However if your goal is to practice Java as it should be nowadays, I think if reaches the goal.

Spotted
  • 4,021
  • 17
  • 33