Skip to main content

Posts

Showing posts from 2019

Example displaying two histograms and their ratio

// Example displaying two histograms and their ratio. void ratioplot() { // Define two gaussian histograms. Note the X and Y title are defined // at booking time using the convention "Hist_title ; X_title ; Y_title" TH1F *h1 = new TH1F ( "h1" , "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms" , 100, -5, 5); TH1F *h2 = new TH1F ( "h2" , "h2" , 100, -5, 5); h1-> FillRandom ( "gaus" ); h2-> FillRandom ( "gaus" ); // Define the Canvas TCanvas *c = new TCanvas ( "c" , "canvas" , 800, 800); // Upper plot will be in pad1 TPad *pad1 = new TPad ( "pad1" , "pad1" , 0, 0.3, 1, 1.0); pad1-> SetBottomMargin (0); // Upper and lower plot are joined pad1-> SetGridx (); // Vertical grid pad1-> Draw (); // Draw the upper pad: pad1 pad1-> cd (); // pad1 becomes the c...

Program to read numbers from a file and determine histogram of the data

#Program to read numbers from a file and determine histogram of the data: f1=open("hg3.d","r") n=input("Enter the no. of lines:") n=int(n) h=[0 for i in range (n)] x=[] for i in f1:  x.append(eval(i)) t=len(x) mn=min(x) mx=max(x) R=(mx-mn)*1.01 bs=R/n for i in range (t):  k=int(abs((h[i]-mn)/bs))  h[k]=h[k]+1.0  f=[] for i in range (n):  d=mn+(i*bs)  f.append(d) f1.close() f2=open("hg2.d","w") a=len(f) for i in range (a):  print(h[i],f[i],"-",f[i]+bs,file=f2) f2.close

Program to implement 2D Newton Raphson method to find intersecting points

#Program to implement 2D Newton Raphson method to find intersecting points for # i) x^2 + y^2 = 4 # ii) (x-1)^2 + y^2 = 4 from math import* def f(x,y):  y=x**2+y**2-4  return y def g(x,y):  y=(x-1)**2+y**2-4  return y def fx(x,y):  y=2*x  return y def gx(x,y):  y=2*(x-1)  return y def fy(x,y):  y=2*y  return y def gy(x,y):  y=2*y  return y x=float(input("Enter the initial guess,x0=")) y=float(input("Enter the initial guess,y0=")) s=0 det_J_inv=fx(x,y)*gy(x,y)-fy(x,y)*gx(x,y) if det_J_inv==0.0:  print("Enter new guess values.") else:  while abs(f(x,y))>.000000001 and abs(g(x,y))>.000000001 and s<50:   h=(f(x,y)*gy(x,y)-fy(x,y)*g(x,y))/float(det_J_inv)   k=(-gx(x,y)*f(x,y)+g(x,y)*fx(x,y))/float(det_J_inv)   x=x-h   y=y-k   s=s+1 print("The root is located at:\nx=",x,"y=",y,"\nRequired No. of iterations:",s)

Program to diagonalise a given matrix using Gauss Jordan method

#Program to diagonalise a given matrix using Gauss Jordan method a=[] n=input("Enter the dimension of the matrix:") n=int(n) for i in range(n):  r=input("Enter the elements for row "+str(i+1)+" :").split(",")  r=list(map(float,r))  a.append(r) print("The entered matrix is:") for i in range(n):  print(a[i]) for c in range(0,n):  for r in range(0,n):   if r!=c:    l=a[r][c]/a[c][c]    for k in range(n):     a[r][k]=a[r][k]-l*a[c][k] print("The diagonalised matrix is:") for i in range(n):  print(a[i])

Program to convert a matrix to a upper triangular form using Gauss elimination method and to find the determinant of the matrix.

# Program to convert a matrix to a upper triangular form using Gauss elimination method and to # find the determinant of the matrix. a=[] n=input("Enter the dimension of the matrix:") n=int(n) for i in range(n):  r=input("Enter the elements of row "+str(i+1)+" :").split(",")  r=list(map(float,r))  a.append(r) print("The entered matrix is:") for i in range(n):  print (a[i]) for c in range(n-1):  for r in range(c+1,n):   l=a[r][c]/a[c][c]   for k in range(n):    a[r][k]=a[r][k]-l*a[c][k] s=1.0 print("The entered matrix in upper triangular form is:") for i in range(n):  print(a[i])  s=a[i][i]*s print ("The determinant of the matrix is:",s)

Program to evaluate integration using Monte carlo method (four leaf rose):

#Program to evaluate integration using Monte carlo method (four leaf rose): from math import* def f(x):  y=sin(2*x)  return y import random as rn nt=input("Enter total no. darts:") nt=int(nt) ns=0.0 for i in range (nt):  x=rn.random()  y=rn.random()  x1=-1+2*x  y1=-1+2*y  r=sqrt(x1**2+y1**2)  o=atan2(y1,x1)  if r<=abs(f(o)):   ns=ns+1 A=ns/nt*4 print("The value of the integration is:",A)

Program to evaluate integration using monte carlo method:

#Program to evaluate integration using monte carlo method: from math import* def f(x):  y=(1-x*x)*exp(-x*x)  return y import random as rn a=input("Enter the lower limit:") b=input("Enter the upper limit:") nt=input("Enter the total no. darts:") n=input("Enter the no. of divisions:") a=float(a) b=float(b) n=int(n) nt=int(nt) dell=(b-a)/(n*1.0) k=[] for i in range (n):  j=a+i*dell  u=f(j)  k.append(u) h1=max(k)*1.2 s=min(k)*1.2 h2=abs(s) ns=0.0 for i in range(nt):  x=rn.random()  y=rn.random()  x1=a+(b-a)*x  y1=-h2+(h1+h2)*y  y2=f(x1)  if y1<=y2:   ns=ns+1 A=ns/nt*(h1+h2)*(b-a)-h2*(b-a) print("The value of the integration is:",A)

Program to evaluate integration using Monte carlo method:

#Program to evaluate integration using Monte carlo method: from math import* def f(x):  y=exp(x*x)  return y import random as rn a=input("Enter the lower limit:") b=input("Enter the upper limit:") nt=input("Enter the total no. darts:") n=input("Enter the no. of divisions:") a=float(a) b=float(b) n=int(n) nt=int(nt) dell=(b-a)/(n*1.0) k=[] for i in range (n):  j=a+i*dell  u=f(j)  k.append(u) h1=max(k) h=h1*1.2 ns=0.0 for i in range (nt):  x=rn.random()  y=rn.random()  x1=a+(b-a)*x  y1=h*y  y2=f(x1)  if y1<=y2:   ns=ns+1 A=ns/nt*h*(b-a) print("The value of the intergration is:",A)

Program to determine the value of slope by fitting the supplied data with a theoritical

# Program to determine the value of slope by fitting the supplied data with a theoritical # function y=mx+c f1=open("lin1.d","r") f2=open("least.d","w") b=0 a=0 q1=0 q2=0 y1=[] x=[] y=[] for k in f1:  z=k.split()  x.append(eval(z[0]))  y.append(eval(z[1])) print("The entered values of the x's are:\n",x,"\nThe entered values of the y's are:\n",y) n=len(x) for i in range (0,n):  b=b+x[i]  a=a+x[i]*x[i]  q1=q1+x[i]*y[i]  q2=q2+y[i] m=(q1*n-q2*b)/(a*n-b*b) c=(a*q2-b*q1)/(a*n-b*b) print("The value of the slope is:",m,"\nThe value of the c is:",c) for i in range(0,n):  k=m*x[i]+c  y1.append(k)  print(x[i],y[i],y1[i],file=f2) f2.close()

Program to define a function which calculates the trace of a matrix:

#Program to define a function which calculates the trace of a matrix: def imatrix(m):  A=[]  for i in range(m):   a=input("Enter the elements of row "+str(i+1)+" :").split(",")   b=list(map(float,a))   A.append(b)  return A def showm(A):  for i in range (m):   print (A[i]) def tracem(A):  k=0  for i in range (m):   k=k+A[i][i]  return k m=input("Enter the dimension of the matrix:") m=int(m) X=imatrix(m) print("The entered matrix is:") showm(X) Y=tracem(X) print ("The trace of the matrix is:",Y)

Program to find root using secant method for f(x)=x-3(1-exp(-x))

#Program to find root using secant method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the 1st value:") b=input("Enter the 2nd value:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0 s=0 while abs((c-b)/c)>0.00000000001 and s<500:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0  s=s+1 print("The root for the given function is =",c,"\nThe No. of steps required:",s) print("Function's value at point of root=",f(c))

Program to find root by Newton-Raphson method for y=x-tan(x):

#Program to find root by Newton-Raphson method for y=x-tan(x): from math import* def f(x):  y=x-tan(x)  return y def f1(x):  y1=1-1/(cos(x)*cos(x))  return y1 x=input("Enter the initial guess,x0=") x=float(x) s=0 while abs(f(x))>0.000001 and s<500:  h=float(f(x))/float(f1(x))  x=x-h  s=s+1 print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)

Program to find root using Regula Falsi method for f(x)=x-tan(x)

#Program to find root using Regula Falsi method for f(x)=x-tan(x) from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the function does not lie between the limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The solution of the function is=",c,"\nRequired No. of steps:",s) print("The value of the function at the point of solution is=",f(c))

Program to find root using Bisection method for f(x)=x-tan(x):

#Program to find root using Bisection method for f(x)=x-tan(x): from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root for the given function does not lie between the limits.")  a=input("Enter new lower limit:")  b=input("Enter new upper limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.0000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("the root of the function is=",c,"\nThe required No. of steps:",s) print("The value of the function at point of root is=",f(c))

Program to find roots by using Newton Raphson for f(x)=x-3(1-exp(-x))

#Program to find roots by using Newton Raphson for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y def f1(x):  y=1-3*exp(-x)  return y x=input("Enter the initial guess value,x0=") x=float(x) s=0 while abs(f(x))>.000001 and s<50:  h= f(x)/float(f1(x))  x=x-h  s=s+1 print("The value of the root for the function is:",x,"\nRequired No. of iteration:",s)

Program to find root using secant method for f(x)=x-3(1-exp(-x))

#Program to find root using secant method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the 1st value:") b=input("Enter the 2nd value:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0 s=0 while abs(f(c))>0.00000000001 and s<50:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))  s=s+1 print("The root for the given function is =",c,"\nThe No. of steps required:",s) print("Function's value at point of root=",f(c))

Program to find root using Regula Falsi method for f(x)=x-3(1-exp(-x))

#Program to find root using Regula Falsi method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root for the given function does not exist between the entered limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The solution of the entered function is =",c,"\nRequired No. of steps:",s) print("The value of the function at the point of solution is=",f(c))

Program to find root using Bisection method for y=x-3(1-exp(-x)):

#Program to find root using Bisection method for y=x-3(1-exp(-x)): from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print ("Root of this given function does not exist between the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.00000000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The root of the given function is=",c,"\nRequired No. of steps:",s) print("The value of the function at the point of root is=",f(c))

Program to find root by Newton-Raphson method for y=x-tanh(2x):

#Program to find root by Newton-Raphson method for y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y def f1(x):  y1=1-(2/(cosh(2*x)*cosh(2*x)))  return y1 x=input("Enter the initial guess,x0=") x=float(x) s=0 while abs(f(x))>0.000001 and s<500:  h=float(f(x))/float(f1(x))  x=x-h  s=s+1 print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)

Program to find root by secant method for y=x-tanh(2x):

#Program to find root by secant method for y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))* 1.0 s=0 while abs(f(c))>0 & s<50:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))  s=s+1 print("The root of the function is=",c,"\nThe No. of required steps:",s) print("Function's value at the point of root=",f(c))

Program to find root by regula falsi method for function y=x-tanh(2x):

#Program to find root by regula falsi method for function y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the value of upper limit:") b=input("Enter the value of lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The roots of the given function does not lie between the entered limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print ("The solution of the function is=",c, "\nThe required No. of steps:",s) print ("The value of the function at the point of solution is=",f(c))

Program to find root by Bisection method for the function y=x-tanh(2x)

#Program to find root by Bisection method for the function y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the given function does not lie between the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) s=0 while(f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.00000000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print ("The root of the given function is=",c,"\nRequired No. of steps:",s) print ("The value of the function at the point of root is=",f(c))

Program to find root of a function by Bisection method (y=x^2-x-2)

#Program to find root of a function by Bisection method (y=x^2-x-2): def f(x):  y=x*x-x-2  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the given function does not lie in the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) while(f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.0000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c print("The root of the given function is:",c)

Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error:

#Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error: def f(x):  y=1-exp(-x)  return y a=0 b=1 n=input("Enter the no. of divisions:") n=int(n) h=(b-a)/(n*1.) s=0 from math import* for i in range(1,n):  s=s+f(a+i*h) y=(h/2)*(f(a)+f(b)+2*s) print("The value of integration using trapezoidal rule is:",y) r=abs(exp(-1)-y) print("Error obtained in trapezoidal method is:",r) z=0 c=0 for i in range(1,n,2):  z=z+f(a+i*h) for i in range(2,n-1,2):  c=c+f(a+i*h) g=(h/3)*(f(a)+f(b)+4*z+2*c) print("The value of integration using Simpson's 1/3 rule is:",g) v=abs(exp(-1)-g) print("Error obtained in Simpson's 1/3 method is:",v)

Program to evaluate the integration using Simpson's (1/3) rule for f(x)=1-exp(-x):

# Program to evaluate the integration using Simpson's (1/3) rule for f(x)=1-exp(-x): f1=open("le.d","w") from math import* def f(x):  y=1-exp(-x)  return y a=0 Y=[] B=[] V=[] h=0.001 for n in range(0,5001,50):  b=a+n*h  v=b+exp(-b)-1  p=0  q=0  for j in range(1,n,2):   p=p+f(a+j*h)  for k in range(2,n-1,2):   q=q+f(a+k*h)  y=h/3*(f(a)+f(b)+4*p+2*q)  print(b,y,file=f1)  Y.append(y)  B.append(b)  V.append(v) f1.close() import matplotlib.pyplot as plt plt.plot(B,Y,'r',B,V,'g') #plt.show()

Program to evaluate integration using Trapezoidal rule

#Program to evaluate integration using Trapezoidal rule from math import* def f(x):  y=exp(-x*x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") n=input("Enter the no. of divisions:") a=float(a) b=float(b) n=int(n) h=(b-a)/(n*1.) s=0 for i in range(1,n):  s=s+f(a+i*h) y=(h/2)*(f(a)+f(b)+2*s) print("The value of the integration is:",y)

Program to define a function which calculates the transpose of a square matrix

#Program to define a function which calculates the transpose of a square matrix and test it with main. def imatrix(m):  A=[]  for i in range(m):   a=input("Enter the data of row "+str(i+1)+" of the matrix:").split(",")   b=list(a)   A.append(b)  return A def showm(A):  m=len(A)  for i in range (m):   print(A[i]) def transm(A):  m=len(A)  n=len(A[0])  B=[[0 for i in range (m)] for j in range (n)]  for i in range (m):   for j in range (n):    B[j][i]=A[i][j]  return B m=input("Enter the dimension of the square matrix:") m=int(m) X=imatrix(m) print("The matrix is:") showm(X) print("Transpose of the matrix is:") Y=transm(X) showm(Y)

Program to read number from file containing some repetition.Find the frequency distribution

# Program to read number from file containing some repetition.Find the frequency distribution and # hence the mode of the distribution.write the frequency distribution in a different file. f1=open("fd.d","w") n=input("Enter the no. of dimention:") n=int(n) x=[] for i in range (n):  s=input("Enter the value:")  s=float(s)  x.append(s) for i in range (n):  for j in range (i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a) print("The list of numbers in ascending series=",x,file=f1) f1.close() f1=open("fd.d","r") V=[x[0]] F=[] f=1 for i in range (1,n):  if x[i]==V[-1]:   f=f+1  else:   F.append(f)   f=1   V.append(x[i]) F.append(f) s=len(V) e=[] for i in range(1,s-1):  if F[i]>F[i-1] and F[i]>F[i+1]:   e.append(V[i]) f2=open("fd2.d","w") for k in range (s):  print("For entered value",V[k],"the corresponding frequency is=",F[k],file=f2) print("The mode of the dist...

Program to read names, surnames from a file and arrange them in alphabetical order of surnames

# Program to read names, surnames from a file and arrange them in alphabetical order of surnames # and write them in an another file. n=input("Enter the no of peoples:") n=int(n) f1=open("name.d","w") for i in range (n):  s=input("Enter the name of people "+str(i+1)+":")  print(s,file=f1) f1.close() f1=open("name.d","r") name=[] sname=[] for k in f1:  x=k.split()  name.append(x[0])  sname.append(x[1]) for j in range (n):  for i in range(n-j-1):   if sname[i]>sname[i+1]:    sname[i],sname[i+1]=sname[i+1],sname[i]    name[i],name[i+1]=name[i+1],name[i] f2=open("sname_arranged.d","w") for k in range (n):  print(name[k],sname[k],file=f2) f2.close()

Program to determine the median of n given numbers:

#Program to determine the median of n given numbers: n=input("Enter the no. of terms:") n=int(n) x=[] for i in range (0,n):  m=input("enter the no."+str(i+1)+":")  m=float(m)  x.append(m) for i in range (0,n):  for j in range(i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a) print("Sorted elements in ascending order are:\n",x) if n%2==0:  c=(x[int(n/2)]+x[int((n/2)-1)])/2.0 else:  c=x[int((n-1)/2)] print("Median of the given numbers is:",c)

Program to implement Insertion sort algorithm:

#Program to implement Insertion sort algorithm: n=input("Enter the no. of terms:") n=int(n) x=[] m=0 for i in range (0,n):  m1=input("Enter the no."+str(i+1)+":")  m1=float(m1)  x.append(m1) for i in range (0,n):  for j in range(i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a)    m=m+1 print("Sorted elements in ascending order are\n",x,"\nNo. of swapping required:",m)

Program to implement the Selection sort algorithm:

# Program to implement the Selection sort algorithm: n=input("Enter the no. of terms:") n=int(n) x=[] m=0 for i in range (0,n):  m1=input("Enter the no."+str(i+1)+":")  m1=float(m1)  x.append(m1) for i in range (0,n):  a=i  s=x[i]  for j in range (i+1,n):   if s>x[j]:    a=j    s=x[j]  t=x[i]  x[i]=x[a]  x[a]=t  m=m+1 print("Selection sort of entered no. in ascending order is:\n",x,"\nNo of steps required:",m)

Program to arrange n numbers in ascending order using Bubble sort algorithm:

#Program to arrange n numbers in ascending order using Bubble sort algorithm: n=input("Enter the no. of data:") n=int(n) x=[] s=0 for i in range(n):  m=input("Enter the number "+str(i+1)+":")  m=float(m)  x.append(m) for i in range(0,n,1):   for j in range (0,n-i-1,1):     if x[j]>x[j+1]:       x[j],x[j+1]=x[j+1],x[j]       s=s+1 print("The entered numbers in ascending order is:\n",x,"\nNo. of swapping required:",s)

Program to read two vectors of dimension n and determine the sum and dot product of them:

# Program to read two vectors of dimension n and determine the sum and dot product of them: n=input("Enter the number of dimension:") n=int(n) x=[] y=[] z=[] t=0 for i in range(1,n+1):  s=input("Enter the component "+str(i)+" of the first vector:")  s=float(s)  x.append(s) for i in range(1,n+1):  d=input("Enter the component "+str(i)+" of the second vector:")  d=float(d)  y.append(d) for i in range(0,n):  c=x[i]+y[i]  z.append(c) for i in range(0,n):  p=x[i]*y[i]  t=t+p print("The sum of the entered vectors are:",z) print("The dot product of the entered vectors are:",t)

Program to perform tasks similar to the string method: count() and find():

#Program to perform tasks similar to the string method: count() and find(): x=input("Enter the string:") y=input("Enter the character you want to find:") z=len(x) k=0 for i in range(0,z):  if x[i]==y:   k=k+1   if k==1:    z=i print("The total no. of occurance is:",k) print("The first occureuce position is in:",z+1)

Program to determine the value of golden ratio (phi)

#                                                                                                    t_n+1 # Program to determine the value of golden ratio (phi) = lim   --------- #                                                                               ...

Program to verify if a given number belongs to Fibonacci sequence or not:

# Program to verify if a given number belongs to Fibonacci sequence or not: n=input("Enter a number to check if it belongs to Fibonacci series or not:") a=1 b=1 c=0 c=a+b n=int(n) while c<=n:  a=b  b=c  c=a+b if b==n:  print("Entered number belongs to Fibonacci series.") else:  print("Entered number does not belong to Fibonacci series.")

Program to find sum of the series i) 1/r and ii) 1/r^2 for n = 10,100,1000,10000,100000,1000000

# Program to find sum of the series i) 1/r and ii) 1/r^2 for n = 10,100,1000,10000,100000,1000000 n=input("Enter the no. of terms:") n=int(n) s=0 s1=0 for i in range (1,n):   s=s+1/(float(i))   s1=s1+(1/float(i))**2 print("The sum of 1/r series is=",s) print("The sum of 1/r^2 series is=",s1)

Program to input from the user, the market price per unit of purchased items and unit purchased

# Program to input from the user, the market price per unit of purchased items and unit purchased # until the user asks to stop. Calculate the total price. Give discount of 5% if the total price # is between 100 to 500, 10% if that between 500 to 1500,15% for that between 1500 to 3000 and 25% # if the total price is above 3000.Output the total price to be paid. z=input("Enter the no. of items:") z=int(z) c=0.0 if z>0:  for n in range (1,z+1):   y=input("Market price per unit of the item no."+str(n)+":")   y=float(y)   c=y+c  print("Total amount=",c)  if c>=100 and c<=500:   d=c*(100-5)/100  if c>=500 and c<=1500:   d=c*(100-10)/100  if c>=1500 and c<=3000:   d=c*(100-15)/100  if c>=3000:   d=c*(100-25)/100  print("The amount to be paid=",(round(d,2)))  print("Paid.")  print("Thank you, visit again.") else :  print("You have not purchased any item. Thank you, visit again.")

Program to input six components of two vectors and to determine

# Program to input six components of two vectors and to determine # i) the angle between them in degrees # ii) the norm of the difference vector # iii) if these vectors are OA and OB, find the area of the triangle AOB. # Co-ordinate of O is (0,0,0) # iv) find the another co-ordinate of the vertex of parallelogram whose three vertices are O, A, B. x1,y1,z1=input("Enter the components of vector A:").split(",") x2,y2,z2=input("Enter the components of vector B:").split(",") x1=float(x1) y1=float(y1) z1=float(z1) x2=float(x2) y2=float(y2) z2=float(z2) from math import* norm1=sqrt(x1**2+y1**2+z1**2) norm2=sqrt(x2**2+y2**2+z2**2) angle=degrees(acos((x1*x2+y1*y2+z1*z2)/(norm1*norm2))) print("The angle between these two vectors are:",angle) a=x1-x2 b=y1-y2 c=z1-z2 n=sqrt(a**2+b**2+c**2) print("The norm of the difference vector is:",n) s=sin(radians(angle)) area=(norm1*norm2)*s/2 print("The area of triangle AOB is:",area) x=x...

Program to input a vector and determine i) the norm and ii)angle with each of the axes

# Program to input a vector and determine i) the norm and ii)angle with each of the axes x,y,z =input("Enter the values of x, y and z:").split(",") x=float(x) y=float(y) z=float(z) from math import * n=sqrt(x**2+y**2+z**2) x1=degrees(acos(x/n)) y1=degrees(acos(y/n)) z1=degrees(acos(z/n)) print("Norm of the entered vector=",n) print("Angle made with x axis in degrees:",x1) print("Angle made with y axis in degrees:",y1) print("Angle made with z axis in degrees:",z1)

Program to find solution of a quadratic equation

# Program to find solution of a quadratic equation a,b,c=input("Enter the values of a,b,c:").split(",") a=float(a) b=float(b) c=float(c) dis=b*b-4*a*c from math import * if dis>=0:  x1=(-b+sqrt(dis))/(2*a)  x2=(-b-sqrt(dis))/(2*a)  print("The solutions for the given equation are ",x1,"and",x2) else:  from cmath import *  xr=-b/(2*a)  xi=sqrt(dis)/(2*a)  print("The 1st solution for the given equation is ",xr,"+",xi)  print("The 2nd solution for the given equation is ",xr,"-",xi)

Program in fortran to find the value of integration of the function [(1-x^2)^1/2]*cosx by Trapezoidal rule

!       Program in fortran to find the value of integration of the !       function [(1-x^2)^1/2]*cosx by Trapezoidal rule         write(*,*)'Enter the lower and upper limits respectively:'         read(*,*)a,b         Write(*,*)'Enter the number of sub intervals (n):'         read(*,*)n         sum=f(a)+f(b)         h=(b-a)/n         do i=1,n-1         sum=sum+2.0*f(a+i*h)         end do         sum=sum*h/2.0         write(*,11)sum 11    format(2x,'The value of the integration corrected upto six decimal places is',f16.6)  ...

Program to find the mean, median and mode of n numbers

C      Program to find the mean, median and mode of n numbers         dimension a(100)         sum=0.0         write(*,*)'Enter the no. of the data:'         read(*,*)n         write(*,*)'Now, enter the data:'         do i=1,n         read(*,*)a(i)         sum=sum+a(i)         end do         rmean=sum/n         do i=1,n-1         do j=i+1,n         if(a(i).gt.a(j))then         temp=a(i)         a(i)=a(j)        ...

Program to find the sum of the e^x series with an error < 10^5

C      Program to find the sum of the e^x series with an error < 10^5         write(*,*)'Enter the value of x:'         read(*,*)x         sum=0.0         term=1.0         i=1 10    sum=sum+term         term=x**i/fact(i)         k=k+1         i=i+1         if(abs(term).gt..00001)goto 10         write(*,20)sum,x  20   format(2x,'The sum of the series is:'f16.4,' for x=',f16.4)         end C      Program sub function         function fact(i)         fact=1.0 ...

Program to find the sum of the cube of the digits of a three digit number

C      Program to find the sum of the cube of the digits of a C      three digit number         write(*,*)'Enter a three digit number:'         read(*,*)n         m=n         r=mod(n,10)         n=n/10         i=mod(n,10)         n=n/10         write(*,*)'The digits of the number',m,'is',n,',',i,',',r         k=n**3+i**3+r**3         write(*,*)'The sum of the cube of these digits is',k         end

Program to find the square root of a real number

C      Program to find the square root of a real number         real::a         write(*,*)'Input the number:'         read(*,*)a         if (a.ge.0.0)then         c=sqrt(a)         write(*,10)c 10    Format(2x,'The square root of the number is',f16.4)         else         d=sqrt(-a)         write(*,20)d  20   Format(2x,'The square root of the number is i',f16.4)         end if         end

Program to test whether a year is leap year or not

C      Program to test whether a year is leap year or not         write(*,*)'Enter the year:'         read(*,*)l         if (mod(l,4).ne.0)goto 10         if (mod(l,100).ne.0)goto 20         if (mod(l,400).ne.0)goto 10 20    write(*,*)'The year is a leap year.'         stop 10    write(*,*)'The year is  not a leap year.'         stop         end

Program to find large number between two numbers

C      Program to find large number between two numbers         write(*,*)'Input the two numbers:'         read(*,*)a,b         large=a         if(large.lt.b)then         large=b         write(*,*)'The large number is',large         else         write(*,*)'The large number is',large         end if         end

Program to find the trace of a matrix

C     Program to find the trace of a matrix        integer::n        real::trace=0.0,a(10,10)        print*,'Enter the number of row and coloumns of square matrix:'        read(*,*)n        write(*,*)'Enter the matrix elements:'        do i=1,n        read(*,*)(a(i,j),j=1,n)        end do        write(*,*)'The matrix is :'        do i=1,n        write(*,*)(a(i,j),j=1,n)        end do        do i=1,n        trace=trace+a(i,i)        end do        write(*,*)'The trace of the entered matrix is',tra...

Program to find the value of integration using Simpson's 1/3 rule

!       Program to find the value of integration using Simpson's 1/3 rule        write(*,*)'Enter the values of lower and upper limit of integration  respectively:'         read(*,*)a,b         k=4         write(*,*)'Enter the number of even sub intervals:'         read(*,*)n         sum=f(a)+f(b)         h=(b-a)/n         do i=1,n-1         sum=sum+k*f(a+i*h)         k=6-k         end do         sum=sum*h/3.0         write(*,10)sum 10    format(2x,'The value of integration is',f16.3)  ...

Program to find the reverse of a given number

C      Program to find the reverse of a given number         write(*,*)'Enter an integer:'         read(*,*)n         m=n         rev=0         if (n.eq.0)then         write(*,*)'The reverse number is:',n         else         dowhile (n.ne.0)         r=mod(n,10)         rev=rev*10+r         n=n/10         end do         write(*,*)'The reverse of the given number is',rev         end if         end          ...

Program to find the root of the quadratic equation

C      Program to find the root of the quadratic equation         write(*,*)'Enter the values of a,b,c'         read(*,*)a,b,c         d=b*b-(4.0*a*c)         if (d.eq.0)then         write(*,*)'The root is real and equal.'         x1=-b/(2.0*a)         write(*,*)'The equal root is :',x1         else if (d.gt.0)then         write(*,*)'The roots are real and distinct.'         x1=(-b+sqrt(d))/(2.0*a)         x2=(-b-sqrt(d))/(2.0*a)         write(*,*)'The roots are :',x1,' and ',x2         else    ...

Program to find prime numbers between two integers

C      Program to find prime numbers between two integers         write(*,*)'Enter the limits:'         read(*,*)m,n         if (m.gt.n)then         temp=m         m=n         n=temp         end if         i=m         print*,'The prime numbers between are given below:'  20   do j=2,i/2         r=mod(i,j)         if(r.eq.0)goto 10         end do         write(*,*)i  10   if(i.lt.n)then         i=i+1         goto 20 ...

Program to find prime or composite number

C      Program to find prime or composite number         write(*,*)'Input the integer:'         read(*,*)n         kount=0         if (n.gt.1)then         do i=1,(n/2)         l=mod(n,i)         if (l.eq.0) then         kount=kount+1         end if         end do         if (kount.eq.1)then         write(*,*)'The number is prime.'         else         write(*,*)'The number is composite.'         end if         e...

Prime factor of a given integer

C      Prime factor of a given integer         write(*,*)'Input an integer:'         read(*,*)n         write(*,*)'The prime factor(s) of the integer is given below:'         do i=2,n         r=mod(n,i)         if (r.ne. 0.0)goto 10         do j=2,i/2         if(mod(i,j).eq.0)goto 10         end do         write(*,*)i  10   end do         end         

Program to find whether a number is perfect square or not

C      Program to find whether a number is perfect square or not         write(*,*)'Enter the number:'         read(*,*)x         y=sqrt(x)         if (x.eq.y**2)then         write(*,*)'The number is a perfect square.'         else         write(*,*)'The number is not a perfect square.'         endif         end

Program to find perfect numbers between 1 to 10000

C      Program to find perfect numbers between 1 to 10000         write(*,*)'The perfect numbers between 1 and 10000 are:'         do i=1,10000         sum=0         do j=1,(i/2)         r=mod(i,j)         if (r.eq.0)then         sum=sum+j         end if         end do         if (sum.eq.i)then         write(*,*)i         end if         end do         end        

Program to check whether a number is perfect or not

C      Program to check whether a number is perfect or not         write(*,*)'Input the number:'         read(*,*)n         sum=0         write(*,*)'The factors excluding that number are:'         do i=1,(n/2)         r=mod(n,i)         if (r.eq.0)then         write(*,*)i         sum=sum+i         end if         end do         if (sum .eq. n)then         write(*,*)'The number is perfect.'         else         write(*,*)'The number is not perfect....

Program to find the 1st nth terms and their sum of fibonacci series

C      Program to find the 1st nth terms and their sum of fibonacci series         write(*,*)'Enter the number of terms you want to see'         read(*,*)n         i=1         j=1         sum=0         write(*,*)'The 1st',n,'terms are:'         do l=1,n         write(*,*)i         sum=sum+i         k=i+j         i=j         j=k         end do         write(*,*)'The sum of the series is:',sum         end

Program to find the fibonacci series whose last term does not exceed the desired limit

!       Program to find the fibonacci series whose last term does not exceed the desired limit         write(*,*)'Enter the desired limit:'         read(*,*)n         print*,'The fibonacci series whose last term is not greater than',n,' is:'         i=1         j=1         do while (i.lt.n)         write(*,*)i         k=i+j         i=j         j=k         end do         end        

Program to find fibonacci series upto n terms

C     Program to find fibonacci series upto n terms        write(*,*)'Enter the terms you want see:'        read(*,*)n        i=1        j=1        write(*,*)'The series is:'        do l=1,n        write(*,*)i        k=i+j        i=j        j=k        end do        end       

Program to find the digits of a given number and their sum

C      Program to find the digits of a given number and their sum         write(*,*)'Enter the integer:'         read(*,*)n         m=n         sum=0         if (n.eq.0)then         write(*,*)'The number is',n         write(*,*)'The sum is',sum         else         write(*,*)'The digits are given below :'         do while (n.ne.0)         r=abs(mod(n,10))         write(*,*)r         sum=sum+r         n=n/10         end do     ...

Program to arrange the number in decending order

C      Program to arrange the number in decending order         dimension a(100)         write(*,*)'Enter the no. of terms:'         read(*,*)n         write(*,*)'Enter the numbers:'         do i=1,n         read(*,*)a(i)         end do         do i=1,n-1         do j=i+1,n         if (a(i).lt.a(j))then         temp=a(i)         a(i)=a(j)         a(j)=temp         end if         end do         end do  ...

Program in fortran to arrange the order in ascending order

C      Program in fortran to arrange the order in ascending order         dimension a(100)         write(*,*)'Enter the number of data:'         read(*,*)n         write(*,*)'Now, input the data:'         do i=1,n         read(*,*)a(i)         end do         do i=1,n-1         do j=i+1,n         if (a(i).gt.a(j))then         temp=a(i)         a(i)=a(j)         a(j)=temp         end if         end do         end ...

Program to find the sum of AP series

!       Program to find the sum of AP series         real::a,d         integer::n         write(*,*)'Enter the number of terms, 1st term and common difference:'         read(*,*)n,a,d         sum=0         do i=0,n-1         term=a+i*d         sum=sum+term         end do         write(*,*)'The sum of the AP series is',sum         end        

Program to find the value of cosine of a given angle

C      Program to find the value of cosine of a given angle         Write(*,*)'Enter the value of the angle in degree:'         read(*,*)x         y=(acos(-1.0)*x)/180.0         sum=0.0         term=1.0         i=0         s=1.0         dowhile(abs(term).ge.1.0E-6)         term=(y**i/fact(i))*s         sum=sum+term         i=i+2         s=-s         end do         write(*,10)sum 10    format(2x,'The value of cosx is',f16.4)       ...

Program to find sine of a given angle

C      Program to find sine of a given angle         write(*,*)'Enter the angle in degree:'         read(*,*)y         sum=0.0         term=1.0         i=1         s=1.0         x=(acos(-1.0)*y)/180.0         dowhile (abs(term).ge.1.0E-6)         term=(x**i/fact(i))*s         sum=sum+term         i=i+2         s=-s         end do         write(*,10)y,sum  10   format(2x,'The value of sin',f16.2,' is',f16.4)         end   ...

Program to find the sum of GP series

C      Program to find the sum of GP series         print*,'Enter the number of terms, 1st term and common ratio:'         read(*,*)n,a,r         sum=0         do i=0,n-1         sum=sum+a*r**i         end do         write(*,*)'The sum of the GP series is',sum         end        

Program to find the factors of an number

C      Program to find the factors of an number         write(*,*)'Enter the integer:'         read(*,*)n         write(*,*)'The factors are of the given no. are:'         do i=1,n         r=mod(n,i)         if (r.eq.0)then         write(*,*)i         end if         end do         end        

Program to find the factorial of a non-negative integer

C      Program to find the factorial of a non-negative integer         write(*,*)'Enter a non-negative integer:'         read(*,*)n         write(*,*)'The factorial of',n,'is',fact(n)         end          C      Program sub function         function fact(n)         fact=1         do i=1,n         fact=fact*i         end do         return         end

Program to test whether a number is positive or not

C      Program to test whether a number is positive or not         write(*,*)'Input the number:'         read(*,*)n         if (n.eq.0)then         write(*,*)'The entered number is zero.'         else if (n.gt.0)then         write(*,*)'The entered number is positive.'         else         write(*,*)'The entered number is negative.'         end if         end

Program to find the volume and surface area of a cuboid

C      Program to find the volume and surface area  of a cuboid         write(*,*)'Enter the length:'         read(*,*)r         write(*,*)'Enter the width:'         read(*,*)w         write(*,*)'Enter the height:'         read(*,*)h         a=2.0*(w*r+r*h+h*w)         v=w*r*h         d=sqrt(w**2+r**2+h**2)         write(*,10)a         write(*,20)v         write(*,30)d  10   format(2x,'The area of the cuboid is',f16.4)  20   format(2x,'The volume of the cuboid is',f16.4)  30   format(2x,'The space di...

Program to find the volume and total surface area of a cone

C      Program to find the volume and total surface area of a cone         write(*,*)'Enter the radius:'         read(*,*)r         write(*,*)'Enter the height:'         read(*,*)h         a=acos(-1.0)*r*(r+sqrt(r*r+h*h))         v=((acos(-1.0)*r*r*h)/3.0)         write(*,10)a         write(*,20)v 10    format(2x,'The total surface area of the cone is',f16.4) 20    format(2x,'The volume of the cone is',f16.4)         end

To check whether a number is devisible by 7 or not

C      To check whether a number is devisible by 7 or not         write(*,*)'Input the integer number:'         read(*,*)n         if (mod(n,7).eq. 0)then         write(*,*)'The number is divisible to 7.'         else         write(*,*)'The number is not divisible to 7.'         end if         end

Program to find sum of the series 2 + 4 + 2 + 4 + 2 + ... upto a no. of terms

C       Program to find sum of the series 2 + 4 + 2 + 4 + 2 + ... upto a no. of terms         write(*,*)'Input the no. of terms:'         read(*,*)n         sum=0         p=2         do i=1,n         sum=sum+p         p=6-p         end do         write(*,*)'The sum of the given series is:',sum         end        

Program to find the sum of the series 1 + 1/3 + 1/5 + ... upto n terms

C      Program to find the sum of the series 1 + 1/3 + 1/5 + ... upto n terms         write(*,*)'Enter the no. of terms:'         read(*,*)n         sum=0.0         i=1         do j=1,n         sum=sum+1.0/i         i=i+2         end do         write(*,10)sum  10   format(2x,'The sum of the series is',f16.4)         end        

Program to find the sum of a series upto a no. of terms

c                                                                   (4n-3)(4n-2) c      Program to find the sum of the series ------------------ upto a no. of terms c                                                                      4n(4n-1)         write(*,*)'Enter the no. of terms:'         read(*,*)n         sum=0       ...

Program to find sum of n^2 series

C      Program to find sum of n^2 series         write(*,*)'Enter the no. of terms:'         read(*,*)n         sum=0.0         do i=1,n         term=i**2         sum=sum+term         end do         write(*,*)'The sum of the given series is:',sum         end        

Program to find the sum and average using array

C      Program to find the sum and average using array         dimension a(100)         sum=0.0         write(*,*)'Enter the no. of data you want to enter:'         read(*,*) n         write(*,*)'Now, enter the data:'         do i=1,n         read(*,*)a(i)         sum=sum+a(i)         end do         ave=sum/real(n)         write(*,*)'The sum of the given numbers is',sum         write(*,*)'The average of the given numbers is',ave         end        

Program to find the scalar product of two vectors

C      Program to find the scalar product of two vectors         dimension x(20),y(20)         product=0.0         write(*,*)'Enter the total no. components in each vector:'         read(*,*)n         write(*,*)'Enter the components of the first vector:'         do i=1,n         read(*,*)x(i)         end do         write(*,*)'Enter the components of the second vector:'         do i=1,n         read(*,*)y(i)         end do         do i=1,n         product=product+x(i)*y(i)  ...