for Loop in Python
- for loop -- Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
even and odd numbers
- An even number is a number that can be divisible by 2.
- Even numbers are 2, 4, 6, 8 and so on.
- An odd number is a number that cannot be divisible by 2.
- Odd numbers are 1, 3, 5, 7, 9 so on.
Input :
how many even and odd nums you want 5
Output :
first 5 even nums are
2 4 6 8 10
first 5 odd nums are
1 3 5 7 9
# Program to print first n even and odd numbers using for loop.
choice = int(input("how many even and odd nums you want "))
n=(choice*2)+1
print("first",choice,"even nums are ")
for i in range(1,n):
if i%2 == 0:
print(i,end=" ")
print("\nfirst",choice,"odd nums are ")
for i in range(1,n):
if i%2!=0:
print(i,end=" ")
Output :
first 5 even nums are
2 4 6 8 10
first 5 odd nums are
1 3 5 7 9
Comments
Post a Comment
Type Your Comments !
Please do not enter any spam link in the comment box.