0% found this document useful (0 votes)
1 views

Python_Test_Programs

The document contains a series of simple Python programs that demonstrate various programming concepts such as checking if a number is even or odd, identifying positive or negative numbers, and calculating the sum of digits. Each program includes source code, example outputs, and explanations of the logic used. The programs cover a range of topics including recursion, loops, and conditionals.

Uploaded by

vdell3580
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)
1 views

Python_Test_Programs

The document contains a series of simple Python programs that demonstrate various programming concepts such as checking if a number is even or odd, identifying positive or negative numbers, and calculating the sum of digits. Each program includes source code, example outputs, and explanations of the logic used. The programs cover a range of topics including recursion, loops, and conditionals.

Uploaded by

vdell3580
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/ 11

Object 1

Simple Python Programs


Python Program to Check Whether a Given Number is Even or Odd using Recursion

Source Code

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

if n % 2 == 0:
print(n, "is an even number.")
else:
print(n, "is an odd number.")

OutPut

Enter a number: 5

5 is an odd number.

Python Program to Check Whether a Number is Positive or Negative

Source Code

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


if(n>0):
print("Number is positive")
else:
print("Number is negative")

OutPut

Case 1:

Enter number: 45
Number is positive

Case 2:

Enter number: -30


Number is negative
Python Program to Print All Odd Numbers in a Range

Source Code

lower=int(input("Enter the lower limit for the range:"))


upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)

OutPut

Case 1:

Enter the lower limit for the range:1


Enter the upper limit for the range:16
1
3
5
7
9
11
13
15

Case 2:

Enter the lower limit for the range:150


Enter the upper limit for the range:180
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179

Python Program to Check if a Number is a Palindrome

Source Code

n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

OutPut

Enter number:121

The number is a palindrome!

Enter number:567

The number isn't a palindrome!

Python Program to Reverse a Number

Source Code

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


rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)

OutPut

Enter number: 124

Reverse of the number: 421

Enter number: 4538

Reverse of the number: 8354


Python Program to Print All Integers that Aren’t Divisible by Either 2 or 3

Source Code

for i in range(0,51):
if(i%2!=0&i%3!=0):
print(i)

OutPut

1
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49

Python Program to Find Numbers which are Divisible by 7 and Multiple of 5 in a Given Range

Source Code

lower=int(input("Enter the lower range:"))


upper=int(input("Enter the upper range:"))
for i in range (lower,upper+1):
if(i%7==0 and i%5==0):
print(i)

OutPut

Case 1:
Enter the lower range:1
Enter the upper range:100
35
70

Case 2:
Enter the lower range:400
Enter the upper range:700
420
455
490
525
560
595
630
665
700

Python Program to Print All Numbers in a Range Divisible by a Given Number

Source Code

lower=int(input("Enter lower range limit:"))


upper=int(input("Enter upper range limit:"))
n=int(input("Enter the number to be divided by:"))
for i in range(lower,upper+1):
if(i%n==0):
print(i)

OutPut

Case 1:
Enter lower range limit:1
Enter upper range limit:50
Enter the number to be divided by:5
5
10
15
20
25
30
35
40
45
50

Case 2:
Enter lower range limit:50
Enter upper range limit:100
Enter the number to be divided by:7
56
63
70
77
84
91
98

Python Program to Find Sum of Digits of a Number

Source Code

n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)

OutPut

Case 1:
Enter a number:1892
The total sum of digits is: 20

Case 2:
Enter a number:157
The total sum of digits is: 13

Python Program to Find Sum of Digit of a Number using Recursion


Source Code

l=[]
def sum_digits(b):
if(b==0):
return l
dig=b%10
l.append(dig)
sum_digits(b//10)
n=int(input("Enter a number: "))
sum_digits(n)
print(sum(l))

OutPut

Case 1:
Enter a number: 135
9

Case 2:
Enter a number: 546
15

Python Program to Find Sum of Digit of a Number Without Recursion


Source Code

l=[]
b=int(input("Enter a number: "))
while(b>0):
dig=b%10
l.append(dig)
b=b//10
print("Sum is:")
print(sum(l))
OutPut

Case 1:
Enter a number: 123
Sum is:
6

Case 2:
Enter a number: 567
Sum is:
18

Python Program to Count the Number of Digits in a Number

Source Code

lower=int(input("Enter lower range limit:"))


upper=int(input("Enter upper range limit:"))
n=int(input("Enter the number to be divided by:"))
for i in range(lower,upper+1):
if(i%n==0):
print(i)

OutPut

Case 1:
Enter lower range limit:1
Enter upper range limit:50
Enter the number to be divided by:5
5
10
15
20
25
30
35
40
45
50

Case 2:
Enter lower range limit:50
Enter upper range limit:100
Enter the number to be divided by:7
56
63
70
77
84
91
98
Python Program to Find All the Divisors of an Integer
Source Code

n=int(input("Enter an integer:"))
print("The divisors of the number are:")
for i in range(1,n+1):
if(n%i==0):
print(i)

OutPut

Case 1:
Enter an integer:25
The divisors of the number are:
1
5
25

Case 2:
Enter an integer:20
The divisors of the number are:
1
2
4
5
10
20

Python Program to Find the Smallest Divisor of an Integer


Source Code

n=int(input("Enter an integer:"))
a=[]
for i in range(2,n+1):
if(n%i==0):
a.append(i)
a.sort()
print("Smallest divisor is:",a[0])

OutPut

Case 1:
Enter an integer:75
Smallest divisor is: 3

Case 2:
Enter an integer:64
Smallest divisor is: 2
Python Program to Print Binary Equivalent of an Integer using Recursion
Source Code

l=[]
def convert(b):
if(b==0):
return l
dig=b%2
l.append(dig)
convert(b//2)
a=int(input("Enter a number: "))
convert(a)
l.reverse()
print("Binary equivalent:")
for i in l:
print i,

OutPut

Case 1:
Enter a number: 20
Binary equivalent:
1 0 1 0 0

Case 2:
Enter a number: 7
Binary equivalent:
1 1 1

Python Program to Print Binary Equivalent of a Number without Using Recursion


Source Code

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


a=[]
while(n>0):
dig=n%2
a.append(dig)
n=n//2
a.reverse()
print("Binary Equivalent is: ")
for i in a:
print(i,end=" ")

OutPut

Case 1:
Enter a number: 2
Binary Equivalent is:
1 0
Case 2:
Enter a number: 7
Binary Equivalent is:
1 1 1

Python Program to Print Table of a Given Number


Source Code

n=int(input("Enter the number to print the tables for:"))


for i in range(1,11):
print(n,"x",i,"=",n*i)

OutPut

Case 1:
Enter the number to print the tables for:7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Case 2:
Enter the number to print the tables for:17
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170

Python Program to Calculate Grade of a Student


Source Code

sub1=int(input("Enter marks of the first subject: "))


sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")

OutPut

Case 1:
Enter marks of the first subject: 85
Enter marks of the second subject: 95
Enter marks of the third subject: 99
Enter marks of the fourth subject: 93
Enter marks of the fifth subject: 100
Grade: A

Case 2:
Enter marks of the first subject: 81
Enter marks of the second subject: 72
Enter marks of the third subject: 94
Enter marks of the fourth subject: 85
Enter marks of the fifth subject: 80
Grade: B

Python Program to Check Whether a given Year is a Leap Year


Source Code

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

OutPut

Enter year to be checked:2024


The year is a leap year!

You might also like