Skip to main content

Posts

Showing posts from April, 2019

To build a polar graph in a square Canvas

void polar() { TCanvas* c = new TCanvas("myCanvas","myCanvas",600,600); double rmin=0; double rmax=TMath::Pi()*6; const int npoints=1000; Double_t r[npoints]; Double_t theta[npoints]; for (Int_t ipt = 0; ipt < npoints; ipt++)  { r[ipt] = ipt*(rmax-rmin)/npoints+rmin; theta[ipt] = TMath::Sin(r[ipt]); } TGraphPolar grP1 (npoints,r,theta); grP1.SetTitle("A Fan"); grP1.SetLineWidth(3); grP1.SetLineColor(2); grP1.DrawClone("AOL"); } The graph of a fan obtained with ROOT : Add caption

To find the solutions of simultaneous linear equations by Gauss-Siedel method

/* To find the solutions of simultaneous linear equations by Gauss-Siedel method */ #include<stdio.h> #include<math.h> float X1(float x2,float x3) { return(14-5*x2+2*x3)/20; } float X2(float x1,float x3) { return(17-3*x1-x3)/10; } float X3(float x2,float x1) { return(23-x1+4*x2)/10; } int main() { float x1=0,x2=0,x3=0,acc,y1,y2,y3; int i=0; printf("Enter desired accuracy:"); scanf("%f",&acc); printf("\n x1 \t x2 \t x3 \n"); printf("\n %f %f %f",x1,x2,x3); do { y1=X1(x2,x3); y2=X2(y1,x3); y3=X3(y1,y2); if(fabs(y1-x1)>=acc && fabs(y2-x2)>=acc && fabs(y3-x3)>=acc) { x1=y1; x2=y2; x3=y3; printf("\n %f %f %f",x1,x2,x3); } else { printf("\n Required solution:"); printf("\nx1=%f",y1); printf("\nx2=%f",y2); printf("\nx3=%f\n",y3); i=1; } } while(i!=1); }

To find corresponding f(x) value of given x value using a set of given data by Lagrange's interpolation method

/* To find corresponding f(x) value of given x value using a set of given data by Lagrange's interpolation method */ #include<stdio.h> int main() { float x[20],f[20],xn,fxn=0,p[30]; int n,i,j; printf("Enter the no of data:"); scanf("%d",&n); printf("Enter the values of xi:\n"); for(i=0;i<n;i++) { printf("x[%d]=",i); scanf("%f",&x[i]); } printf("Enter the values of fi:\n"); for(i=0;i<n;i++) { printf("f[%d]=",i); scanf("%f",&f[i]); } printf("Enter the values of xn:"); scanf("%f",&xn); for(i=0;i<n;i++) { p[i]=1; for(j=0;j<n;j++) { if(i==j) { continue; } else { p[i]=p[i]*((xn-x[j])/(x[i]-x[j])); } } } for(i=0;i<n;i++) { fxn=fxn+(p[i]*f[i]); } printf("Result is %f\n",fxn); }

Finding prime numbers in a limit and count them

/* Finding prime numbers in a limit and count them */ #include<stdio.h> int main() { int l,u,n,i,r,flag,term=0; printf("Enter lower limit:"); scanf("%d",&l); printf("Enter upper limit:"); scanf("%d",&u); printf("Prime number between %d and %d are:",l,u); for(n=l;n<=u;n++) { flag=0; for(i=2;i<n;i++) { r=n%i; if(r==0) { flag=1; break; } } if (flag==0 &&n!=1) { printf("%d ",n); term++; } } printf("\nThere are total %d prime numbers exist between %d and %d\n",term,l,u); return(0); }

To determine sum of log series

/* Sum of log series */ #include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int i=1; float sum=0,term,x,accu,preterm; printf("Enter the number for finding the log base e:"); scanf("%f",&x); printf("Enter the accuracy:"); scanf("%f",&accu); term=x-1; preterm=term/i; while(fabs(preterm)>accu) { preterm=term/i; sum+=preterm; term=term*(1-x); i+=1; } printf("The value of log%.2f=%f\n",x,sum); return(0); }

Finding the factor of a given number and check whether it is prime or not

/* Finding the factor of a number and check whether it is prime or not */ #include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int n,i,r,flag=0; printf("Enter a number:"); scanf("%d",&n); for(i=2;i<n;i++) { r=n%i; if(r==0) { flag=1; printf("%d is the factor of %d\n",i,n); } } if(flag==1) { printf("The number is not prime.\n"); } else { printf("The number is prime.\n"); } return(0); }