0% found this document useful (0 votes)
5 views6 pages

Python For Loop

The document contains a series of Python programs that demonstrate the use of for loops for various tasks, such as printing squares and cubes of numbers, finding sums and factorials, generating multiplication tables, and checking for prime numbers. Each program includes user input and outputs results based on the specified operations. The examples also illustrate how to manipulate lists and print patterns using loops.

Uploaded by

ponjit.borgohain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Python For Loop

The document contains a series of Python programs that demonstrate the use of for loops for various tasks, such as printing squares and cubes of numbers, finding sums and factorials, generating multiplication tables, and checking for prime numbers. Each program includes user input and outputs results based on the specified operations. The examples also illustrate how to manipulate lists and print patterns using loops.

Uploaded by

ponjit.borgohain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programs on for loop

1.​ Print square of numbers from 1 to N

n = int(input("Enter value of N: "))

for i in range(1, n + 1):


print("Square of", i, "=", i * i)

2.​ Print cube of numbers from 1 to N


n = int(input("Enter value of N: "))

for i in range(1, n + 1):


print("Cube of", i, "=", i * i * i)

3.​ Print numbers from 1 to N

n = int(input("Enter value of N: "))

for i in range(1, n + 1):


print(i)

4.​ Print even numbers between 1 and 100


for i in range(1, 101):
if i % 2 == 0:
print(i)

5.​ Print odd numbers between 1 and 100


for i in range(1, 101):
if i % 2 != 0:
print(i)

6.​ Find sum of first N natural numbers

n = int(input("Enter value of N: "))


total = 0

for i in range(1, n + 1):


total =total + i

print("Sum =", total)

7.​ Find factorial of a number

n = int(input("Enter a number: "))


fact = 1

for i in range(1, n + 1):


fact *= i

print("Factorial =", fact)

8.​ Generate multiplication table of a number

n = int(input("Enter a number: "))

for i in range(1, 11):


print(n, "x", i, "=", n * i)

9.​ Count digits in a number

num = input("Enter a number: ")


count = 0

for digit in num:


if [Link](): # ensures only digits are counted
count += 1

print("Total number of digits:", count)

10.​Check if a number is prime or not


num = int(input("Enter a number: "))

if num <= 1:
print("Not a Prime Number")
else:
for i in range(2, num):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")

11.​ Sum of First N even numbers

n = int(input("Enter value of N: "))


total = 0

for i in range(2, 2*n + 1, 2):


total += i

print("Sum of first", n, "even numbers =", total)

12.​Calculate average of list elements

numbers = [10, 20, 30, 40, 50]


total = 0

for num in numbers:


total += num

average = total / len(numbers)

print("Average =", average)


13.​ Search an element in a list

numbers = [10, 20, 30, 40, 50]


key = int(input("Enter element to search: "))

for num in numbers:


if num == key:
print("Element found")
break
else:
print("Element not found")

14.​Find common elements from two lists

list1 = [10, 20, 30, 40, 50]


list2 = [30, 40, 60, 70]

common = [ ]

for item in list1:


if item in list2:
[Link](item)

print("Common elements:", common)

15.​Print the following using for loop


*
**
***
****
*****

for i in range(1,6):
print("*" * i)

16.​Print the following using for loop


*****
****
***
**
*

for i in range(6, 0, -1):


print("*" * i)

17.​ Print the following using for loop


1
12
123
1234
12345

for i in range(1, 6):


for j in range(1, 6):
print(j, end="")
print()

18.​print odd numbers in a given range using for loop:


start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))

for i in range(start, end + 1):


if i % 2 != 0:
print(i)

19.​Print Prime Numbers in a Range


start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))

for num in range(start, end + 1):


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)

You might also like