a=int(raw_input('Enter the value of a:'))
b=int(raw_input('Enter the value of b:'))
print ' 1. add 2.sub 3.mul 4. div 5.exp'
n=int(raw_input('Enter the choice:'))
if n==1:
c=a+b
print 'Added value is:',c
elif n==2:
if a>b: #Nested if else
c=a-b
print 'Subtracted value is:',c
else:
c=b-a
print 'Subtracted value is:',c
elif n==3:
c=a*b
print 'Multiplication value is:',c
elif n==4:
if b==0: #Nested if else
print 'Division by zero not defined'
else:
c=float(a)/b
print 'Divison value is:',c
elif n==5:
c=a**b
print 'Exponent value is:',c
else:
print 'Invalid options'
Solve Problems by Coding Solutions - A Complete solution for python programming
Factorial using while loop
f=i=1
n=int(raw_input('Enter the number: '))
while i<=n:
f=f*i
i=i+1
print f
n=int(raw_input('Enter the number: '))
while i<=n:
f=f*i
i=i+1
print f
To check the number is Prime or Not
n=int(raw_input('Enter the number to check:'))
flag=0
i=2
while(i<=(n/2)):
if (n%i==0):
flag=1
i=i+1
if(flag==0):
print 'The number ',n, ' is prime number'
else:
print 'The number ',n, ' is not prime number'
flag=0
i=2
while(i<=(n/2)):
if (n%i==0):
flag=1
i=i+1
if(flag==0):
print 'The number ',n, ' is prime number'
else:
print 'The number ',n, ' is not prime number'
Factorial using Recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
Searching the character count from string
word = raw_input('Enter the String:')
char = raw_input('Enter the Character to search:')
count = 0
for letter in word:
if letter == char:
count = count + 1
print 'Number of times repeated:', count
char = raw_input('Enter the Character to search:')
count = 0
for letter in word:
if letter == char:
count = count + 1
print 'Number of times repeated:', count
Subscribe to:
Posts (Atom)