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

Python_Test_2

The document contains various Python programs that demonstrate how to perform operations related to numbers, such as finding prime numbers, checking for perfect and Armstrong numbers, calculating sums, and generating patterns. Each program is accompanied by source code and sample outputs for different cases. Additionally, it includes programs for mathematical functions like finding roots of quadratic equations and calculating simple interest.

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_2

The document contains various Python programs that demonstrate how to perform operations related to numbers, such as finding prime numbers, checking for perfect and Armstrong numbers, calculating sums, and generating patterns. Each program is accompanied by source code and sample outputs for different cases. Additionally, it includes programs for mathematical functions like finding roots of quadratic equations and calculating simple interest.

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/ 13

Python Programs on Numbers

Python Program to Find Prime Numbers in a Given Range


Source Code

r=int(input("Enter upper limit: "))


for a in range(2,r+1):
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print(a)

OutPut

Case 1:
Enter upper limit: 15
2
3
5
7
11
13

Case 2:
Enter upper limit: 40
2
3
5
7
11
13
17
19
23
29
31
37

Python Program to Check Prime Number


Source Code

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


k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print("Number is prime")
else:
print("Number isn't prime")

OutPut

Enter number: 7
Number is prime

Python Program to Check whether a Number is Prime or Not using Recursion


Source Code

def check(n, div = None):


if div is None:
div = n - 1
while div >= 2:
if n % div == 0:
print("Number not prime")
return False
else:
return check(n, div-1)
else:
print("Number is prime")
return 'True'
n=int(input("Enter number: "))
check(n)

OutPut

Case 1:
Enter number: 13
Number is prime

Case 2:
Enter number: 30
Number not prime

Python Program to Check Whether a Given Number is Perfect Number


Source Code

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

Case 1:
Enter any number: 6
The number is a Perfect number!

Case 2:
Enter any number: 25
The number is not a Perfect number!

Python Program to Check Armstrong Number


Source Code

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


a=list(map(int,str(n)))
b=list(map(lambda x:x**3,a))
if(sum(b)==n):
print("The number is an armstrong number. ")
else:
print("The number is not an arsmtrong number. ")

OutPut

Enter any number: 371


The number is an armstrong number.

Python Program to Find Sum of First N Natural Numbers


Source Code

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


sum1 = 0
while(n > 0):
sum1=sum1+n
n=n-1
print("The sum of first n natural numbers is",sum1)

OutPut

Enter a number: 18
The sum of first n natural numbers is 171

Python Program to Check if a Number is a Strong Number


Source Code

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

OutPut

Case 1:
Enter a number:145
The number is a strong number.

Case 2:
Enter a number:234
The number is not a strong number.

Python Program to Print Numbers in a Range Without using Loops


Source Code

def printno(upper):
if(upper>0):
printno(upper-1)
print(upper)
upper=int(input("Enter upper limit: "))
printno(upper)

OutPut

Case 1:
Enter upper limit: 5
1
2
3
4
5

Case 2:
Enter upper limit: 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Python Program to Print the Natural Numbers Summation Pattern


Source Code

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


for j in range(1,n+1):
a=[]
for i in range(1,j+1):
print(i,sep=" ",end=" ")
if(i<j):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))

print()

OutPut

Case 1:
Enter a number: 4
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

Case 2:
Enter a number: 5
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
Python Program to Find the Prime Factors of a Number
Source Code

n=int(input("Enter an integer:"))
print("Factors are:")
i=1
while(i<=n):
k=0
if(n%i==0):
j=1
while(j<=i):
if(i%j==0):
k=k+1
j=j+1
if(k==2):
print(i)
i=i+1

OutPut

Case 1:
Enter an integer:25
Factors are:
5

Case 2:
Enter an integer:200
Factors are:
2
5

Python Program to Find Whether a Number is a Power of Two


Source Code

def is_power_of_two(n):
"""Return True if n is a power of two."""
if n <= 0:
return False
else:
return n & (n - 1) == 0

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

if is_power_of_two(n):
print('{} is a power of two.'.format(n))
else:
print('{} is not a power of two.'.format(n))
OutPut

Case 1:
Enter a number: 5
5 is not a power of two.

Case 2:
Enter a number: 0
0 is not a power of two.

Case 3:
Enter a number: 8
8 is a power of two.

Python Program to Find Product of Two Numbers using Recursion


Source Code

def product(a,b):
if(a<b):
return product(b,a)
elif(b!=0):
return(a+product(a,b-1))
else:
return 0
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print("Product is: ",product(a,b))

OutPut

Case 1:
Enter first number: 12
Enter second number: 10
Product is: 120

Case 2:
Enter first number: 12
Enter second number: 11
Product is: 132

Python Program to Find All Perfect Squares in the Given Range


Source Code

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


u=int(input("Enter upper range: "))
a=[]
a=[x for x in range(l,u+1) if (int(x**0.5))**2==x and
sum(list(map(int,str(x))))<10]
print(a)

OutPut

Case 1:
Enter lower range: 1
Enter upper range: 40
[1, 4, 9, 16, 25, 36]

Case 2:
Enter lower range: 50
Enter upper range: 100
[81, 100]

Python Program to Print All Possible Combinations of Three Digits


Source Code

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


b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k])

OutPut

Case 1:
Enter first number:1
Enter second number:2
Enter third number:3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Case 2:
Enter first number:5
Enter second number:7
Enter third number:3
5 7 3
5 3 7
7 5 3
7 3 5
3 5 7
3 7 5

Pattern Programs in Python

Python Program to Print an Inverted Star Pattern

Source Code

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


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

OutPut

Case 1:
Enter number of rows: 5
*****
****
***
**
*

Case 2:
Enter number of rows: 10
**********
*********
********
*******
******
*****
****
***
**
*

Python Program to Print Pascal Triangle

Source Code

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


a=[]
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1,i):
a[i].append(a[i-1][j-1]+a[i-1][j])
if(n!=0):
a[i].append(1)
for i in range(n):
print(" "*(n-i),end=" ",sep=" ")
for j in range(0,i+1):
print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
print()

OutPut

Case 1:

Enter number of rows: 3

1
1 1
1 2 1

Case 2:

Enter number of rows: 4

1
1 1
1 2 1
1 3 3 1

Python Programs on Math Functions

 Python Program to Find the Roots of a Quadratic Equation


Source Code

print("Equation: ax^2 + bx + c ")


a=int(input("Enter a: "))
b=int(input("Enter b: "))
c=int(input("Enter c: "))
d=b**2-4*a*c
d1=d**0.5
if(d<0):
print("The roots are imaginary. ")
else:
r1=(-b+d1)/2*a
r2=(-b-d1)/2*a
print("The first root: ",round(r1,2))
print("The second root: ",round(r2,2))

OutPut

Case 1:
Equation: ax^2 + bx + c
Enter a: 1
Enter b: -5
Enter c: 6
The first root: 3.0
The second root: 2.0

Case 2:
Equation: ax^2 + bx + c
Enter a: 1
Enter b: 5
Enter c: 10
The roots are imaginary.

 Python Program to Find the Area of a Triangle


Source Code

import math
a=int(input("Enter first side: "))
b=int(input("Enter second side: "))
c=int(input("Enter third side: "))
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ",round(area,2))

OutPut

Case 1:
Enter first side: 15
Enter second side: 9
Enter third side: 7
Area of the triangle is: 20.69

Case 2:
Enter first side: 5
Enter second side: 6
Enter third side: 7
Area of the triangle is: 14.7

 Python Program to Find Simple Interest


Source Code

principle=float(input("Enter the principle amount:"))


time=int(input("Enter the time(years):"))
rate=float(input("Enter the rate:"))
simple_interest=(principle*time*rate)/100
print("The simple interest is:",simple_interest)

OutPut

Enter the principle amount:200


Enter the time(years):5
Enter the rate:5.0
The simple interest is: 50.0

 Python Program to Find Quotient and Remainder of Two Numbers


Source Code

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


b=int(input("Enter the second number: "))
quotient=a//b
remainder=a%b
print("Quotient is:",quotient)
print("Remainder is:",remainder)

OutPut

Case 1:
Enter the first number: 15
Enter the second number: 7
Quotient is: 2
Remainder is: 1

Case 2:
Enter the first number: 125
Enter the second number: 7
Quotient is: 17
Remainder is: 6

 Python Program to Swap Two Numbers without using Third Variable


Source Code

a=int(input("Enter value of first variable: "))


b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)

OutPut

Case 1
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3

Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56

 Python Program to Print an Identity Matrix


Source Code

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


for i in range(0,n):
for j in range(0,n):
if(i==j):
print("1",sep=" ",end=" ")
else:
print("0",sep=" ",end=" ")
print()

OutPut

Case 1:
Enter a number: 4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Case 2:
Enter a number: 5
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

You might also like