2

There's this problem from my programming class that I can't get right... The output must be all odd numbers, in odd amounts per line, until the amount of numbers per line meets the odd number that was entered as the input. Example:

input: 5 correct output:

1
3 5 7
9 11 13 15 17

If the number entered is even or negative, then the user should enter a different number. This is what I have so far:

public static void firstNum() {
    Scanner kb = new Scanner(System.in);
    int num = kb.nextInt();
    if (num % 2 == 0 || num < 0)
        firstNum();
    if (num % 2 == 1)
        for (int i = 0; i < num; i++) {
            int odd = 1;
            String a = "";
            for (int j = 1; j <= num; j++) {
                a = odd + " ";
                odd += 2;
                System.out.print(a);
            }
            System.out.println();
        }
}

public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    firstNum();
}

The output I'm getting for input(3) is

1 3 5 
1 3 5 
1 3 5 

When it really should be

1
3 5 7

Can anyone help me?

EddieTheEd
  • 31
  • 1
  • 4

5 Answers5

1

Try this:

public static void firstNum() {
    Scanner kb = new Scanner(System.in);
    int num = kb.nextInt();
    while (num % 2 == 0 || num < 0) {
        num = kb.nextInt();
    }
    int odd = 1;
    for (int i = 1; i <= num; i += 2) {
        String a = "";
        for (int j = 1; j <= i; j++) {
            a = odd + " ";
            odd += 2;
            System.out.print(a);
        }
        System.out.println();
    }
}
Hans Brende
  • 7,847
  • 4
  • 37
  • 44
0
if (num % 2 == 1) {
    int odd = 1;
}

for (int i = 0; i < num; i++) {
    String a = "";

    for (int j = 1; j <= odd; j++) {
        a = odd + " ";
        odd += 2;
        System.out.print(a);
    }

    System.out.println();
}

You should assign odd before for loop. In inner for loop compare j and odd together.

robyaw
  • 2,274
  • 2
  • 22
  • 29
klaymen
  • 237
  • 2
  • 10
0

Seems I am bit late to post, here is my solution:

public static void firstNum() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the odd number: ");
    int num = scanner.nextInt();
    if (num % 2 == 0 || num < 0) {
        firstNum();
    }
    if (num % 2 == 1) {
        int disNum = 1;
        for (int i = 1; i <= num; i += 2) {
            for (int k = 1; k <= i; k++, disNum += 2) {
                System.out.print(disNum + " ");
            }
            System.out.println();
        }
    }
}
SyntaX
  • 2,090
  • 17
  • 30
0

For questions like this, usually there is no need to use and conditional statements. Your school probably do not want you to use String as well. You can control everything within a pair of loops.

This is my solution:

int size = 7; // size is taken from user's input
int val = 1;
int row = (size / 2) + 1;

for (int x = 0; x <= row; x++) {
    for (int y = 0; y < (x * 2) + 1; y++) {
        System.out.print(val + " ");
        val += 2;
    }
    System.out.println("");
}

I left out the part where you need to check whether input is odd.

How I derive my codes:

  1. Observe a pattern in the desired output. It consists of rows and columns. You can easily form the printout by just using 2 loops.
  2. Use the outer loop to control the number of rows. Inner loop to control number of columns to be printed in each row.
  3. The input number is actually the size of the base of your triangle. We can use that to get number of rows. That gives us: int row = (size/2)+1;
  4. The tricky part is the number of columns to be printed per row.
  • 1st row -> print 1 column
  • 2nd row -> print 3 columns
  • 3rd row -> print 5 columns
  • 4th row -> print 7 columns and so on

We observe that the relation between row and column is actually: column = (row * 2) + 1

Hence, we have: y<(x*2)+1 as a control for the inner loop.

  1. Only odd number is to be printed, so we start at val 1 and increase val be 2 each time to ensure only odd numbers are printed. (val += 2;)

Test Run:

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 
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • **Remarks:** I left out the codes to check whether input number is odd for clarity of codes and I think you have no issues for that part, so it has been left out. – user3437460 Nov 19 '15 at 06:30
0

You can use two nested loops (or streams) as follows: an outer loop through rows with an odd number of elements and an inner loop through the elements of these rows. The internal action is to sequentially print and increase one value.

  1. a loop in a loop
    int n = 9;
    int val = 1;
    // iterate over the rows with an odd
    // number of elements: 1, 3, 5...
    for (int i = 1; i <= n; i += 2) {
        // iterate over the elements of the row
        for (int j = 0; j < i; j++) {
            // print the current value
            System.out.print(val + " ");
            // and increase it
            val += 2;
        }
        // new line
        System.out.println();
    }
    
  2. a stream in a stream
    int n = 9;
    AtomicInteger val = new AtomicInteger(1);
    // iterate over the rows with an odd
    // number of elements: 1, 3, 5...
    IntStream.iterate(1, i -> i <= n, i -> i + 2)
            // iterate over the elements of the row
            .peek(i -> IntStream.range(0, i)
                    // print the current value and increase it
                    .forEach(j -> System.out.print(val.getAndAdd(2) + " ")))
            // new line
            .forEach(i -> 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 

See also: How do I create a matrix with user-defined dimensions and populate it with increasing values?