1. Print the first n natural numbers using for loop.
print(“the first n natural numbers using for loop”)
n = int(input(“Enter Any Number: “))
for i in range(1,n):
print(i)
2. Python program to print a multiplication table of a given number
# if the given range is 10
given_number = int(input(“Give A Number: “)
for i in range(11):
print (given_number," x",i," =",5*i)
3. Python program to print all the even and odd numbers within the given range.
print(“To print all the even numbers within the given range”)
given_range = int(input(“Give A Number: “)
for i in range(given_range):
# if number is divisble by 2 , then it's even
if i%2==0:
# if above condition is true, print the number
print(i,”is Even Number)
else:
print(i,”is Odd Number)
4. Python program to calculate the sum of all numbers from 1 to a given number.
# to calculate the sum of all numbers from 1 to a given number
print(“To calculate the sum of all numbers from 1 to a given number”)
given_number = int(input(“Give A Number: “)
# set up a variable to store the sum with initial value of 0
sum = 0
# since we want to include the number 10 in the sum increment given number by 1 in the for loop
for i in range(1,given_number+1):
sum+=i
# print the total sum at the end
print(sum)
5. Python program to calculate the sum of all the odd numbers within the given range.
# to calculate the sum of all the odd numbers within the given range
print(“To calculate the sum of all the odd numbers within the given range”)
given_range = int(input(“Give A Number: “)
# set up a variable to store the sum with initial value of 0
sum = 0
for i in range(given_range):
# if i is odd, add it to the sum variable
if i%2!=0:
sum+=i
# print the total sum at the end
print(sum)
6. Find the factorial of a given number
print(“To find the factorial of a Given Number”)
num = int(input(“Give of a Number: “)
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
# run loop 5 times
for i in range(1, num + 1):
# multiply factorial by current number
factorial = factorial * i
print("The factorial of", num, "is", factorial)
7. Python program to get the Fibonacci series between 0 to 50.
number = int(input("Enter the Series Range: "))
Number_1 = 0
Number_2 = 1
for number in range(0, number):
if(number <= 1):
value = number
else:
value = Number_1 + Number_2
Number_1 = Number_2
Number_2 = value
print(value,end=' ')
8. Python program to print all prime numbers in a given interval (use break)
start = int(input(“Give starting Number: “)
end = int(input(“Give last Number: “)
print("Prime numbers between", start, "and", end, "are:")
for num in range(start, end + 1):
# all prime numbers are greater than 1
# if number is less than or equal to 1, it is not prime
if num > 1:
for i in range(2, num):
# check for factors
if (num % i) == 0:
# not a prime number so break inner loop and
# look for next number
break
else:
print(num)
9. Print the below triangle using for loop.
5
44
333
2222
11111
num=int(input("Enter the Number of Rows"))
for i in range(1,num+1):
for j in range(1,i+1):
print(num,end=' ')
num=num-1
print()
10. Demonstrate the following control transfer statements in Python with suitable examples.
i) break ii) continue iii) pass
i) break
#Use break statement inside loops to break loop execution based on some condition
for i in range(10):
if i==7:
print("processing is enough..plz break")
break
print(i)
ii) con nue
# Use continue statement to skip current iteration and continue next iteration.
number=int(input("Enter a Number: "))
for i in range(number):
if (i>7 and i<12):
continue
print(i)
iii) pass
for i in range(100):
if i%9==0:
print(i)
else:
pass