Physics, asked by masifakrami, 1 day ago

to print first 20 natural numbers (python)

Answers

Answered by ritimaurya242
1

Explanation:

Python Program to Print Natural Numbers

Write a Python Program to Print Natural Numbers using While Loop and For Loop with an example.

Python Program to Print Natural Numbers using For Loop

This Python program for natural numbers allows users to enter any integer value. Next, this program prints natural numbers from 1 to user-specified value using For Loop.

# Python Program to Print Natural Numbers from 1 to N

number = int(input("Please Enter any Number: "))

print("The List of Natural Numbers from 1 to {0} are".format(number))

for i in range(1, number + 1):

print (i, end = ' ')

Python natural numbers output

Please Enter any Number: 10

The List of Natural Numbers from 1 to 10 are

1 2 3 4 5 6 7 8 9 10

Python Natural Numbers Program using While Loop

In this Python Program to display Natural Numbers, we just replaced the For Loop with While Loop

# Python Program to Print Natural Numbers from 1 to N

number = int(input("Please Enter any Number: "))

i = 1

print("The List of Natural Numbers from 1 to {0} are".format(number))

while ( i <= number):

print (i, end = ' ')

i = i + 1

Python natural numbers using while loop output

Python Program to display Natural Numbers within a range

This Python program for natural numbers is the same as the first example. But this time, we are allowing the user to enter the minimum and maximum values. It means this program prints natural numbers from minimum to maximum.

# Python Program to Print Natural Numbers within a range

minimum = int(input("Please Enter the Minimum integer Value : "))

maximum = int(input("Please Enter the Maximum integer Value : "))

print("The List of Natural Numbers from {0} to {1} are".format(minimum, maximum))

for i in range(minimum, maximum + 1):

print (i, end = ' ')

Attachments:
Similar questions