0% found this document useful (0 votes)
235 views60 pages

GE17151 PSPP Unit II Programs PDF

The document contains Python programs to perform basic calculations and conditional logic checks. The programs include adding two numbers, finding data types, swapping variables, calculating quotient and remainder, simple interest, area and circumference of a circle, temperature conversions, average, cutoff marks, even/odd checks, eligibility checks, grade calculations, and checking for leap years. The programs demonstrate the use of basic Python constructs like variables, input, print, if/else statements.

Uploaded by

Sem1
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)
235 views60 pages

GE17151 PSPP Unit II Programs PDF

The document contains Python programs to perform basic calculations and conditional logic checks. The programs include adding two numbers, finding data types, swapping variables, calculating quotient and remainder, simple interest, area and circumference of a circle, temperature conversions, average, cutoff marks, even/odd checks, eligibility checks, grade calculations, and checking for leap years. The programs demonstrate the use of basic Python constructs like variables, input, print, if/else statements.

Uploaded by

Sem1
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
You are on page 1/ 60

2 B.

BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to add the two integers 10 and 20.

Program:

firstno = 10
secondno = 20
result = firstno + secondno
print("Result =", result)

Output:

Result = 30

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3


Develop a Python program to find the type of various values.

Program:

a = 10
b = 3.14
c = "REC"
d = True
print("Type of a is", type(a))
print("Type of b is", type(b))
print("Type of c is", type(c))
print("Type of d is", type(d))

Output:

Type of a is <class 'int'>


Type of b is <class 'float'>
Type of c is <class 'str'>
Type of d is <class 'bool'>

4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to swap two numbers (using three variables).

Program:

a = int(input("Enter the first number : "))


b = int(input("Enter the second number : "))
print("Before swapping :")
print("a =", a, "b =",b)
c = a
a = b
b = c
print("After swapping :")
print("a =", a, "b =",b)

Output:

Enter the first number : 10


Enter the second number : 20
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5


Develop a Python program to swap two numbers (using two variables).

Program:

a = int(input("Enter the first number : "))


b = int(input("Enter the second number : "))
print("Before swapping :")
print("a =", a, "b =",b)
a = a + b
b = a - b
a = a - b
print("After swapping :")
print("a =", a, "b =",b)

Output:

Enter the first number : 10


Enter the second number : 20
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to read numerator and denominator and print
their quotient and remainder.

Program:

n = int(input("Enter the numerator : "))


d = int(input("Enter the denominator : "))
q = n // d
r = n % d
print("Quotient =", q, "Remainder =", r)

Output:

Enter the numerator : 10


Enter the denominator : 3
Quotient = 3 Remainder = 1

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7


Develop a Python program to compute the simple interest.

Program:

p = int(input("Enter the principle amount : "))


n = int(input("Enter the number of years : "))
r = float(input("Enter the rate of interest : "))
si = (p * n * r) / 100
print("Simple interest =", si)

Output:

Enter the principle amount : 10000


Enter the number of years : 5
Enter the rate of interest : 12.00
Simple interest = 6000.0

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the area and circumference of a circle.

Program:

r = float(input("Enter the radius : "))


area = 3.14 * r * r
circum = 2 * 3.14 * r
print("Area = :", area)
print("Circumference = :", circum)

Output:

Enter the radius : 3


Area = : 28.259999999999998
Circumference = : 18.84

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 9


Develop a Python program to convert the °C to °F.

Program:

c = float(input("Enter the centigrade : "))


f = (1.8 * c + 32)
print("The fahrenheit is =", f)

Output:

Enter the centigrade : 45


The fahrenheit is = 113.0

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to convert the °F to °C.

Program:

f = float(input("Enter the fahrenheit : "))


c = (f - 32) / 1.8
print("The centigrade is =", c)

Output:

Enter the fahrenheit : 113


The centigrade is = 45.0

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 11


Develop a Python program to demonstrate sequence execution (average of
three numbers).

Program:

num1 = float(input("Enter the first number : "))


num2 = float(input("Enter the first number : "))
num3 = float(input("Enter the first number : "))
avg = (num1 + num2 + num3) / 3
print("Average =", avg)

Output:

Enter the first number : 10


Enter the first number : 20
Enter the first number : 30
Average = 20.0

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the cutoff marks in an engineering
admission using if statement.

Program:

cutoff = float(input("Enter cutoff mark : "))


quota = int(input("Enter the quota 1-General 2-Sports : "))
if quota == 2:
cutoff = cutoff + 5
print("Cutoff mark =", cutoff)

Output:

Enter cutoff mark : 170


Enter the quota 1-General 2-Sports : 1
Cutoff mark = 170.0

Enter cutoff mark : 160


Enter the quota 1-General 2-Sports : 2
Cutoff mark = 165.0

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 13


Develop a Python program to find whether the given number is even or odd
using if statement.

Program:

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


if num % 2 == 0:
print("The given number is even")
if num % 2 != 0:
print("The given number is odd")

Output:

Enter the number : 10


The given number is even

Enter the number : 5


The given number is odd

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find whether the given number is positive or
negative using if statement.

Program:

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


if num > 0:
print("The given number is positive")
if num < 0:
print("The given number is negative")

Output:

Enter the number : 2


The given number is positive

Enter the number : -14


The given number is negative

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 15


Develop a Python program to check whether the person is eligible to vote
or not using if statement.

Program:

age = int(input("Enter the age : "))


if age >= 18:
print("The person is eligible to vote")
if age < 18:
print("The person is not eligible to vote")

Output:

Enter the age : 20


The person is eligible to vote

Enter the age : 16


The person is not eligible to vote

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to check whether the person is in teen age or
not using if statement.

Program:

age = int(input("Enter the age : "))


if age >= 13 and age <= 19:
print("The person is in teen age")
if age <= 12 or age >= 20:
print("The person is not in teen age")

Output:

Enter the age : 16


The person is in teen age

Enter the age : 21


The person is not in teen age

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 17


Develop a Python program to find whether the given number is positive or
negative using if...else statement.

Program:

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


if num > 0:
print("The given number is positive")
else:
print("The given number is negative")

Output:

Enter the number : 2


The given number is positive

Enter the number : -14


The given number is negative

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find whether the given number is even or odd
using if...else statement.

Program:

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


if num % 2 == 0:
print("The given number is even")
else:
print("The given number is odd")

Output:

Enter the number : 10


The given number is even

Enter the number : 5


The given number is odd

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 19


Develop a Python program to check whether the person is eligible to vote
or not using if...else statement.

Program:

age = int(input("Enter the age : "))


if age >= 18:
print("The person is eligible to vote")
else:
print("The person is not eligible to vote")

Output:

Enter the age : 20


The person is eligible to vote

Enter the age : 16


The person is not eligible to vote

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to check whether the person is in teen age or
not using if...else statement.

Program:

age = int(input("Enter the age : "))


if age >= 13 and age <= 19:
print("The person is in teen age")
else:
print("The person is not in teen age")

Output:

Enter the age : 16


The person is in teen age

Enter the age : 21


The person is not in teen age

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 21


Develop Python program to input the mark and print grade. Grades are
awarded according to the following criteria:

91 to 100 S Grade
81 to 90 A Grade
71 to 80 B Grade
61 to 70 C Grade
57 to 60 D Grade
50 to 56 E Grade
<50 U Grade

Program:

mark = int(input("Enter the mark : "))


if mark >= 91:
print("S Grade")
elif mark >= 81:
print("A Grade")
elif mark >= 71:
print("B Grade")
elif mark >= 61:
print("C Grade")
elif mark >= 57:
print("D Grade")
elif mark >= 50:
print("E Grade")
else:
print("U Grade")

Output

Enter the mark : 85


A Grade

Enter the mark : 45


U Grade

22 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to check whether the given year is leap year or
not using if...elif...else statement.

Program:

year = int(input("Enter the year : "))


if year % 400 == 0:
print(year, "is a leap year")
elif year % 4 == 0 and year % 100 != 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

Output:

Enter the year : 2012


2012 is a leap year

Enter the year : 2013


2013 is not a leap year

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 23


Develop a Python program to calculate electricity bill for domestic
consumers as follows:
Units Charges (Rs. / Unit)
Upto 100 3.00
101 – 200 3.25
201 – 500 Rs.700 + 4.60 per unit excess of 200
501 and above Rs.2080 + 6.60 per unit excess of 500

Program:

units = int(input("Enter the units consumed : "))


if units <= 100:
amount = 3.00 * units
elif units <= 200:
amount = 3.25 * units
elif units <= 500:
amount = 700.00 + 4.60 * (units - 200)
else:
amount = 2080.00 + 6.60 * (units - 500)
print("Units =", units, "Amount =", amount)

Output:

Enter the units consumed : 77


Units = 77 Amount = 231.0

Enter the units consumed : 140


Units = 140 Amount = 455.0

Enter the units consumed : 280


Units = 280 Amount = 1068.0

Enter the units consumed : 560


Units = 560 Amount = 2476.0

24 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to compute the roots of a quadratic equation.

Program:

a = float(input("Enter the value for a : "))


b = float(input("Enter the value for b : "))
c = float(input("Enter the value for c : "))
d = (b ** 2) - (4 * a * c)
if d < 0:
print("Roots are imaginary")
real = - b / (2 * a)
d = -d
imag = (d ** 0.5) / (2 * a)
print("root 1 =", real, "+ j", imag)
print("root 2 =", real, "- j", imag)
elif d == 0:
print("Roots are real and equal")
r = - b / (2 * a)
print("root =", r)
elif d > 0:
print("Roots are real and unequal")
r1 = (- b + (d ** 0.5)) / (2 * a)
r2 = (- b - (d ** 0.5)) / (2 * a)
print("root 1 =", r1)
print("root 2 =", r2)

Output:

Enter the value for a : 1


Enter the value for b : 2
Enter the value for c : 7
Roots are imaginary
root 1 = -1.0 + j 2.449489742783178
root 2 = -1.0 - j 2.449489742783178

Enter the value for a : 1


Enter the value for b : 2
Enter the value for c : 1
Roots are real and equal
root = -1.0

Enter the value for a : 2


Enter the value for b : 7
Enter the value for c : 1
Roots are real and unequal
root 1 = -0.14921894064178787
root 2 = -3.350781059358212

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 25


Develop a Python program to check if the number is positive or negative
or zero using nested if..else statement.

Program:

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


if num == 0:
print("The number is zero")
else:
if num > 0:
print("The number is positive")
else:
print("The number is negative")

Output:

Enter the number : 2


The number is positive

Enter the number : -14


The number is negative

Enter the number : 0


The number is zero

26 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the biggest of 3 numbers using nested
if...else statement.

Program:

a = int(input("Enter the first number : "))


b = int(input("Enter the second number : "))
c = int(input("Enter the third number : "))
if a > b:
if a > c:
print(a, "is the biggest number")
else:
print(c, "is the biggest number")
else:
if b > c:
print(b, "is the biggest number")
else:
print(c, "is the biggest number")

Output:

Enter the first number : 10


Enter the second number : 20
Enter the third number : 30
30 is the biggest number

Enter the first number : 20


Enter the second number : 30
Enter the third number : 10
30 is the biggest number

Enter the first number : 30


Enter the second number : 10
Enter the third number : 20
30 is the biggest number

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 27


Develop a Python program to check whether the given year is leap year or
not using nested if...else statement.

Program:

year = int(input("Enter the year: "))


if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

Output:

Enter the year: 2012


2012 is a leap year

Enter the year: 2013


2013 is not a leap year

28 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the sum of numbers up to 10 using while
statement.

Program:

s = 0
i = 1
while i <= 10:
s = s + i
i = i + 1
print("The sum =", s)

Output:

The sum = 55

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 29


Develop a Python program to count the number of digits in an integer
using while statement.

Program:

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


count = 0
while n > 0:
count = count + 1
n = n // 10
print("The number of digits =", count)

Output:

Enter the number : 179


The number of digits = 3

30 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the sum of digits in an integer using
while statement.

Program:

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


s = 0
while n > 0:
rem = n % 10
s = s + rem
n = n // 10
print("The sum of the digits =", s)

Output:

Enter the number : 786


The sum of the digits = 21

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 31


Develop a Python program to reverse the digits of a given number using
while statement.

Program:

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


rev = 0
while n > 0:
rem = n % 10
rev = rem + (rev * 10)
n = n // 10
print("The reversed number =", rev)

Output:

Enter the number : 2468


The reversed number = 8642

32 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to check whether the number and its reverse are
same or not using while statement.

Program:

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


temp = n
rev = 0
while n > 0:
rem = n % 10
rev = rem + (rev * 10)
n = n // 10
if temp == rev:
print("The number and its reverse are same")
else:
print("The number and its reverse are not same")

Output:

Enter the number : 143


The number and its reverse are not same

Enter the number : 141


The number and its reverse are same

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 33


Develop a Python program to find the largest digit of a number using
while statement.

Program:

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


large = 0
while n > 0:
rem = n % 10
if rem > large:
large = rem
n = n // 10
print("The largest digit =", large)

Output:

Enter the number : 786


The largest digit = 8

34 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find sum of the digits, reversal of the
digits and largest of digit using while statement.

Program:

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


s = 0
rev = 0
large = 0
while n > 0:
rem = n % 10
s = s + rem
rev = rem + (rev * 10)
if rem > large:
large = rem
n = n // 10
print("The sum of the digits =", s)
print("The reverse of the digits =", rev)
print("The largest digit =", large)

Output:

Enter the number : 786


The sum of the digits = 21
The reverse of the digits = 687
The largest digit = 8

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 35


Develop a Python program to check whether the given number is Armstrong
number or not using while statement.

Program:

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


temp = n
s = 0
nod = len(str(n))
while n > 0:
rem = n % 10
s = s + (rem ** nod)
n = n // 10
if s == temp:
print(temp, "is an Armstrong number")
else:
print(temp, "is not an Armstrong number")

Output:

Enter the number : 153


153 is an Armstrong number

Enter the number : 123


123 is not an Armstrong number

36 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the value of x n using while statement.

Program:

x = int(input("Enter the value of x : "))


n = int(input("Enter the value of n : "))
i = 1
s = 1
while i <= n:
s = s * x
i = i + 1
print("The power of", x, "^", n, "=", s)

Output:

Enter the value of x : 2


Enter the value of n : 4
The power of 2 ^ 4 = 16

Enter the value of x : 2


Enter the value of n : 0
The power of 2 ^ 0 = 1

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 37


Develop a Python program to find the factorial of a given number using
while statement.

Program:

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


fact = 1
i = 1
while i <= n:
fact = fact * i
i = i + 1
print("The factorial of", n, "is", fact)

Output:

Enter the number : 5


The factorial of 5 is 120

38 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the sum of numbers up to 10 using for
statement.

Program:

s = 0
for i in range(1, 11):
s = s + i
print("The sum =", s)

Output:

The sum = 55

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 39


Develop a Python program to find the factorial of a given number using
for statement.

Program:

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


fact = 1
for i in range(1, n + 1):
fact = fact * i
print("The factorial of", n, "is", fact)

Output:

Enter the number : 5


The factorial of 5 is 120

40 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to print addition table of the given number
using for statement.

Program:

t = int(input("Enter the table : "))


n = int(input("Enter the limit : "))
for i in range(1, n + 1):
print(i, "+", t, "=", i + t)

Output:

Enter the table : 5


Enter the limit : 15
1 + 5 = 6
2 + 5 = 7
3 + 5 = 8
4 + 5 = 9
5 + 5 = 10
6 + 5 = 11
7 + 5 = 12
8 + 5 = 13
9 + 5 = 14
10 + 5 = 15
11 + 5 = 16
12 + 5 = 17
13 + 5 = 18
14 + 5 = 19
15 + 5 = 20

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 41


Develop a Python program to print multiplication table of the given
number using for statement.

Program:

t = int(input("Enter the table : "))


n = int(input("Enter the limit : "))
for i in range(1, n + 1):
print(i, "*", t, "=", i * t)

Output:

Enter the table : 5


Enter the limit : 15
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
11 * 5 = 55
12 * 5 = 60
13 * 5 = 65
14 * 5 = 70
15 * 5 = 75

42 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the average of first n natural numbers
without using the formula using for statement.

Program:

n = int(input("Enter the limit : "))


s = 0
for i in range(1, n + 1):
s = s + i
avg = s / n
print("Average of first", n, "numbers is :", avg)

Output:

Enter the limit : 10


Average of first 10 numbers is : 5.5

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 43


Develop a Python program to print all the divisors of a given number
using for statement.

Program:

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


print("The divisors are :")
for i in range(1, n + 1):
if n % i == 0:
print(i, end = "\t")

Output:

Enter the number : 15


The divisors are :
1 3 5 15

44 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to print the numbers that are divisible by a
given number using for statement.

Program:

n = int(input("Enter the limit : "))


d = int(input("Enter the number : "))
print("The numbers divisible by", d, "are :")
for i in range(1, n + 1):
if i % d == 0:
print(i, end = "\t")

Output:

Enter the limit : 15


Enter the number : 3
The numbers divisible by 3 are :
3 6 9 12 15

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 45


Develop a Python program to generate odd and even numbers using for
statement.

Program:

n = int(input("Enter the limit : "))


print("The odd numbers are :")
for i in range(1, n + 1, 2):
print(i, end = "\t")
print("\nThe even numbers are :")
for i in range(2, n + 1, 2):
print(i, end = "\t")

Output:

Enter the limit : 10


The odd numbers are :
1 3 5 7 9
The even numbers are :
2 4 6 8 10

46 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the sum of odd and even numbers using
for statement.

Program:

n = int(input("Enter the limit : "))


oddsum = 0
evensum = 0
for i in range(1, n + 1, 2):
oddsum = oddsum + i
for i in range(2, n + 1, 2):
evensum = evensum + i
print("The odd numbers sum is :", oddsum)
print("The even numbers sum is :", evensum)

Output:

Enter the limit : 10


The odd numbers sum is : 25
The even numbers sum is : 30

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 47


Develop a Python program to print first ‘n’ terms of the Fibonacci series
using for statement.

Program:

n = int(input("Enter the number of terms : "))


a = -1
b = 1
print("The fibonacci series is :")
for i in range(1, n + 1):
c = a + b
print(c, end = "\t")
a = b
b = c

Output:

Enter the number of terms : 7


The fibonacci series is :
0 1 1 2 3 5 8

48 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to the sum of Fibonacci series using for
statement.

Program:

n = int(input("Enter the number of terms : "))


a = -1
b = 1
s = 0
print("The fibonacci series is :")
for i in range(1, n + 1):
c = a + b
print(c, end = "\t")
s = s + c
a = b
b = c
print("\nThe sum of the series is :", s)

Output:

Enter the number of terms : 5


The fibonacci series is :
0 1 1 2 3
The sum of the series is : 7

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 49


Develop a Python program to find the given number is perfect number or
not using for statement.

Program:

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


s = 0
for i in range(1, n):
if n % i == 0:
s = s + i
if s == n:
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")

Output:

Enter the number : 6


6 is a perfect number

Enter the number : 28


28 is a perfect number

Enter the number : 12


12 is not a perfect number

50 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to generate numbers between 20 and 100 which are
divisible by 2 and not divisible by 3 and 5 using for statement.

Program:

print("The numbers are :")


for i in range(20, 101):
if i % 2 == 0 and (i % 3 != 0 and i % 5 != 0):
print(i, end = "\t")

Output:

The numbers are :


22 26 28 32 34 38 44 46 52 56 58 62 64 68
74 76 82 86 88 92 94 98

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 51


Develop a Python program to find the sum of the series 1 + x2/2 + x3/3 +
... xn/n using for statement.

Program:

n = int(input("Enter the number of terms : "))


x = int(input("Enter the value of x : "))
s = 1
for i in range(2, n + 1):
s = s + (x ** i) / i
print("The sum of the series is =", s)

Output:

Enter the number of terms : 5


Enter the value of x : 2
The sum of the series is = 16.066666666666666

52 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to find the sum of the series 1 + 1/2 + 1/3 +
... 1/n using for statement.

Program:

n = int(input("Enter the number of terms : "))


s = 1
for i in range(2, n + 1):
s = s + 1 / i
print("The sum of the series is =", s)

Output:

Enter the number of terms : 5


The sum of the series is = 2.283333333333333

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 53


Develop a Python program to generate the Armstrong numbers from 1 – 1000.

Program:

print("The Armstrong numbers from 1 to 1000 are :")


for i in range(1, 1001):
n = i
s = 0
nod = len(str(n))
while n > 0:
rem = n % 10
s = s + (rem ** nod)
n = n // 10
if s == i:
print(i, end = "\t")

Output:

The Armstrong numbers from 1 to 1000 are :


1 2 3 4 5 6 7 8 9 153 370 371 407

54 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to check whether a given number is a prime
number or not and print the result using for statement.

Program:

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


flag = 0
for i in range(2, n//2 + 1):
if n % i == 0:
flag = 1
break
if flag == 1:
print("The number is not a prime")
else:
print("The number is a prime")

Output:

Enter the number : 6


The number is not a prime

Enter the number : 11


The number is a prime

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 55


Develop a Python program to print 10 x 10 multiplication table using
nested for statement.

Program:

print("Multiplication Table")
for i in range(1, 11):
for j in range(1, 11):
print(i * j, end = "\t")
print()

Output:

Multiplication Table
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

56 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Develop a Python program to generate Floyd's triangle using nested for
statement.

Program:

n = int(input("Enter the number of rows : "))


c = 1
for i in range(n):
for j in range(i + 1):
print(c, end = "\t")
c = c + 1
print()

Output:

Enter the number of rows : 5


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 57


Develop a Python program to the first ‘N’ prime numbers using nested for
statement.

Program:

a = int(input("Enter the limit : "))


for n in range(1, a + 1):
flag = 0
for i in range(2, n//2 + 1):
if n % i == 0:
flag = 1
break
if flag == 0:
print(n, end = "\t")

Output:

Enter the limit : 7


1 2 3 5 7

58 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 59
60 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai

You might also like