Write a program to print first n numbers in python

Doing with FOR loop

How to do it?

  • Generate a range from 1 to n
  • Print each element
# Program to print all numbers stored in a list

n=100
# iterate over the list
for val in range(1,n):
	print(val)
	

When you run the program, the output will be:

1
2
3
.
.
100

Using WHILE loop

How to do it?

  • Concept is Simple Counting
  • x=f(x), use a loop
  • N=N+1
  • Derive the next number by adding 1 to previous number
num=1
last=100
while (num <=last):
        print 'The number is:', num
        num= num+ 1

When you run the program, the output will be:

1
2
3
.
.
100

Leave a Reply

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