How do I square numbers?

A forum for general discussion of the Python programming language.

How do I square numbers?

Postby el_manu » Mon Aug 19, 2013 8:34 am

How can I write a program to calculating square numbers. The program should read a single line of input containing an integer, and print out the squares of all numbers up to and including that number.

Your program should work like this:

Example 1
Enter a number: 4
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16

Example 2
Enter a number: 6
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
Last edited by Mekire on Mon Aug 19, 2013 9:03 am, edited 1 time in total.
Reason: Title changed; post locked.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: How do I square numbers?

Postby Mekire » Mon Aug 19, 2013 9:07 am

You are going to need to post an attempt before you will receive much help. You need to get the input; loop through the numbers; and square and print each number.

Also please read this thread carefully before posting again and use code tags when you do post your attempt.

-Mek
User avatar
Mekire
 
Posts: 1143
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: How do I square numbers?

Postby Somelauw » Mon Aug 19, 2013 3:42 pm

Learn about the following functions and keywords by reading your favourite python tutorial:
Code: Select all
while
input
print


To calculate the square you can simply multiply the number with itself, but there is also a pow function.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 75
Joined: Tue Feb 12, 2013 8:30 pm

Re: How do I square numbers?

Postby el_manu » Tue Aug 20, 2013 9:02 am

Don't worry I did it:


Code: Select all
a = int(input("Enter a number: "))
for a in range(1, a+1):
   print(a, "squared is", a**2)


it was easy.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 10 guests