Monday, 30 January 2017

VTU Computer Programming Lab in "Python"

1) 

""" python program to Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a Quadratic equation (ax^2 +bx+c=0) as input and compute all possible roots. Implement a Python program for the developed flowchart/algorithm and execute the same to output the possible
roots for a given set of coefficients with appropriate messages.find two roots of a quadratic equation """


a=float(input("enter value of a:"))
b=float(input("enter value of b:"))
c=float(input("enter value of c:"))
disc=(b*b)-(4*a*c)
if (disc==0):
     r1=r2=-b/(2*a)
     print "roots are equal:",r1
elif (disc >0):
     print "roots are not equal"
     r1=(-b+(disc**0.5))/(2*a)
     r2=(-b-(disc**0.5))/(2*a)
     print "root1:",r1
     print "root2:",r2
else:
     print "roots are imaginary"
     r=-b/(2*a)
     i1=(abs(disc)**0.5)/(2*a)
     i2=(abs(disc)**0.5)/(2*a)
     print "root1:"+str(r)+"+i"+str(i1)
      print "root2:"+str(r)+"-i"+str(i2)


OUTPUT:
Output of Quadratic Equation

2)
"""Design and develop an algorithm to find the reverse of an integer number NUM and check whether it is PALINDROME or NOT. Implement a C program for the developed algorithm that takes an integer number as input and output the reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome"""

a=int(input("enter an interger value :"))
temp=a
rev=0
while (a>0):
      rem=a%10
      a=a/10
      rev=rev * 10+rem
print "entered number is: %d\nreversed number is: %d" %(temp,rev)
if(temp==rev):
      print "\"%d in a PALINDROME\"" %temp
else:
       print "\"%d in a NOT A PALINDROME\"" %temp


OUTPUT:



3)

"""Design and develop a C program to read a year as an input and find whether it is leap year or not. Also consider end of the centuries."""


year=int(input("enter a year:"))

if (((year%4==0) or (year%100==0)) or (year%400==0)):
    print "entered year %d is a LEAP YEAR" %year
else:
     print "entered year %d is a NOT A LEAP YEAR" %year

OUTPUT: 


4)

"""Factorial of any number between 0 to 989 using Recursive function"""
 
num=int(input("enter a integer number:"))

def fact(f):
    "this function computes factorial" #optional function description
    if f==0:
        return 1
    else:
        return f*fact(f-1)

i=0
while(i<=num):

    res=fact(i)
    print "Factorial("+str(i)+")="+str(res)
    i=i+1
     

OUTPUT:

No comments:

Post a Comment