[go: up one dir, main page]

0% found this document useful (0 votes)
19 views13 pages

Logics

Uploaded by

Keren Persis P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

Logics

Uploaded by

Keren Persis P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

7/25/23, 1:30 PM D.

Sridhar/P&T/TT - Colaboratory
Code Text

GCD of two numbers | Program to find the GCD or HCF of two numbers

Double-click (or enter) to edit

print("Enter two numbers : ")


a = int(input())
b = int(input())
i = 1
gcd = 0
while(i <= a and i <= b):
if((a % i == 0) and (b % i == 0)):
gcd = i
i = i + 1
print("GCD : ",gcd);

Enter two numbers :


3
5
GCD : 1

#Simple Program
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a

# Get input from the user


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# Calculate and print the GCD


result = gcd(num1, num2)
print(f"The Greatest Common Divisor (GCD) of {num1} and {num2} is: {result}")

Enter the first number: 4


Enter the second number: 2
The Greatest Common Divisor (GCD) of 4 and 2 is: 2

Program to find the LCM of two numbers

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

Program to find the LCM of two numbers by finding their GCD

def gcd(a, b):


while b != 0:
a=b
b=a%b
#a, b = b, a % b
return a

def lcm(a, b):


return (a * b) // gcd(a, b)

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


num2 = int(input("Enter the second number: "))

result = lcm(num1, num2)


print(f"The Least Common Multiple (LCM) of {num1} and {num2} is: {result}")

Enter the first number: 4


Enter the second number: 2
The Least Common Multiple (LCM) of 4 and 2 is: 4

Prime number programs

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

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")

Enter the number: 6


6 is not a prime number

Print Prime Number from 1 to n

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

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

10
20
11 13 17 19

Check whether the number is armstrong or not

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


n=len(str(num))
# initialize sum
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum = sum+ digit ** n
temp = temp // 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Enter a number: 153


153 is an Armstrong number

Print the Armstrong numbers present between the two intervals

m = int(input("Enter Starting Value"))


n = int(input("Enter Ending Vlaue"))

for num in range(m,n + 1):


order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10

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)

Enter Starting Value100


Enter Ending Vlaue2000
153
370
371
407
1634

Program to check if a given number is a strong number or not

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.

num = int(input("Enter a number you want to check: \n"))


num_of_digits = len(str(num))
square = num**2
last_digits = square%pow(10,num_of_digits)
if last_digits == num:
print("Yes, {} is an automorphic number".format(num))
else:
print("No, {} is not an automorphic number".format(num))

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 you want to check:


25
Yes, 25 is an automorphic number

check whether a number is an abundant number or not

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


total = 0
abun = 0
for i in range(1,input_no):
if(input_no % i == 0):
total = total + i
if(total > input_no):
is_abundant = 1
break
if((total > input_no) or (abun ==1)):
print("It is an abundant number.")
else :
print("It is not an abundant number.")

Enter a number : 12
It is an abundant number.

Print Fibonacci series upto n value

nterms = int(input("Enter your Fibonacci: "))


n1 = 0
n2 = 1

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

Enter your Fibonacci: 6


Fibonacci sequence:
0 1 1 2 3 5

Convert the given Binary Number into Decimal

From decimal to binary Input : 8 Output : 1 0 0 0 From

binary to decimal Input : 100 Output : 4

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()

Enter Decimal Value: 17


10001

# Python program to convert decimal into other number systems


dec = 344

print("The decimal value of", dec, "is:")


print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")

The decimal value of 344 is:


0b101011000 in binary.
0o530 in octal.
0x158 in hexadecimal.

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 = int(input("Enter the integer number: "))


revs_number = 0
while (number > 0):
remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10
print("The reverse number is : {}".format(revs_number))

Enter the integer number: 12345


The reverse number is : 54321

Solid Star Pattern

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


for i in range(n):
for j in range(n):
print("*", end=" ")
print()

Enter the number of rows: 5


* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Hollow Star Pattern

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


cols = int(input("Enter the number of columns: "))
for i in range(rows):
for j in range(cols):
if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()

Enter the number of rows: 5


Enter the number of columns: 5
* * * * *
* *
* *
* *
* * * * *

Palindrome half pyramid pattern using numbers Input: 5

(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

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


for i in range(1, n+1):
for k in range(1, i+1):
print(k, end ='')
for l in range(i-1, 0, -1):
print(l , end ='')
print('\n')

Enter the number of rows : 5


1

121

12321

1234321

123454321

Palindrome half pyramid pattern using alphabets Input: 5 (number of rows) Output: A

ABA

ABCBA

ABCDCBA

ABCDEDCBA

Palindrom Pattern printing

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


for i in range(1, n+1):
for k in range(1, i+1):
print(chr(k+65-1), end ='')
for l in range(i-1, 0, -1):
print(chr(l+65-1) , end ='')
print('\n')

Enter the number of rows : 5


A

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

Palindrome pyramid pattern using numbers

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)

Enter the number of rows for the pyramid: 5


1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Diamond pattern printing using stars

def print_diamond(rows):
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("* " * i)

for i in range(rows - 1, 0, -1):


print(" " * (rows - i), end="")
print("* " * i)

num_rows = int(input("Enter the number of rows for the diamond: "))

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

Enter the number of rows for the diamond: 5


*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

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: "))

# Call the function to print Floyd's triangle


print_floyds_triangle(num_rows)

Enter the number of rows for Floyd's triangle: 5


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

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()

num_rows = int(input("Enter the number of rows for Pascal's triangle: "))

print_pascals_triangle(num_rows)

Enter the number of rows for Pascal's triangle: 5


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Remove Vowels

string = input("Enter your Letter: ")


vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
result = ""
for i in range(len(string)):
if string[i] not in vowels:
result = result + string[i]
print("\n After removing Vowels:", result)
#def rem_vowel(string):
# vowels = ['a','e','i','o','u']
# result = [letter for letter in string if letter.lower() not in vowels]
# result = ''.join(result)
# print(result)

Enter your Letter: Tamilnadu

After removing Vowels: Tmlnd

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

def find3Numbers(A, arr_size, sum):


for i in range( 0, arr_size-2):
for j in range(i + 1, arr_size-1):
for k in range(j + 1, arr_size):
if A[i] + A[j] + A[k] == sum:
print("Triplet is", A[i],",", A[j], ",", A[k])
return True
return False
A = [1, 4, 45, 6, 10, 8]
sum = 22
arr_size = len(A)
find3Numbers(A, arr_size, sum)

Triplet is 4 , 10 , 8
True

Find the Frequency of Characters in a String

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

Program to find the largest palindrome in an array

def is_palindrome(num):
num_str = str(num)
return num_str == num_str[::-1]

def find_largest_palindrome(arr):
largest_palindrome = None

for num in arr:


if is_palindrome(num):
if largest_palindrome is None or num > largest_palindrome:
largest_palindrome = num
return largest_palindrome

array = [123, 121, 232, 4554, 909, 456]


result = find_largest_palindrome(array)

if result is not None:


print("The largest palindrome in the array is:", result)
else:
print("No palindrome found in the array.")

The largest palindrome in the array is: 4554

Colab paid products - Cancel contracts here 1s

completed at 1:27 PM

https://colab.research.google.com/drive/1Pq9Bxug3Jjt5FdISf82l-DQcq1Ngrq2W#scrollTo=18R_8wFUVIiG&printMode=true 13/

You might also like