In this post, we will see programs for how to print even natural number upto n in python, provided n is positive.
Even Number : Any integer either positive or negative, which can be divided exactly by 2 is an even number.The numbers having last digit is 0, 2, 4, 6 or 8 are considered as an even number.
Note : Natural number is positive integers. It’s range from 1 to infinity.
Examples:
Input:Â Â Â Â Â n : 10
Output:Â Â Â 2 Â Â 4 Â Â 6 Â Â 8Â Â 10
Input:Â Â Â Â n : 15
Output:Â Â Â 2Â Â 4Â Â 6Â Â 8Â Â 10Â Â 12Â Â 14
Let’s see the different version of codes for this program :
Code version 1: Using a concept of range() function with for loop.
# main code if __name__ == "__main__" : # input value of n n = 10 # for loop starts from 2 # and ends on n-2. # each time value of variable # i is incremented by 2 for i in range(2,n,2) : # print value of variable i # and end parameter is space # so print all outputs # in a single line print(i,end=" ")
Output: 2 4 6 8
Code version 2: Using a concept of pass statement with for loop.
# main code if __name__ == "__main__" : # input value of n n = 15 # for loop starts from 1 # and ends on n-1. # each time value of variable # i is incremented by 1 for i in range(1,n) : # if condition is True # that means i % 2 gives # non-zero value then # just ignore this block # else print i if i % 2 : pass # if condition is false # that means i % 2 return 0 else : # print value of variable i # and end parameter is space # so print all outputs # in a single line print(i,end=" ")
Output: 2 4 6 8 10 12 14
Code version 3: Using a concept of continue statement with for loop.
# main code if __name__ == "__main__" : # input value of n n = 20 # for loop starts from 1 # and ends on n-1. # each time value of variable # i is incremented by 1 for i in range(1,n) : # if condition is True # that means i % 2 gives # non-zero value then # just go to next iteration # else print i if i % 2 : continue # if condition is false # that means i % 2 return 0 else : # print value of variable i # and end parameter is space # so print all outputs # in a single line print(i,end=" ")
Output: 2 4 6 8 10 12 14 16 18
Code version 4: Using a concept of increment operator with while loop .
# main code if __name__ == "__main__" : # input value of n n = 20 # initialise value of i by 2 i = 2 # while loop starts from 1 # run untill value of i # is less than n while i < n : # print value of variable i # and end parameter is space # so print all outputs # in a single line print(i,end=" ") # each time value of variable # i is incremented by 2 i += 2
Output: 2 4 6 8 10 12 14 16 18
Code version 5: Using a concept of pass statement with while loop .
# main code if __name__ == "__main__" : # input value of n n = 20 # initialise value of i by 1 i = 1 # while loop starts from 1 # run untill value of i # is less than n while i < n : # if condition is True # that means i % 2 gives # non-zero value then # just ignore this block # else print i if i % 2 : pass # if condition is false # that means i % 2 return 0 else : # print value of variable i # and end parameter is space # so print all outputs # in a single line print(i,end=" ") # each time value of variable # i is incremented by 1 i += 1
Output: 2 4 6 8 10 12 14 16 18