forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Program for finding the HCF,LCM and Palindrome using and recursion an…
…d non recursion
- Loading branch information
Shivam Arora
committed
Nov 23, 2018
1 parent
11d0d64
commit 768a39d
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Program to find the HCF of two Numbers | ||
def find_hcf(num_1, num_2): | ||
if num_1 == 0: | ||
return num_2 | ||
if num_2 == 0: | ||
return num_1 | ||
# Base Case | ||
if num_1 == num_2: | ||
return num_1 | ||
if num_1 > num_2: | ||
return find_hcf(num_1 - num_2, num_2) | ||
return find_hcf(num_1, num_2 - num_1) | ||
|
||
|
||
def main(): | ||
num_1 = 24 | ||
num_2 = 34 | ||
print('HCF of %s and %s is %s:' % (num_1, num_2, find_hcf(num_1, num_2))) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def find_lcm(num_1, num_2): | ||
max = num_1 if num_1 > num_2 else num_2 | ||
while (True): | ||
if ((max % num_1 == 0) and (max % num_2 == 0)): | ||
break | ||
max += 1 | ||
return max | ||
|
||
|
||
def main(): | ||
num_1 = 12 | ||
num_2 = 76 | ||
print(find_lcm(num_1, num_2)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Program to find whether given string is palindrome or not | ||
def is_palindrome(str): | ||
start_i = 0 | ||
end_i = len(str) - 1 | ||
while start_i < end_i: | ||
if str[start_i] == str[end_i]: | ||
start_i += 1 | ||
end_i -= 1 | ||
else: | ||
return False | ||
return True | ||
|
||
|
||
# Recursive method | ||
def recursive_palindrome(str): | ||
if len(str) <= 1: | ||
return True | ||
if str[0] == str[len(str) - 1]: | ||
return recursive_palindrome(str[1:-1]) | ||
else: | ||
return False | ||
|
||
|
||
def main(): | ||
str = 'ama' | ||
print(recursive_palindrome(str.lower())) | ||
print(is_palindrome(str.lower())) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |