简体   繁体   中英

Displaying all the Odd number between 1 - 99 in java using Nested For Loop

Currently I'm only able to display odd numbers of 1 3 5 7 9. However, I would like to display all the odd numbers from 1 - 99 with 9 rows and 5 col. May I know how am I able to display from 11 onwards rather than just 9 rows of 1 3 5 7 9.

Below is the code I'm stuck with.

public static void main(String args[])
{
    for (int i=1; i<=9; i++)
    {
        for (int j=1; j<=10; j++)
        {
            if (j%2 !=0)
            System.out.print(j + " " );
        }
        System.out.println();
    }
}

first you need to calculate your number, try

for (int i=0; i<=9; i++)
    {
        for (int j=1; j<=10; j++)
        {
            int number = j+i*10
            if (number%2 !=0)
            System.out.print(number + " " );
        }
        System.out.println();
    }

but this problem you can solve with single loop

 for (int i=1; i<=99; i++)
 {
      if (number%2 !=0)
          System.out.print(number + " " );

      if (number%10 ==0)
          System.out.println();
 }
for ( i = 1; i < 100; i+=2 ) {
    System.out.print(i);
}
System.out.println();

Maybe this helps:

public static void main(String args[])
{
    for (int j = 0; j < 10; j++)
    {
        for (int i = 1; i <= 9; i++)
        {
            int c = j * 10 + i;
            if (c % 2 !=0)
                System.out.print(c + " " );
        }
        System.out.println();
    }
}

Other alternative with just one loop:

public static void main(String args[]) {
    for (int j = 1; j <= 99; j += 2) {
        System.out.print(j + " ");
        if ((j + 1) % 10 == 0) {
            System.out.println();
        }
    }
}

Your code now prints 9 rows of:

1 3 5 7 9 

This is happening because your inner loop loops on values between 1 and 9, always.

You should try something like:

int counter = 0;
for(int i=1; i<=99; i++) {
    if(i%2 != 0) {
        System.out.print(i + " ");
        counter++;
    }
    if(counter == 5) {
        System.out.println();
        counter = 0;
    }
}

This will print:

1 3 5 7 9 
11 13 15 17 19 
21 23 25 27 29 
31 33 35 37 39 
41 43 45 47 49 
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 
public static void main(String args[])
{
    for (int i=1; i<=99; i++)
    {
        if (i%2 !=0)
        System.out.print(i + " " );
        if(i%10 == 0)
        System.out.println();
    }
}

This code will allow you to do what you want with 1 loop which is more efficient than using nested loops.

This will loop through each number from 1-99 and print if it is odd. If the number is a multiple of 10 then it will print a new line.

If you want to use a nested forloop you have to use both iterating variables to build your numbers. You dont use i at all in the inner for-loop. Therefore only 1 3 5 7 9 gets printed 9 times. For a hotfix try to use something like

 System.out.print(i + j + " " ); 
instead of

 System.out.print(j + " " ); 
note how i+j does not compute an addition here. Anyway like pointed out in the comments you dont really need 2 loops here.

we cheat:

for (int i = 1; i <= 99; i += 2)
    System.out.printf("%d%s", i, i % 10 == 9 ? "\n" : " ");

we check:

int c =0;
for (int i = 1; i <= 99; i++)
    if ((i & 1) == 1) {
        c++;
        System.out.printf("%d%s", i, c % 5==0 ? "\n" : " ");
    }

both output same, 5 odd numbers in a row, without trailing spaces.

With this one you can easily set the number of columns as you like it.

We use a single loop.

Code:

int columns = 5;
int start   = 1;
int end     = 99;

// iterate through every seconds i.
for (int i = start; i <= end; i += 2) {
    System.out.printf("%-4d", i);

    // if we have displayed enough words, start a new line
    if (i % (2 * columns) == 0) {
        System.out.println();
    }
}

Output:

1   3   5   7   9   
11  13  15  17  19  
21  23  25  27  29  
31  33  35  37  39  
41  43  45  47  49  
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

However, if I understood your question correctly, you wanted to show numbers with 5 columns and 9 rows? Well this is impossible if we print only the numbers from 1 to 99. With 5 columns and numbers 1 to 99, you will get 10 rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM