Logics
Logics
Sridhar/P&T/TT - Colaboratory
Code Text
GCD of two numbers | Program to find the GCD or HCF of two numbers
#Simple Program
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
a = int(input())
b = int(input())
lcm = 0
if(a > b):
lcm = a
else:
lcm = b
while(1):
if( lcm % a == 0 and lcm % b == 0 ):
print("LCM of",a,"and",b,"is",lcm)
break
lcm = lcm + 1
4
2
LCM of 4 and 2 is 4
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 1/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
a=int(input())
b=int(input())
for Number in range (a, b):
count = 0
for i in range(2, Number):
if(Number % i == 0):
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 2/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
count = count + 1
break
10
20
11 13 17 19
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 3/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
sum += digit ** order
temp //= 10
if num == sum:
print(num)
num=int(input("Enter a number:"))
sum=0
temp=num
while(num):
i=1
fact=1
rem=num%10
while(i<=rem):
fact=fact*i
i=i+1
sum=sum+fact
num=num//10
if(sum==temp):
print("Given number is a strong number")
else:
print("Given number is not a strong number")
Enter a number:145
Given number is a strong number
Automorphic Number or Not Input: 25 Output - Yes, it is an automorphic number. Reason - The square of 25 gives 625, since the
last digits are 25 it is an automorphic number.
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 4/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
Enter a number : 12
It is an abundant number.
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 5/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1,end=" ")
n = n1 + n2
n1 = n2
n2 = n
count += 1
def binaryTodecimal(n):
decimal = 0
mul = 1
while n>0:
rem = n%10
n = n//10
decimal = decimal + rem*mul
mul = mul*2
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 6/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
return decimal
print(binaryTodecimal(101))
print(binaryTodecimal(1010))
print(binaryTodecimal(0))
print(binaryTodecimal(1))
print(binaryTodecimal(1000))
print(binaryTodecimal(100))
print(binaryTodecimal(111))
5
10
0
1
8
4
7
Decimal to Binary
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end='')
dec = int(input("Enter Decimal Value: "))
convertToBinary(dec)
print()
binary_number = '0b1011'
decimal_number = int(binary_number,2)
print(decimal_number)
11
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 7/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
Reverse Number
(number of rows)
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 8/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
Output:
121
12321
1234321
123454321
121
12321
1234321
123454321
Palindrome half pyramid pattern using alphabets Input: 5 (number of rows) Output: A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABA
ABCBA
ABCDCBA
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 9/1
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
ABCDEDCBA
def print_number_pyramid(rows):
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
for j in range(i - 1, 0, -1):
print(j, end=" ")
print()
num_rows = int(input("Enter the number of rows for the pyramid: "))
print_number_pyramid(num_rows)
def print_diamond(rows):
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("* " * i)
print_diamond(num_rows)
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 10/
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
Floyd's triangle
23
456
7 8 9 10
11 12 13 14 15
def print_floyds_triangle(rows):
num = 1
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
# Get the number of rows for Floyd's triangle from the user
num_rows = int(input("Enter the number of rows for Floyd's triangle: "))
Pascal triangle
def print_pascals_triangle(rows):
for i in range(rows):
num = 1
for j in range(1, rows - i):
print(" ", end="")
for k in range(0, i + 1):
print(num, end=" ")
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 11/
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
num = num * (i - k) // (k + 1)
print()
print_pascals_triangle(num_rows)
Remove Vowels
Find all Triplets with the given sum in the given array Input: array = {12, 3, 4, 1, 6, 9}, sum = 24; Output: 12, 3, 9
Triplet is 4 , 10 , 8
True
test_str = input()
all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 12/
7/25/23, 1:30 PM D.Sridhar/P&T/TT - Colaboratory
all_freq[i] = 1
print("Count of all characters is :\n "+ str(all_freq))
saveetha
Count of all characters is :
{'s': 1, 'a': 2, 'v': 1, 'e': 2, 't': 1, 'h': 1}
Palindrom or not
string=input(("Enter a letter:"))
if(string==string[::-1]):
print("The {} is a palindrome".format(string))
else:
print("The {} is not palindrome".format(string))
Enter a letter:setes
The setes is a palindrome
def is_palindrome(num):
num_str = str(num)
return num_str == num_str[::-1]
def find_largest_palindrome(arr):
largest_palindrome = None
completed at 1:27 PM
https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 13/