Python program to display square of first five natural numbers
Answers
Answer:
# Python 3 program to print squares of first
# 'n' natural numbers without using *, / and -
def printSquares(n):
# Initialize 'square' and previous
# value of 'x'
square = 0; prev_x = 0;
# Calculate and print squares
for x in range(0, n):
# Update value of square using
# previous value
square = (square + x + prev_x)
# Print square and update prev
# for next iteration
print(square, end = " ")
prev_x = x
n = 5;
printSquares(n);
program to display square of first five natural numbers
Output:
Square of first five natural numbers :
1 = 1
2 = 4
3 = 9
4 = 16
5 = 25
Explanation:
Python Program to display square of first five natural numbers can be given as follows:
Program:
square=0 #defining variable
print ('Square of first five natural numbers :') #message
for i in range(1,6) : #loop for calculate square
square = (i * i) #holds square value
print (i ,'=',square) #print square
Following are the description of the program.
- In the above python program, a square variable is defined, this variable holds a value, that is 0.
- In the next line, a loop is defined, that is used to count natural numbers, and inside a loop, a square variable is used, that calculates the square of natural numbers.
- The end of the loop print function is used that prints square variable value.
Learn More:
- Program to calculate natural number using python: https://brainly.in/question/5551459
- Natural number: https://brainly.in/question/3512178