Write a Java application to print out the numbers 10 through 49 in the following manner:

10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49

Try to develop the program code so that it can be extended easily to handle any range of values. You can do this modification in two ways: with a nested-for statement or with modular arithmetic. (Modulo arithmetic is using the remainder of a division calculation to restrict a variable to a given range of numbers based on some counter variable. For example, if you have a variable that is increased by 1 in a loop and use the modulus operator ( % ) to calculate the remainder of this variable with a value of 10, then as the variable’s value increases the modulus operator will return a value of between 0 and 9:

i i % 10
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 0
11 1
12 2
13 3
14 4
15 5
16 6
17 7
18 8
19 9
20 0
21 1

Recommended Answers

All 4 Replies

This is the answer to your first problem:

public static void main(String[] args) {

    int num = 10;
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=10; j++)
            {
                System.out.print(num + " ");
                num++;
            }

        System.out.println("");
    }
}

Let me give another time for the second one. Enjoy! ^_^

Here's the code for the second problem. Contact me if you have any question. ^_^

package sampleprogram;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter start number: ");
        int input1 = sc.nextInt();
        System.out.println("Enter end row: ");
        int input2 = sc.nextInt();

        for(int i=1; i<=input2; i++)
        {
            for(int j=0; j<10; j++)
                {
                    System.out.print(input1 + " ");
                    input1++;
                }
            System.out.println("");
        }
    }
}
commented: boo! quit feeding the parasites. -1

jeth88 you just made a mistake, by providing this code. Our friend king_786 posted another few exercises and is expecting quick solutions. Meaning he intend to only collect solution and submit them as is own, which is WRONG!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.