Python Programs-1
Python Programs-1
def factorial(n):
fact = 1
for num in range(2, n + 1):
fact *= num
return fact
def factorial(n):
if n == 1: #base case
return n
else:
return n*factorial(n-1) # recursive case
number = 4
if number < 0:
print("Factorial exists for positive numbers only")
elif number == 0:
print("Factorial of 0 : 1")
else:
print("Factorial of",number,":",factorial(number))
sum = 0
for digit in str(n):
sum += int(digit)
return sum
n = 12345
print(getSum(n))
for i in range(3):
for j in range(3):
for k in range(3):
# Driver Code
comb([1, 2, 3])
Python Program for Efficient program to print all prime factors of a
given number
def primeFactors(n):
# Condition if n is a prime
# number greater than 2
if n > 2:
print n
n = 315
primeFactors(n)
Python program to calculate the number of digits and letters in a
string
all_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
# given string
string = "geeks2for3geeks"
# intialized value
total_digits = 0
total_letters = 0
ANOTHER APPROACH:
string = "python1234"
# intialized value
total_digits = 0
total_letters = 0
# Driver Code
n = 193202042
print(getSortedNumber(n))
def findLen(str):
counter = 0
while str[counter:]:
counter += 1
return counter
str = "geeks"
print(findLen(str))
num = 29
# initialize sum
sum = 0
lower = 900
upper = 1000
# define a function
def compute_hcf(x, y):
num1 = 54
num2 = 24
Occurrence of a Character in
String
count = 0
my_string = "Programiz"
my_char = "r"
for i in my_string:
if i == my_char:
count += 1
print(count)
def countdown(time_sec):
while time_sec:
mins, secs = divmod(time_sec, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(1)
time_sec -= 1
print("stop")
countdown(5)
Python Program to Check If Two
Strings are Anagram
str1 = "Race"
str2 = "Care"
else:
print(str1 + " and " + str2 + " are not anagram.")
result = 1
while exponent != 0:
result *= base
exponent-=1
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
*
* *
* * *
* * * *
* * * * *
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print("\n")
# string of vowels
vowels = 'aeiou'
print(count)
# Program to sort alphabetically the words form a string provided by
the user
my_str = 'aIbohPhoBiA'
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# Driver Code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
Ans = largest(arr,n)
print ("Largest in given array is",Ans)
# initialize a variable
# to store the sum
# while iterating through
# the array later
sum=0
return(sum)
# driver function
arr=[]
# input values to list
arr = [12, 3, 4, 15]
ans = _sum(arr)
# display sum
print ('Sum of the array is ', ans)
def rev_sentence(sentence):
# then reverse the split string list and join using space
reverse_sentence = ' '.join(reversed(words))
if __name__ == "__main__":
input = 'geeks quiz practice code'
print (rev_sentence(input))