Alright, so for the output:

0 2 4 6 8 10
1 3 5 7 9 11
3 6 9 12 15 18
6 10 14 18 22 26
... (the pattern presented goes on forever)

a code must be written to create it. I know I need a for loop, or a loop of some kind.
But, I don't even know where to begin with this!

Recommended Answers

All 16 Replies

You will need nested loops. The inner loop will print one line of numbers, while the outer loop controls how the pattern changes for the inner loop.

If you're still stuck, try solving the problem in steps. First create a loop that will print just one line of the pattern. Then make it repeat that same line many times. Then finally make the pattern change from line to line.

Also - should the first line not be 0 1 2 3 4 5?

public class OddNum{
public static void main(String args[])
{
for(int a=0;a<11;a=a+2)
System.out.println(a);
}
}
...
OutPut
0 2 4 6 8 10

second program to print
1 3 5 7 9

public class EvenNum{
public static void main(String args[])
{
for(int a=1;a<10;a=a+2)
System.out.println(a);
}
}

Output:
1 3 5 7 9

third program to print
3 6 9 12 15 18

public class Third{
public static void main(String args[])
{
for(int a=3;a<20;a=a+3)
System.out.println(a);
}
}

output:
3 6 9 12 15 18

fourth program,
6 10 14 18 22 26

public class ForthProg{
public static void main(String args[])
{
for(int a=6;a<27;a=a+4)
System.out.println(a);
}
}

OutPut
6 10 14 18 22 26

these Programs are TESTED and 100% Working Without Errors

Regards
Umair Sario
<SNIP>

commented: Hopefully this person gets hired as your coworker so you can continue to do his work, as I definately wouldn't want his ignorance at my work place after he cheated his way to a degree. -2

these Programs are TESTED and 100% Working Without Errors

I think his problem is by putting them all together in one class, and determining the value to add each time.

I think his problem is by putting them all together in one class, and determining the value to add each time.

hmmm in one program its also easy, he can do it also, if he know java ! well let me also post all these program into 1 class code ..

if he know java

that is the main issue, yes :)
best is to let him try to do it himself

hmmm in one program its also easy, he can do it also, if he know java ! well let me also post all these program into 1 class code ..

No, don't. Read the terms and conditions of this forum again (which you agreed to when you signed up here), as you've already violated them once, with your first post, don't do it a second time with just your third.

public class GNumProg{
public static void main(String args[])
{
for(int a=0;a<11;a=a+2)
{
System.out.println(a);}
for(int a=1;a<10;a=a+2)
{
System.out.println(a);}
for(int a=3;a<20;a=a+3)
{
System.out.println(a);}

for(int a=6;a<27;a=a+4)
{
System.out.println(a);}

}
}

..
this is also the simplest way to print series mentioned above ...
there are alot of ways to print but this one is simplest

Actually what you wrote umairsario is wrong, and other good programmers will agree with me. Actually I dare to call it stupid. If I was thinking your way I good make a better (and more stupid) program:

System.out.println("0 1 2 3 4 5");
System.out.println("1 3 5 7 9");
System.out.println("3 6 9 12 15 18");
System.out.println("6 10 14 18 22 26");

The idea is to use 2 for-loops and everything would be dynamically, so you could have N lines and M columns.

The pattern to be followed is this:

The difference of the elements of two rows increases by 1
0 (0 + 0) (row = 1 index of for loop is 0)
1 (0 + 1) (row = 2 index of for loop is 1)
3 (1 + 2) (row = 3 index of for loop is 2)
6 (3 + 3) (row = 4 index of for loop is 3)
10 (6 + 4) (row = 5 index of for loop is 4)
15 (10 + 5) (row = 6 index of for loop is 5)

And for the columns each element is added the row num:
First row: 0 1(0+1) 2(1+1) 3(2+1) 4(3+1)
Second row: 1 3(1+2) 5(3+2) 7(5+2) 9(7+2)

ya but i m new Programmer to Java :d
well sir can you please explain Abstract and Interface i m bit confused and unable to understand these both

commented: Poor code suggestions and unrelated questions all in one thread? -3

public class GNumProg{
public static void main(String args[])
{
for(int a=0;a<11;a=a+2)
{
System.out.println(a);}
for(int a=1;a<10;a=a+2)
{
System.out.println(a);}
for(int a=3;a<20;a=a+3)
{
System.out.println(a);}

for(int a=6;a<27;a=a+4)
{
System.out.println(a);}

}
}

..
this is also the simplest way to print series mentioned above ...
there are alot of ways to print but this one is simplest

The original post says:

.. (the pattern presented goes on forever)

How do you accomplish that?

ya but i m new Programmer to Java :d
well sir can you please explain Abstract and Interface i m bit confused and unable to understand these both

If you are new don't post wrong answers if you don't understand the question.
Also start a new Thread because what you ask is irrelevant with this post. This one is not solved. How can we possibly post 2 different answers in the same post. Should we ignore the creator of this post to answer your question?

System.out.println("0 1 2 3 4 5");
System.out.println("1 3 5 7 9");
System.out.println("3 6 9 12 15 18");
System.out.println("6 10 14 18 22 26");

but this would be wrong too :)
if I read correct in the first post, the first line has an augmentation of 2 as well. he needs to use two for loops, and check that the row number is below 2, then he has to use 2 as augmentation, otherwise the row number

Also - should the first line not be 0 1 2 3 4 5?

I pressumed that the answer was Yes

@umairsario
First you posted entire code which should not be done as this place does not sanction that. Then even after you were told you posted entire code (changed) a second time violating the T&C a second time. Third you are not even guiding someone the correct way by posting such nonsense code. (The pattern should be printed using nested for loops) as javaAddict rightly points out. Fourth you are trying to squeeze in your question, which has been asked in another thread on this same forum, in one of your posts here.
Are you trying to make it to the guiness records of "number of wrong things done in one post" or something like that ?

My original post was correct. What the first line read is what the output is supposed to be. And for the "the pattern continue's forever", I was told that as long as there was a pattern, it didn't matter.
line 1: up by 2
lined 2: up by 2
line 3: up by 3
line 4: up by 4
5: up by 5
etc.

public void loop4()
{
int counter = -1;
int increment = 2;
System.out.println("0 2 4 6 8 10");
while(increment {
for(int i = 0; i {
System.out.print(counter+increment + " ");

counter = counter+increment;
}
counter = increment-2;
increment++;
System.out.println();

}

}

I believe that is a correct answer? But I'm not sure, because I can't test it in BlueJ without creating a class.

in which case: you create a class. you'll need to test it sooner or later, or you'll end up handing in a lot of incorrect, not working code

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.