for loop

Reply

Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

for loop

 
0
  #1
Dec 4th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: for loop

 
0
  #2
Dec 5th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 8
Reputation: umairsario has a little shameless behaviour in the past 
Solved Threads: 0
umairsario umairsario is offline Offline
Newbie Poster

Re: for loop

 
-1
  #3
Dec 5th, 2008
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>
Last edited by happygeek; Dec 5th, 2008 at 4:27 am. Reason: Email and URL snipped
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 807
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 100
stultuske's Avatar
stultuske stultuske is offline Offline
Practically a Posting Shark

Re: for loop

 
0
  #4
Dec 5th, 2008
Originally Posted by umairsario View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 8
Reputation: umairsario has a little shameless behaviour in the past 
Solved Threads: 0
umairsario umairsario is offline Offline
Newbie Poster

Re: for loop

 
0
  #5
Dec 5th, 2008
Originally Posted by stultuske View Post
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 ..
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,639
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 283
Moderator
masijade's Avatar
masijade masijade is offline Offline
Posting Maven

Re: for loop

 
0
  #6
Dec 5th, 2008
Originally Posted by umairsario View Post
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.
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 807
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 100
stultuske's Avatar
stultuske stultuske is offline Offline
Practically a Posting Shark

Re: for loop

 
0
  #7
Dec 5th, 2008
Originally Posted by umairsario View Post
if he know java
that is the main issue, yes
best is to let him try to do it himself
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 8
Reputation: umairsario has a little shameless behaviour in the past 
Solved Threads: 0
umairsario umairsario is offline Offline
Newbie Poster

Re: for loop

 
0
  #8
Dec 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2,108
Reputation: javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold 
Solved Threads: 265
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Postaholic

Re: for loop

 
0
  #9
Dec 5th, 2008
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:

  1. System.out.println("0 1 2 3 4 5");
  2. System.out.println("1 3 5 7 9");
  3. System.out.println("3 6 9 12 15 18");
  4. 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 -
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 8
Reputation: umairsario has a little shameless behaviour in the past 
Solved Threads: 0
umairsario umairsario is offline Offline
Newbie Poster

Re: for loop

 
-1
  #10
Dec 5th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1193 | Replies: 16
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC