Sunday 25 August 2019

Python program - determining odd or even number

#determine the odd or even number by python program
#Odd or Even
num = input("Enter a Number: \n")
if num%2 == 0:
print "Even"
else:
print "Odd"

Saturday 24 August 2019

Python program for sum of numbers, area of a triangle

#Sum of numbers from 1 to 100
sum = 0
for n in range(1,101):
sum = sum + n
print "Sum = ", sum

Result:
Sum = 5050

#Area of a Triangle by Heron's formula
import math
a, b, theta = input('Enter a, b & angle in degree: \n')
theta = math.pi*theta/180 #Convert into radian
c = math.sqrt(a*a + b*b - 2*a*b*math.cos(theta))
s =(a + b + c)*0.5
area = math.sqrt(s*(s - a)*(s - b)*(s - c))
print 'Area = ', area


Monday 19 August 2019

Python programming for beginner

Writing a Python program is very easy and close to the human language and logic. Python
comes with minimal syntaxes.
Some mathematical operations:-
Let, variable- a=8,b=2
Addition (+): a + b = 10
Subtraction (-): a – b = 6
Multiplication (*): a*b = 16
Division(/): a/b = 4
Modulus (%): a%b = 0 [Remainder, as a is divided by b]
Power or Exponent (**): a**b = 64
Rounding off (//): Example, 7.5//2 = 3.0, where 7.5/2 = 3.75
Variables in python:-
Name of a variable (or any identifier such as class, function, module or other object) can be
anything combined with alphabets, letters and some symbols in the keyboard (except @, $, %
etc.), beginning with a letter (the upper case or lower case letters from A to Z). The names of
the variables are, of course, case sensitive. The variable names, ‘Aa’, ‘aa’, ‘aA' are all different.
But the names cannot be the words given in the following table, used for system commands
and functions. Those are Python keywords (in lower case letters) reserved for the use by the
system.
Some reserved words:
and, assert, break, class, continue, def, del, elif, else,
except, exec, finally, for, from, global, if, import, in,
is, lambda, not, or, pass, print, raise, return, try,
while, with, yield

Numbers:-
Real or Floating [Examples: 19.25, 0.0, -3.23, 1.9e-20, 3.75e2 ]
 Integer [Examples: 6, -23]
 Complex [Examples: 70j, 1.9j, 7+9j, 9.3e-21j]
 Long [Examples: Any number ends with "l" or "L". 123L]

Input/Output(I/O) Statements:
input -
x = 56
y = -190.8
z = 54
x,y,z = 56,-190.8,54
xx = "name" [Alphabetic characters, put inside quotes]
a = input("Give some value")
output-
print x, y, xx, Aa
print “Some statement”
print 'x = ', x

Reading/ Writing in a File
To Open a file"
f1 = open("filename", "w")                             #"w" for writing
f2 = open("filename", "r")                              #"r" for reading
To write in a file
print >> f1, x, y                                            #Writing data: x, y in file indexed ‘f1’
To Close the file
f1.close()

Sunday 18 August 2019

ratio of consecutive terms of fibonocci series - c program


/*ratio of consecutive terms of fibonocci series*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i;
                float r0,r1,f[100];
                i=0;
                f[0]=1;
                f[1]=1;
                r1=1.00;
label:
                r0=r1;
                i++;
                f[i+1]=f[i]+f[(i-1)];
                r1=f[i+1]/f[i];
                if(fabs(r1-r0)>0.001)
                                goto label;
                printf("the value of the ratio is %f\n",r1);
}
RESULT:
the value of the ratio is 1.618182
Press any key to continue


C program for least square fitting of a straight line


/*least square fitting of a straight line */
#include<stdio.h>
#include<math.h>
void
main()
{
int n,i;
float a,b,x[20],y[20],sx=0.0,sxx=0.0,sy=0.0,sxy=0.0;
printf("enter the total number of data:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",&x[i]);
}
printf("enter the values of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
for(i=0;i<n;i++)
{
                sx=sx+x[i];
                sxx=sxx+(x[i]*x[i]);
                sy=sy+y[i];
                sxy=sxy+(x[i]*y[i]);
}
a=(n*sxy-sx*sy)/(n*sxx-sx*sx);
b=(sxx*sy-sx*sxy)/(n*sxx-sx*sx);
printf("the line of least fit is:\n y=%fx+%f\n",a,b);
}
Result:
enter the total number of data:5
enter the value of x:1 2 3 4 5
enter the values of y:2.03 4.26 6.59 8.12 10.28
the line of least fit is:
 y=2.036000x+0.148000
Press any key to continue

C program for least square fitting of aexp(bx)


/*least square fitting of aexp(bx) */
#include<stdio.h>
#include<math.h>
void
main()
{
int n,i;
float a,b,x[20],y[20],x1[20],y1[20],sx=0.0,sxx=0.0,sy=0.0,sxy=0.0,c,m,e=2.71828;
printf("enter the total number of data:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",&x[i]);
}
printf("enter the values of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
for(i=0;i<n;i++)
{ x1[i]=x[i];
y1[i]=log(y[i]);
                sx=sx+x1[i];
                sxx=sxx+(x1[i]*x1[i]);
                sy=sy+y1[i];
                sxy=sxy+(x1[i]*y1[i]);
}
m=(n*sxy-sx*sy)/(n*sxx-sx*sx);
c=(sxx*sy-sx*sxy)/(n*sxx-sx*sx);
a=pow(e,c);
b=m;
printf("the line of least fit is:\n y=%fe^(%fx)\n",a,b);
}
RESULT:
enter the total number of data:5
enter the value of x:1 2 3 4 5
enter the values of y:70 259 920 3340 12000
the line of least fit is:
 y=19.563120e^(1.284523x)
Press any key to continue

C program - Newton Raphson method


/*newton raphson method*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int count=0;
                float x0,xn,fx,dfx;
                printf("enter the guess value:");
                scanf("%f",&x0);
                /*iteration process*/
label: fx=log(x0)-x0+2;
                   dfx=pow(x0,-1)-1;
                   xn=x0-(fx/dfx);
                   if((fabs(xn-x0))<0.001)
                                   printf("\n the final root is %.4f \n",xn);
                   else
                   {x0=xn;
                   count=count+1;
                   if (count>100)
                   {
                                   printf("the solution does not converge");
                                   printf("enter a different guess value:");
                   }
                   else
                                   goto label;
                   }
}
Result1:
enter the guess value:2
 the final root is 3.1462
Press any key to continue
Result2:
enter the guess value:10

 the final root is 3.1462
Press any key to continue
/*least square fitting of a(x^b)*/
#include<stdio.h>
#include<math.h>
void
main()
{
int n,i;
float a,b,x[20],y[20],x1[20],y1[20],sx=0.0,sxx=0.0,sy=0.0,sxy=0.0,c,m,e=2.71828;
printf("enter the total number of data:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",&x[i]);
}
printf("enter the values of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
for(i=0;i<n;i++)
{
x1[i]=log(x[i]);
y1[i]=log(y[i]);
                sx=sx+x1[i];
                sxx=sxx+(x1[i]*x1[i]);
                sy=sy+y1[i];
                sxy=sxy+(x1[i]*y1[i]);
}
m=(n*sxy-sx*sy)/(n*sxx-sx*sx);
c=(sxx*sy-sx*sxy)/(n*sxx-sx*sx);
a=pow(e,c);
b=m;
printf("the line of least fit is:\n y=(%f)(x,(%f))\n",a,b);
}
Result:
enter the total number of data:5
enter the value of x:1 2 3 4 5
enter the values of y:20 30 38 43 48
the line of least fit is:
 y=(20.295511)(x,(0.546196))
Press any key to continue

C programming for bisection method


/*bisection method*/
#include<stdio.h>
#include<math.h>
#define f(x) ((x*sin(x))+((x-2)*cos(x)))
void
main()
{
                int count;
                float a,b,x,y,y1,y2;
    printf("enter the valu of the intervals:");
                scanf("%f %f",&a,&b);
                y1=f(a);
                y2=f(b);
                count=1;
                if(y1*y2>0)
                                printf("\n invalid interval\n");
                                else
                                {label: x=(a+b)/2;
                                y=f(x);
                                if(y1*y<0)
                                {               b=x;
                                                y2=y;
                                }
                                else
                                {               a=x;
                                                y1=y;
                                }
                                printf("%d\t",count);
                                printf("%f\n",x);
                                count=count+1;
                                if(fabs(b-a)>0.001)
                                                goto label;
                                }
}
RESULT:
enter the valu of the intervals:2 5
1       3.500000
2       2.750000
3       3.125000
4       2.937500
5       2.843750
6       2.890625
7       2.867188
8       2.855469
9       2.849609
10      2.852539
11      2.851074
12      2.851807
Press any key to continue

C program for Newton Raphson method


/*newton raphson method*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int count=0;
                float x0,xn,fx,dfx;
                printf("enter the guess value:");
                scanf("%f",&x0);
                /*iteration process*/
label: fx=(((1-x0*x0)*tan(x0))-x0);
                   dfx=(((1-x0*x0)*(pow(cos(x0),-2)))-(1+(2*x0*tan(x0))));
                   xn=x0-(fx/dfx);
                   if((fabs(xn-x0))<0.001)
                                   printf("\n the final root is %.4f \n",xn);
                   else
                   {x0=xn;
                   count=count+1;
                   if (count>100)
                   {
                                   printf("the solution does not converge");
                                   printf("enter a different guess value:");
                   }
                   else
                                   goto label;
                   }
}
RESULT:
enter the guess value:2 3
 the final root is 2.7437
Press any key to continue

Programming for integration by simpson's 1/3 rule


/*integration by simpson's 1/3 rule*/
#include<stdio.h>
#include<math.h>
float f(float x)
{
float d;
d=(x/(1+pow(x,5)));
return d;
}
void
main()
{
int i,n;
float a,b,h,s=0.0,c;
printf("enter the value of lower and upper limit:");
scanf("%f %f",&a,&b);
printf("enter the number of intervals:");
scanf("%d",&n);
label:
h=(b-a)/n;
c=s;
s=f(a)+f(b)+4*f(a+h);
for(i=3;i<=(n-1);i=i+2)
{
                s=s+(4*f(a+i*h))+(2*f(a+(i-1)*h));
}
s=(h/3.0)*s;
if(fabs (s-c)/s>0.001)
{
                n=n+10;
                goto label;
}
printf("the value of the interval is %.3f and is converged %d intervals\n",s,n);
}
RESULT:
enter the value of lower and upper limit:0 3
enter the number of intervals:10
the value of the interval is 0.648 and is converged 20 intervals
Press any key to continue

Programming for Lagrange's interpolation method


/*lagrange's interpolation method*/
#include<stdio.h>
#include<math.h>
void
main()
{int i,j,n;
float x[20],y[20],x0,sum=0.0,num,deno;
printf("enter the total number of inputs:");
scanf("%d",&n);
printf("enter the value of x:");
for(i=0;i<n;i++)
{
                scanf("%f",& x[i]);
}
printf("enter the corresponding value of y:");
for(i=0;i<n;i++)
{
                scanf("%f",&y[i]);
}
printf("enter the value of x at which y is calculated:");
scanf("%f",& x0);
for(i=0;i<n;i++)
{
                num=1.0;
                deno=1.0;
                for(j=0;j<n;j++)
                {
                                if(j!=i)
                                {
                                                num=num*(x0-x[j]);
                                                deno=deno*(x[i]-x[j]);
                                }
                }
                sum=sum+(num/deno)*y[i];
}
printf("the required value of y is=%f\n",sum);
}
RESULT:
enter the total number of inputs:7
enter the value of x:2 3 4 5 6 7 8
enter the corresponding value of y:2 7 14 23 34 47 62
enter the value of x at which y is calculated:5.5
the required value of y is=28.250000
Press any key to continue

Programming for Trapizoidal rule


/*trapizoidal rule*/
#include<stdio.h>
#include<math.h>
float fun(float x)
{
                float d;
                d=sqrt(1-(x*x));
                return(d);
}
void
main()
{
                float a,b,sum=0.0,integral=1.0,c=0.0,h;
                int i,n;
                printf("enter the lower and upper limits:");
                scanf("%f%f",&a,&b);
                printf("enter the total number of intervals:");
                scanf("%d",&n);
label:
                c=integral;
                h=(b-a)/n;
                sum=fun(a)+fun(b);
                for(i=1;i<n;i++)
                {
                                sum=sum+(2*fun(a+(i*h)));
                }
                integral=(h*0.5)*sum;
                if(fabs((integral-c)/integral)>0.001)
                {
                                n=n+10;
                                goto label;
                }
                printf("the value of the integral is %f and is converged for %d intervals",integral,n);
}
Result:
enter the lower and upper limits:0 1
enter the total number of intervals:10
the value of the integral is 0.784237 and is converged for 40 intervals
Press any key to continue









/*trapizoidal rule*/
#include<stdio.h>
#include<math.h>
float fun(float x)
{
                float d;
                d=(x*cos(x));
                return(d);
}
void
main()
{
                float a,b,sum=0.0,integral=1.0,c=0.0,h;
                int i,n;
                printf("enter the lower and upper limits in degree:");
                scanf("%f%f",&a,&b);
                printf("enter the total number of intervals:");
                scanf("%d",&n);
                a=a*(3.1415/180);
                b=b*(3.1415/180);
label:
                c=integral;
                h=(b-a)/n;
                sum=fun(a)+fun(b);
                for(i=1;i<n;i++)
                {
                                sum=sum+(2*fun(a+(i*h)));
                }
                integral=(h*0.5)*sum;
                if(fabs((integral-c)/integral)>0.001)
                {
                                n=n+10;
                                goto label;
                }
                printf("the value of the integral is %f and is converged for %d intervals",integral,n);
}
Result:
enter the lower and upper limits in degree:90 180
enter the total number of intervals:10
the value of the integral is -2.570212 and is converged for 20 intervals
Press any key to continue

Program - Gauss seidel method | convergence test


/*gauss seidel method*/
#include<stdio.h>
#include<math.h>
void
main()
{
float a[20][20],b[20],x[20],error,sum=0.0,e,s=0.0;
int n,i,j,c=0,f=0;
/*taking inputs*/
printf("\n enter the number of variables:");
 scanf("%d",&n);
 printf("\n enter the coefficient matrix rowwise\n");
 for(j=0;j<n;j++)
 {
                 for(i=0;i<n;i++)
                 {
                                 printf("\n\t a[%d][%d]=",j,i);
                                 scanf("%f",&a[j][i]);
                 }
 }
 printf("\n enter the constants:\n");
for(i=0;i<n;i++)
{
                printf("\n\t b[%d]=",i);
                scanf("%f",&b[i]);
}
/*convergence test*/
for(j=0;j<n;j++)

{
                sum=0;
                for(i=0;i<n;i++)
                {
                                if(i!=j)
                                                sum=sum+fabs(a[i][j]);
                }

if(fabs(a[j][j]>sum))
{
                f++;
}
}
if(f==0)
{
                printf("\n the solution does not converge");
                goto end;
}
else
{
                printf("\n\t %d equations satisfy the conditions",f);
}
/*iteration process*/
printf("\n enter the guess value of the solution \n");
for(i=0;i<n;i++)
{
                printf("\n\t x[%d]=",i);
                scanf("%f",&x[i]);
}
f=0;
printf("\n enter the required amount of accuracy");
scanf("%f",&error);
/*iteration loop*/
label:
{
                e=error;
                for(j=0;j<n;j++)
                {
                                s=0;
                                for(i=0;i<n;i++)
                                {
                                                if(i!=j)
                                                {
                                                                s=s+a[j][i]*x[i];
                                                }
                                }
                                sum=(b[j]-s)/a[j][j];
                                if(fabs(sum-x[j]<error))
                                {
                                                x[j]=sum;
                                }
                                else
                                {
                                                e=fabs(sum-x[j]);
                                                x[j]=sum;
                                }
                }
                c++;
                printf("\n number of iteration is %d \n",c);
                for(i=0;i<n;i++)
                {
                                printf("\n\t x[%d]=%f\n",i,x[i]);
                }
}
if(e>error)
goto label;
end:;
}
RESULT1:
 enter the number of variables:3
 enter the coefficient matrix rowwise
         a[0][0]=12
         a[0][1]=3
         a[0][2]=-5
         a[1][0]=1
         a[1][1]=5
         a[1][2]=3
         a[2][0]=3
         a[2][1]=7
         a[2][2]=13
 enter the constants:
         b[0]=1
         b[1]=28
         b[2]=76
         2 equations satisfy the conditions
 enter the guess value of the solution
         x[0]=1
         x[1]=1
         x[2]=1
 enter the required amount of accuracy0.001
 number of iteration is 1
         x[0]=0.250000
         x[1]=4.950000
         x[2]=3.123077
 number of iteration is 2
         x[0]=0.147116
         x[1]=3.696731
         x[2]=3.821657
 number of iteration is 3
         x[0]=0.751508
         x[1]=3.156704
         x[2]=3.972965
 number of iteration is 4
         x[0]=0.949559
         x[1]=3.026309
         x[2]=3.997474
 number of iteration is 5
         x[0]=0.992370
         x[1]=3.003042
         x[2]=4.000123
 number of iteration is 6
         x[0]=0.999291
         x[1]=3.000068
         x[2]=4.000127

 number of iteration is 7
         x[0]=1.000036
         x[1]=2.999917
         x[2]=4.000037
Press any key to continue
RESULT2:
 enter the number of variables:4
 enter the coefficient matrix rowwise
         a[0][0]=10
         a[0][1]=-2
         a[0][2]=-1
         a[0][3]=-1
         a[1][0]=-2
         a[1][1]=10
         a[1][2]=-1
         a[1][3]=-1
         a[2][0]=-1
         a[2][1]=-1
         a[2][2]=10
         a[2][3]=-2
         a[3][0]=-1
         a[3][1]=-1
         a[3][2]=-2
         a[3][3]=10
 enter the constants:
         b[0]=3
         b[1]=15
         b[2]=27
         b[3]=-9
         4 equations satisfy the conditions
 enter the guess value of the solution
         x[0]=1
         x[1]=1
         x[2]=1
         x[3]=1
 enter the required amount of accuracy0.001
 number of iteration is 1
         x[0]=0.700000
         x[1]=1.840000
         x[2]=3.154000
         x[3]=-0.015200
 number of iteration is 2
         x[0]=0.981880
         x[1]=2.010256
         x[2]=2.996174
         x[3]=-0.001552
 number of iteration is 3
         x[0]=1.001513
         x[1]=1.999765
         x[2]=2.999818
         x[3]=0.000091
 number of iteration is 4
         x[0]=0.999944
         x[1]=1.999980
         x[2]=3.000011
         x[3]=-0.000006
Press any key to continue