| | |
for loop
![]() |
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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?
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?
•
•
Join Date: Feb 2008
Posts: 8
Reputation:
Solved Threads: 0
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>
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>
Last edited by happygeek; Dec 5th, 2008 at 4:27 am. Reason: Email and URL snipped
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Feb 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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:
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)
Java Syntax (Toggle Plain Text)
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)
Last edited by javaAddict; Dec 5th, 2008 at 3:50 am.
I murdered thousands for the Emperor and he gave me nothing except his damning silence. Now his lapdogs yap for every life I take, while the gods promise me the galaxy.
- Svane Vulfbad -
- Svane Vulfbad -
![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: Urgently Explain Abstract And Interface Advantages and Disadvantages
- Next Thread: help in classes reference
Views: 1193 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for Java
access add android applet arguments array arrays binary bluetooth build c++ chat class classes client code combobox component constructor convert converter data database db design detection eclipse error event exception file forloop game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javafx jframe jpanel jtable jtextfield julia key lazy linked linked-list list loop looping main method methods mobile netbeans newbie number object objects os parameter pattern pixel printing problem program programming project read recursion remote remove return robot scanner screen server service set size sms sql string swing system test text text-file threads transfer translate tree user







