100% found this document useful (1 vote)
120 views14 pages

Python Programs PDF

The document contains 14 python programs related to numbers, including programs to: reverse a number, find the sum of digits, calculate power, add/subtract numbers without operators, find the largest of 3 numbers, find the generic root of a number, prime factors of a number, NCR factor of a number, LCM and HCF of 2 numbers, swapping numbers and arrays, and converting between decimal, binary, and octal number systems. The programs provide sample inputs and outputs to demonstrate the functionality.

Uploaded by

mca kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
120 views14 pages

Python Programs PDF

The document contains 14 python programs related to numbers, including programs to: reverse a number, find the sum of digits, calculate power, add/subtract numbers without operators, find the largest of 3 numbers, find the generic root of a number, prime factors of a number, NCR factor of a number, LCM and HCF of 2 numbers, swapping numbers and arrays, and converting between decimal, binary, and octal number systems. The programs provide sample inputs and outputs to demonstrate the functionality.

Uploaded by

mca kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

1. Write a python program to check given number is perfect number or not.

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


sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

output

Enter any number: 6


The number is a Perfect number!

Enter any number: 20


The number is not a Perfect number!

2. Write a python program to check given number is Armstrong or not.

sum = 0
num=int(input("Enter any Number : "))
temp = num
while temp > 0:
digit = temp % 10
sum += (digit ** 3)
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

output

Enter any Number : 407


407 is an Armstrong number

Enter any Number : 107


107 is not an Armstrong number
3. Write a python program to check given number is prime number or not.

num=int(input("Enter any Number : "))


count=0
for i in range(1,num+1):
if (num%i == 0):
count+=1
if count == 2:
print("The number is Prime number!")
else:
print("The number is not a Prime number!")

output

Enter any Number : 13


The number is Prime number!

Enter any Number : 20


The number is not a Prime number!

4. Write a python program to check given number is strong number or not.


num=int(input("Enter a number:"))
temp=num
sum1=0
while(num):
i,f=1,1
r=num%10
while(i<=r):
f=f*i
i+=1
sum1+=f
num//=10
if(sum1==temp):
print("The number is a strong number")
else:
print("The number is not a strong number")

output

Enter a number:145
The number is a strong number

Enter a number:200
The number is not a strong number
5. write python program to check a number is odd or even.

num=int(input("Enter a number:"))
if(num%2==0):
print("The number is a Even number")
else:
print("The number is Odd number")

Output

Enter a number:100
The number is a Even number

Enter a number:201
The number is Odd number

6. Write a python program to check given number is palindrome or not.

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:161
The number is a palindrome!

Enter number:162
The number isn't a palindrome!

8. Write a python program to check given string is palindrome or not.

my_str =input('Enter a String : ')


my_str = my_str.casefold()
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
print("It is palindrome")
else:
print("It is not palindrome")

Output

Enter a String : malayalam


It is palindrome

Enter a String : vignannirula


It is not palindrome

7. Write a python program to solve quadratic equation.

import cmath
a = int(input('Enter a Value for a : '))
b = int(input('Enter a Value for b : '))
c = int(input('Enter a Value for c : '))
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))

output

Enter a Value for a : 1


Enter a Value for b : 5
Enter a Value for c : 6
The solution are (-3+0j) and (-2+0j)

8. Write a python program to print Fibonacci series of given range.

n=int(input('Enter END/Range value : '))


n1=int(input('Enter Feb[1] value : '))
n2=int(input('Enter Feb[2] value : '))
print(n1)
print(n2)
while((n1+n2)<n):
i=(n1+n2)
n1=n2
n2=i
print(n2)

output

Enter END/Range value : 100


Enter Feb[1] value : 1
Enter Feb[2] value : 1
1
1
2
3
5
8
13
21
34
55
89

9. Write a python program to get factorial of given number.

n=int(input('Enter a value : '))


fact=1
if n==0:
print(1)
else:
for i in range(1,n+1):
fact*=i
print(fact)

output

Enter a value : 5
120

10. Write a python program for Floyd’s triangle​.

n=int(input('Enter a Vaule : '))


count = 1
string = ""
for i in range(1,n+2):
for j in range(1,i):
string = string + " " + str(count)
count = count + 1
print(string)
string = ""

output

Enter a Vaule : 10

1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

11. Write a python program to print Pascal triangle.


12. Write a python program to generate multiplication table.

n=int(input('Enter a Table Number : '))


for i in range(1,n+1):
print(n,'*',i,'=',n*i)

output

Enter a Table Number : 10


10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

13. Write a python program to print ASCII value of all characters.


ch=input('Enter a string : ')
for i in ch:
print('ASCII value of ',i,' is ',ord(i))

output

Enter a string : vignan nirula


ASCII value of v is 118
ASCII value of i is 105
ASCII value of g is 103
ASCII value of n is 110
ASCII value of a is 97
ASCII value of n is 110
ASCII value of is 32
ASCII value of n is 110
ASCII value of i is 105
ASCII value of r is 114
ASCII value of u is 117
ASCII value of l is 108
ASCII value of a is 97

14. Write a python program which produces its own source code as its output

f=input("Enter file name : ")


x=open(f)
for i in x :
print(i)

output

Enter file name : 14.py


f=input("Enter file name : ")

x=open(f)

for i in x :

print(i)

python program with numbers


1. Write a python program to reverse any number.
n=int(input("Enter a Number : "))
rev=0
while(n):
r=n%10
rev=rev*10+r
n=n//10
print(rev)

output

Enter file name : 756


657

2. Write a python program to find out sum of digit of given number.

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


sum=0
r=0
while(n):
r=n%10
n//=10
sum+=r
print(sum)

output

Enter a Number : 12345


15

3. Write a python program to find out power of number. ​

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


n2=int(input("Enter number: ")) n2=int(input("Enter number: ")) n2=int(input("Enter number: "))
print(n1," power of ",n2," is ",pow(n1,n2)) print(n1," power of ",n2," is ",n1**n2) p,x=1,n2
while(n2):
p=p*n1
n2-=1
print(n1," power of ",x," is ",p)

output

Enter number: 2 Enter number: 2 Enter number: 2


Enter number: 10 Enter number: 10 Enter number: 10
2 power of 10 is 1024 2 power of 10 is 1024 2 power of 10 is 1024
4. Write a python program to add two numbers without using addition operator.​(s​um = a - (-b);​sum = a -
~b -1;)

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


n2=int(input("Enter number: "))
sum=n1 - (~n2) -1
print(sum)

output

Enter number: 10
Enter number: 20
30

5. Write a python program to subtract two numbers without using subtraction operator.​(​ sum = a + ~b +
1)

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


n2=int(input("Enter number: "))
sub=n1 + (~n2) + 1
print(sub)

output

Enter number: 20
Enter number: 30
-10

>>>

Enter number: 30
Enter number: 20
10

6. Write a python program to find largest among three numbers using binary minus operator.
​7. Write a python program to find largest among three numbers using conditional operator
8. Write a python program to find out generic root of any number.
(​Generic root: %d"​,(x=num%9)?x:9)
9. Write a python program to find out prime factor of given number.
10. Write a python program to find out NCR factor of given number.
11. How to convert string to int without using library functions in c
12. Program in c to print 1 to 100 without using loop
13. python program for swapping of two numbers
14. Program to find largest of n numbers in c
15. Split number into digits in python programming
16. python program to count number of digits in a number

L.C.M and H.C.F.

1. Write a python program to find out L.C.M. of two numbers.


2. Write a python program to find out H.C.F. of two numbers.
3. Write a python program to find out G.C.D. of two numbers.

Swapping

1. Write a python program to swap two numbers.


2. Write a python program to swap two numbers without using third variable.
3. Write a python program for swapping of two arrays.
4. Write a python program for swapping of two string.

Conversion ( Number System )

1. Write a python program to convert decimal number to binary number.


2. Write a python program to convert decimal number to octal number.
3. Write a python program to convert decimal number to hexadecimal number.
4. Write a python program to convert octal number to binary number.
5. Write a python program to convert octal number to decimal number.
6. Write a python program to convert octal number to hexadecimal number.
7. Write a python program to convert hexadecimal number to binary number.
8. Write a python program to convert hexadecimal number to octal number.
9. Write a python program to convert hexadecimal number to decimal number.
10. Write a python program to convert binary number to octal number.
11. Write a python program to convert binary number to decimal number.
12. Write a python program to convert binary number to hexadecimal number.
13. python program for addition of binary numbers .
14. python program for multiplication of two binary numbers.
15. python program fractional binary conversion from decimal.
16. python program for fractional decimal to binary fraction conversion.
17. python program to convert decimal number to roman.
18. python program to convert roman number to decimal number.
19. python program to convert each digits of a number in words
20. python program to convert currency or number in word.
Conversion ( Unit )

1. python program for unit conversion.

String
1. Write a python program to convert the string from upper case to lower case.
2. Write a python program to convert the string from lower case to upper case.
3. Write a python program to delete the all consonants from given string.
4. Write a python program to count the different types of characters in given string.
5. Write a python program to sort the characters of a string.
6. Write a python program for concatenation two strings without using string.h header file.
7. Write a python program to find the length of a string using pointer.
8. Write a python program which prints initial of any name.
9. Write a python program to print the string from given character.
10. Write a python program to reverse a string
11. Reverse a string using recursion in c
12. String concatenation in c without using strcat
13. How to compare two strings in c without using strcmp
14. String copy without using strcpy in c
15. Convert a string to ASCII in c

Matrix
1. Write a python program for addition of two matrices.
2. Write a python program for subtraction of two matrices
3. Write a python program for multiplication of two matrices.
4. Write a python program to find out sum of diagonal element of a matrix.
5. Write a python program to find out transport of a matrix.
6. Write a python program for scalar multiplication of matrix.
7. python program to find inverse of a matrix
8. Lower triangular matrix in c
9. Upper triangular matrix in c
10. Strassen's matrix multiplication program in c
11. python program to find determinant of a matrix

File
1. Write a python program to open a file and write some text and close its.
2. Write a python program to delete a file.
3. Write a python program to copy a file from one location to other location.
4. Write a python program to copy a data of file to other file.
5. Write a python program which display source code as a output.
6. Write a python program which writes string in the file.
7. Write a python program which reads string from file.
8. Write a python program which writes array in the file.
9. Write a python program which concatenate two file and write it third file.
10. Write a python program to find out size of any file.
11. Write a python program to know type of file.
12. Write a python program to know permission of any file.
13. Write a python program to know last date of modification of any file​.
14. Write a python program to find size and drive of any file.

Complex number
1. Complex numbers program in c
2. Write a python program for addition and subtraction of two complex numbers.
3. Write a python program for multiplication of two complex numbers.
4. Write a python program for division two complex numbers.

Series
1. Write a python program to find out the sum of series 1 + 2 + …. + n.
2. Write a python program to find out the sum of series 1^2 + 2^2 + …. + n^2.
3. Write a python program to find out the sum of series 1^3 + 2^3 + …. + n^3.
4. Write a python program to find out the sum of given A.P.
5. Write a python program to find out the sum of given G.P.
6. Write a python program to find out the sum of given H.P.
7. Write a python program to find out the sum of series 1 + 2 + 4 + 8 … to infinity.

Array
1. Write a python program to find out largest element of an array.
2. Write a python program to find out second largest element of an unsorted array.
3. Write a python program to find out second smallest element of an unsorted array.
4. Write a python program which deletes the duplicate element of an array.
5. Write a python program for delete an element at desired position in an array.
6. Write a python program for insert an element at desired position in an array.
7. python program to find largest and smallest number in an array

Sorting
1. Write a python program for bubble sort.
2. Write a python program for insertion sort.
3. Write a python program for selection sort.
4. Write a python program for quick sort.
5. Write a python program for heap sort.
6. Write a python program for merge sort.
7. Write a python program for shell sort.

Recursion
1. Write a python program to find factorial of a number using recursion.
2. Write a python program to find GCD of a two numbers using recursion.
3. Write a python program to find out sum digits of a number using recursion.
4. Write a python program to find power of a number using function recursion.
5. Write a python program to reverse any number using recursion.

Size of data type


1. Write a python program to find the size of int, double, structure, union without using sizeof operator.
Using pointer
1. Write a python program for concatenation two string using pointer.

Searching
1. Write a python program for linear search.
2. Write a python program for binary search.
3. Write a python program for binary search using recursion.

Area and volume


1. Write a python program to find the area of circle.
2. Write a python program to find the area of any triangle.
3. Write a python program to find the area of equilateral triangle.
4. ​Write a python program to find the area of right angled triangle​.
5. Write a python program to find the area of rectangle.
6. Write a python program to find the area of trapezium. ​
7. Write a python program to find the area of rhombus.
8. Write a python program to find the area of parallelogram.
9. Write a python program to find the volume and surface area of cube.
10. Write a python program to find the volume and surface area of cuboids.
11. Write a python program to find the volume and surface area of cylinder.
12. Write a python program to find the surface area and volume of a cone.
13. Write a python program to find the volume and surface area of sphere.
14. Write a python program to find the perimeter of a circle, rectangle and triangle.

python program with very large numbers

1. Write a python program to find factorial of 100 or very large numbers


2. Write a python program to multiply the two very large number (larger the long int)
3. Write a python program for division of large number (larger than long int)
4. python code for modular division of large number.
5. python code for division of large number.
6. python code for power of large numbers.

You might also like