In: Computer Science
Two coding challenges this week!
Part 1: Estimate the value of e.
e is defined as as n goes to infinity. So the larger the value of n, the closer you should get to e.
math.e is defined as 2.718281828459045
You'll write a program that uses a while loop to estimate e to within some defined error range, while having a limit on how many iterations it will attempt.
Part 2: Palindrome detection
A palindrome is a string that is the same forwards and backwards.
Madam I'm Adam
Racecar
Was it a cat I saw?
There are many sophisticated ways of detecting palindromes. Some are surprisingly simple (s == s[::-1])
You will use a for loop to iterate over the contents of the string to determine if it is a palindrome.
Beware! There are many solutions online. Don't fall prey to looking this one up. It's not as hard as it looks. Draw it out on paper first and think through how you index the string from both ends.
A you didn't mentioned the programing language i did it in python
#pyhton program to find the value of e upto n decimal points
import decimal
#find the factorial of n numbers
def fact(a):
fac = [1]
for i in range(1, a + 1):
fac.append(fac[i - 1] * i)
return fac
# calculating the value of e upto n numbers
def value_of_e(a):
decimal.getcontext().prec = a + 1
e = 2
fac = fact(2 * a + 1)
for i in range(1, a + 1):
c = 2 * i + 2
d = fac[2 * i + 1]
e += decimal.Decimal(c / d) #formula to find the decimal values of
e
return e
#using while loop to print the value of e upto n numbers
while True:
a = int(input("Enter the decimal points after 2 in e : "))
if a >= 0:
break
print(str(value_of_e(a))[:a + 1])
2 .
Program to find the given string is a pallindrome or not
a = input("Enter the string : ") #Enter the string
b = "" #Create an empty array
#finding the given string is a palindrome or not using a for
loop
for j in a:
b = j+b
print("the string in reverse order is : ",b)
if (a==b):
print ("The entered string is a palindrome") #print palindrome if
the string is same
else:
print("The entered string isn't a palindrome") #print not a
palindrome if the string is different
------------------------------------------------------------------------------------------
Kindly comment your queries if any, upvote if you like it. Thank You!!