Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

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

Posted on October 5, 2019October 6, 2019

Table of Contents

  • Python program to display natural numbers from 1 to n
    • Python code to display natural numbers Using for loop
    • Python code to display natural numbers Using while loop
    • Python code to  print natural numbers Using function
    • Related

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

Pythgon program to display natural numbers from 1 to n
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

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes