Print N Numbers In Python Using While Loop & For Loop

Print N Numbers In Python Using While Loop & For Loop | In this article, we will discuss how to print N numbers in Python using while loop, and how to print N numbers in Python using for loop.

Let us see how to print N numbers in Python using while loop. Firstly, take the input from the user by using the python input() function. And then, iterate while looping with the user input number. Then increase the while loop iteration value by 1, as well as the print iteration value.

Program to Print N Numbers In Python Using While Loop

n = int(input("Enter any number: "))
i = 1
print("The list of natural numbers from 1 to ", n)
while (i <= n):
    print(i, end='  ')
    i = i + 1

Output for different input values:-

Enter any number: 5
The list of natural numbers from 1 to 5
1 2 3 4 5

Enter any number: 20
The list of natural numbers from 1 to 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Enter any number: 99
The list of natural numbers from 1 to 99
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

Now let us see how to print n numbers in python using for loop. The for loop is very similar to the while loop. Both are used for iteration.

Print N Numbers In Python Using For Loop

n = int(input("Enter any number: "))
i = 1
print("The list of natural numbers from 1 to", n)
for i in range(1, n + 1):
    print(i, end=' ')
print()

Output for different input values:-

Enter any number: 9
The list of natural numbers from 1 to 9
1 2 3 4 5 6 7 8 9

Enter any number: -5
The list of natural numbers from 1 to -5

Enter any number: 50
The list of natural numbers from 1 to 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

So, here we have learned how to print n numbers in python using for loop and while loop using Python Programming Language. Hope you all find this article useful.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *