Search This Blog

Sunday, February 20, 2011

Functions


#program to calculate sin(x) using two functions power(x,n) and fact(n)
from math import pi
def power(x,n):
    if n==1:
        return x
    return x*power(x,n-1)
def fact(n):
    if n==0:
        return 1
    return n*fact(n-1)

x=raw_input("Enter angle in degrees  ")
xr=float(x)*pi/180.0
term=xr
sum=0
n=3
sign=1
while term>0.00001:
    sum+=sign*term
    term=power(xr,n)/fact(n)
    n+=2
    sign=-sign
print "sin(",x,")=",sum

No comments:

Post a Comment