Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,490 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,168 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Multiple table (table 2 to 5) generation problem.

 
Reply to this topicStart new topic

Multiple table (table 2 to 5) generation problem., Help my guyz

rockey
post 21 Jun, 2008 - 08:48 AM
Post #1


D.I.C Head

**
Joined: 2 Jan, 2008
Posts: 57


My Contributions


Hello everyone,

I want to create tables (table 2 to table 5) using while loop, without nested loop.

the output of my program is:

CODE

2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18
3x0=0
3x0=0
3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21


it's displaying extra value (3x0 = 0) i dont know why it's happening. Now i am tired. I don't know what's is the problem is in my coding. So help my guyes.

CODE

<HTML>
<BODY>
<script language = "JavaScript">
d = 2;    //Table 2 to 5
n = 1;

while(n<=10, d<=5)
{
document.write(d+ "x" + n + "=" + d*n + "<BR>")
n++;

if (n == 10)
{
d++;
n = 0;
document.write(d+ "x" + n + "=" + d*n + "<BR>")
}
}

</SCRIPT>
</BODY>
</HTML>


This post has been edited by rockey: 21 Jun, 2008 - 08:52 AM
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 21 Jun, 2008 - 10:13 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,581



Thanked 44 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(rockey @ 21 Jun, 2008 - 11:48 AM) *

I want to create tables (table 2 to table 5) using while loop, without nested loop.


Why? Do you not like yourself? wink2.gif

The problem is this:

CODE

document.write(d+ "x" + n + "=" + d*n + "<BR>")
n++;
//...
n = 0;
// you print a 0 here.  When the loop goes back up, you'll get this exact same thing again, before you hit n++
document.write(d+ "x" + n + "=" + d*n + "<BR>")


Remove that second write and it should work fine. I don't know if you want the zeros, either, considering you start at 1. Also, I'm not sure how this:while(n<=10, d<=5) even works. You usually just have a single condition in a while loop.

Here's how I'd write what you're trying to do:

CODE

d = 2;    //Table 2 to 5
n = 1;
while(d<6) {
    document.write(d+ "x" + n + "=" + d*n + "<BR>")
    if (++n == 10) {
        n = 1;
        d++;
    }
}



Hope this helps.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 02:05PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month