0% found this document useful (0 votes)
5 views

PYTHON-ASSIGNMENT

The document contains a series of Python programs that demonstrate various programming concepts, including basic arithmetic operations, prime number checks, simple interest calculations, temperature conversions, and Fibonacci series generation. Each program includes user input and outputs results based on the computations performed. The examples illustrate fundamental programming techniques suitable for beginners.

Uploaded by

inosukezz167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PYTHON-ASSIGNMENT

The document contains a series of Python programs that demonstrate various programming concepts, including basic arithmetic operations, prime number checks, simple interest calculations, temperature conversions, and Fibonacci series generation. Each program includes user input and outputs results based on the computations performed. The examples illustrate fundamental programming techniques suitable for beginners.

Uploaded by

inosukezz167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

#1.Python program to Add Two Numbers with User Input.

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

sum = float(num1) + float(num2)

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

#2. Write a program to create a simple calculator performing only four basic operations.

val1 = float(input("Enter value 1: "))


val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1/val2
else:
print("Wrong input,program terminated")
print("The result is ",result)

#3. Write a Python program to check if a given number is prime or not

num = int(input("Enter the number to be checked: "))


flag = 0
if num > 1 :
#presume num is a prime number
for i in range(2, int(num / 2)):
if (num % i == 0):
flag = 1 #num is a not prime number
break #no need to check any further
if flag == 1:
print(num , "is not a prime number")
else:
print(num , "is a prime number")
#4.Write a program to find Simple Interest.

p=int(input("Enter the principle amount"))

r=int(input("Enter the rate of interest"))

t=int(input("Enter the time”"))

si=(p*r*t)/100

print("Simple Interest = ",si)

#5.Write a program to find area of a circle.

r= float(input("Enter the radius of circle : "))

area_of_circle= 22/7*r*r

print("Area of circle = ", area_of_circle)

#6. Write a program to convert temperature from Celsius to Fahrenheit.

C= float(input("Enter the temperature in celsius : "))

F=(C*9/5)+32

print("The temperature in Fahrenheit is = ", F)

#7. Write a program to convert temperature from Fahrenheit to Celsius.

fahrenheit = float(input("Enter temperature in Fahrenheit: "))

celsius = (fahrenheit - 32) * 5/9

print("he temperature in celsius is = ", celsius)

#8. Write a program to find the grade of a student based upon his percentage based upon
following conditions.
Percentage >= 80 A+ grade
Percentage>= 60 A grade
Percentage >= 50 B grade
Percentage >=40 C grade
Percentage>=35 D grade
Percentage>=0 F grade
Other : invalid percentage entered

percentage= float(input("Enter your percentage : "))


if percentage>=80 and percentage<=100 :
print("Your grade is A+")
elif percentage>=60:
print("Your grade is A")
elif percentage>=50:
print("Your grade is B")
elif percentage>=40:
print("Your grade is C")
elif percentage>=35:
print("Your grade is D")
elif percentage>=0:
print("Your grade is F")
else :
print("You have entered a wrong percentage")

#9. Write a program to find the largest number among 3 numbers.

num1=int(input("Enter 1st number : "))


num2=int(input("Enter 2nd number : "))
num3=int(input("Enter 3rd number : "))
if num1>num2 :
if num1>num3:
print (num1 , "is greatest")
else :
print(num3, "is greatest")
if num2>num3:
print (num2 , "is greatest")
else:
print(num3, "is greatest")

#10.Program to find prime numbers between 2 to 50 using nested for loops

num = 2
for i in range(2, 50):
j= 2
while ( j <= (i/2)):
if (i % j == 0):
break
j += 1
if ( j > i/j) :
print ( i, "is a prime number")
print ("Bye Bye!!")

#11.Write a program to calculate the factorial of a given number.

num = int(input("Enter a number: "))


fact = 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):

fact = fact * i

print("factorial of ", num, " is ", fact)

#12.Write a program to find roots of a quadratic equation.

print("Equation: ax^2 + bx + c ")

a=int(input("Enter a: "))

b=int(input("Enter b: "))

c=int(input("Enter c: "))

d=b**2-4*a*c

d1=d**0.5

if(d<0):

print("The roots are imaginary. ")

else:

r1=(-b+d1)/2*a

r2=(-b-d1)/2*a

print("The first root: ",round(r1,2))

print("The second root: ",round(r2,2))

#13.Write a Python Program to find the sum of digits in a number.

n=int(input("Enter a number:"))

tot=0
while(n>0):

dig=n%10

tot=tot+dig

n=n//10

print("The total sum of digits is:",tot)

#14.Write a program to print Fibonacci Series.

nterms = int(input("How many terms? "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

#15.Write a Python Program to find the fibonacci series using while loop.

a=int(input("Enter the first number of the series "))

b=int(input("Enter the second number of the series "))


n=int(input("Enter the number of terms needed "))

print(a,b,end=" ")

while(n-2):

c=a+b

a=b

b=c

print(c,end=" ")

n=n-1

#16.Write a Python Program to find the fibonacci series using recursion.

def fibonacci(n):

if(n <= 1):

return n

else:

return(fibonacci(n-1) + fibonacci(n-2))

n = int(input("Enter number of terms:"))

print("Fibonacci sequence:")

for i in range(n):

print(fibonacci(i))

#17. Write a program to Print nth Fibonacci Number using Dynamic Programming
(Bottom-Up Approach)

def fibonacci(n):
"""Return the nth Fibonacci number."""
if n == 0:
return 0

# r[i] will contain the ith Fibonacci number


r = [-1]*(n + 1)
r[0] = 0
r[1] = 1

for i in range(2, n + 1):


r[i] = r[i - 1] + r[i - 2]
return r[n]

n = int(input('Enter n: '))

ans = fibonacci(n)
print('The nth Fibonacci number:', ans)

#18.Write a Python Program to read a number n and print the natural numbers
summation pattern.

n=int(input("Enter a number: "))

for j in range(1,n+1):

a=[]

for i in range(1,j+1):

print(i,sep=" ",end=" ")

if(i<j):

print("+",sep=" ",end=" ")

a.append(i)

print("=",sum(a))

print()

You might also like