Download Program1: Verifying the given number is even or odd #Enter a

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Halting problem wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Factorial wikipedia , lookup

Transcript
Program1: Verifying the given number is even or odd
#Enter a number to verify even or odd
num=int(input("enter any value"))
if num%2==0:
print(" The given number",num,"is even")
else:
print("The given number",num,"is odd")
program 2:# Python program to find the factorial of a number provided by the user.
# to take input from the user
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
program 3:
# Python program to find the factorial of a number using recursion
def recur_factorial(n):
"""Function to return the factorial
of a number using recursion"""
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
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))
program 4: # Python program to find the sum of natural numbers up to n using
recursive function
def recur_sum(n):
"""Function to return the sum
of natural numbers using recursion"""
if n <= 1:
return n
else:
return n + recur_sum(n-1)
# take input from the user
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
program 5: # Python program to display all the prime numbers within an interval
# to take input from the user
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
program 5: # Python program to check if the input number is prime or not
# take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
program 6: # Python program to check if the number provided by the user is an
Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
program 7: #Python Program - Calculate Grade of Student
while True:
print("Enter 'x' for exit.")
print("Enter marks obtained in 5 subjects: ")
mark1 = int(input())
mark2 = int(input())
mark3 = int(input())
mark4 = int(input())
mark5 = int(input())
if mark1 == 'x':
break
else:
sum = mark1 + mark2 + mark3 + mark4 + mark5
average = sum/5
if(average>=91 and average<=100):
print("Your Grade is A+")
elif(average>=81 and average<=90):
print("Your Grade is A")
elif(average>=71 and average<=80):
print("Your Grade is B+")
elif(average>=61 and average<=70):
print("Your Grade is B")
elif(average>=51 and average<=60):
print("Your Grade is C+")
elif(average>=41 and average<=50):
print("Your Grade is C")
elif(average>=0 and average<=40):
print("Your Grade is F")
else:
print("Strange Grade..!!")
program 8: # Python Program - Solve Quadratic Equation
import cmath
while True:
print("Enter 'x' for exit.")
num1 = input("Enter value of a: ")
num2 = input("Enter value of b: ")
num3 = input("Enter value of c: ")
if num1 == 'x':
break
else:
number1 = float(num1)
number2 = float(num2)
number3 = float(num3)
d = (number2**2) - (4*number1*number3)
r1 = (-number2-cmath.sqrt(d))/(2*number1)
r2 = (-number2+cmath.sqrt(d))/(2*number1)
print("The solutions = {0} and {1}" .format(r1,r2))