Categories: Uncategorized

Python program to display natural numbers from 1 to n | 3 different ways

Python program to display natural numbers from 1 to n

In this tutorial, we will discuss the Python program to display natural numbers from 1 to n through different 3 ways

Display Natural numbers

In this post, we are going to learn how to print natural number from 1 to entered number in different 3 ways

Python code to display natural numbers Using for loop

This program allows the user to enter a maximum number. and then, it displays natural numbers from 1 to given number using for loop in Python language

Program 1

#print 1 to n natural number
#using for loop
num=int(input("Please enter maxinmum number: "))
print("Natural numbers 1 to ",num )
for i in range(1,num+1):
     print i,

When the above code is executed it produces the following output

Please enter maxinmum number: 12
Natural numbers 1 to 12
1 2 3 4 5 6 7 8 9 10 11 12

Python code to display natural numbers Using while loop

This program allows the user to enter a maximum number. and then,it displays natural numbers from 1 to given number using while loop in Python language

Program 2

#Python program to print natural number fro 1 to n
#using while loop
num=int(input("Enter maximum Natural number: "))
i=1;
while i<=num:
    print(i),
    i=i+1

When the above code is executed it produces the following output

Enter maximum Natural number: 12
1 2 3 4 5 6 7 8 9 10 11 12

 

Python code to  print natural numbers Using function

This program allows the user to enter a maximum number. and then, it displays natural numbers from 1 to given number using the function in Python language

Program 3

#print 1 to n natural number
#using function
num=int(input("Please enter maxinmum number: "))
print("Natural numbers 1 to ",num )

def naturalNum(num):
   for i in range(1,num+1):
     print i,

naturalNum(num)

When the above code is executed it produces the following output

Please enter maxinmum number: 15
Natural numbers 1 to 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Similar post

C++ program to print natural numbers from 1 to n

C program to print natural numbers from 1 to n

Java program to  print natural numbers from 1 to n

 

Suggested for you

For loop in Pthon language

While loop in Python language

Function in Python

Data type in Python language

Operator in Python language

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.