School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,721 people online right now. Registration is fast and FREE... Join Now!




Write a program which asks the user for a number and generates the mul

 

Write a program which asks the user for a number and generates the mul

akash_9105

11 Oct, 2009 - 11:07 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 5

Write a program which asks the user for a number and generates the multiplication table for
this number.

e.g. If the user enters 5, the program should display:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
.
.
.
12 * 5 = 60

The program should use a while loop to generate the multiples.


This is what i have done so far:

CODE
n=input("enter a number ")
i = 0
mul=0.0
while i < 12:
    mul=i*n
    i=i+1
    print i," * ",n," = ",mul



here is what am getting.

CODE
>>>
enter a number 2
1  *  2  =  0
2  *  2  =  2
3  *  2  =  4
4  *  2  =  6
5  *  2  =  8
6  *  2  =  10
7  *  2  =  12
8  *  2  =  14
9  *  2  =  16
10  *  2  =  18
11  *  2  =  20
12  *  2  =  22
>>>


can anyone show me the correct way pf doing it?

User is offlineProfile CardPM
+Quote Post


programble

RE: Write A Program Which Asks The User For A Number And Generates The Mul

12 Oct, 2009 - 02:05 PM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Feb, 2009
Posts: 423



Thanked: 10 times
Dream Kudos: 50
My Contributions
CODE
n=input("enter a number ")
i = 0
mul=0.0
while i < 12:
    mul=i*n
    print i," * ",n," = ",mul
    i=i+1

User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Write A Program Which Asks The User For A Number And Generates The Mul

13 Oct, 2009 - 03:05 AM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(akash_9105 @ 11 Oct, 2009 - 07:07 PM) *

CODE
n=input("enter a number ")
i = 0
mul=0.0
while i < 12:
    mul=i*n
    i=i+1
    print i," * ",n," = ",mul


Think through what you're telling the computer to do:

The user enters 5 for n.

You start out with i being 0. You set mul to i * n, or 0 * 5, which is 0. Then you add 1 to i, making it 1. You then print out i (1) * n (5) = mul (0).

Next time through the loop, i is 1. mul is i (1) * n (5) = 5. Add 1 to i, making it 2. Print i (2) * n (5) = mul (5).

And so on...

Do you see what the problem is now?
User is offlineProfile CardPM
+Quote Post

programble

RE: Write A Program Which Asks The User For A Number And Generates The Mul

13 Oct, 2009 - 11:23 AM
Post #4

D.I.C Regular
Group Icon

Joined: 21 Feb, 2009
Posts: 423



Thanked: 10 times
Dream Kudos: 50
My Contributions
Much easier way to do this:
CODE
n = raw_input()
for i in range(12):
    print "%i * %i = %i" % (n, i, n*i)

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:56PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month