38 programs (2) (1)

Upload: kashif-ghani

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 38 programs (2) (1)

    1/75

    Program no.1Write a program in C to find sum and average of squareoffirst 100 natural numbers./* PROGRAM TO CALCULATE SUM AND AVERAGE OF 100NATURAL NUMBERS*/#include#include#includevoid main( ){ int i, n=100,sum=0, p;

    float avg;for (i=1;i

  • 8/2/2019 38 programs (2) (1)

    2/75

    program NO.2/*PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL

    NUMBERS*/#include#include#includevoid main(){

    int i,n;long int prod;clrscr();printf("\nEnter the value of n:\t");scanf("%d",&n);i=1,prod=1;a5:i=i+1;prod=prod*i;if(i

  • 8/2/2019 38 programs (2) (1)

    3/75

    Program no. 3

    Write a program in C to calculate simple and compoundinterest./* PROGRAM TO CALCULATE SIMPLE AND COMPOUNDINTEREST */#include#include#includevoid main(){

    float p,r,t,si,ci,a,v;clrscr();printf ("\n Principal amount p=");scanf ("%f",&p);printf("\n rate of interest r= ");scanf("%f",&r);printf("\n Time period t=");scanf("%f",&t);si=(p*r*t)/100;v=1+(r/100);a=p*pow(v,t);ci=a-p;printf("\n Simple Interest = %f",si);printf("\n Compound Interest = %f",ci);getch();

    }

    3

  • 8/2/2019 38 programs (2) (1)

    4/75

    OUTPUT:Principal amount p =5000Rate of interest r =10Time period t =12Simple Interest = 6000.000000Compound Interest = 10692.14188

    4

  • 8/2/2019 38 programs (2) (1)

    5/75

    Program no 4/* FIBBONACCI SERIES */

    #include#include#includevoid main(){

    clrscr();int i;double x1,x2,x;x1=1;x2=1;printf("%12.0f\t%12.0f",x1,x2);for(i=3;i

  • 8/2/2019 38 programs (2) (1)

    6/75

    Program no.5/*GENERATING PRIME NO. BETWEEN 1 AND

    100*/

    #include#include#includevoid main(){

    int i,j,count=0;clrscr();printf("\nprime nos. between 1 and 100\n");for(i=1;i

  • 8/2/2019 38 programs (2) (1)

    7/75

    prime nos. between 1 and 100

    2 3 5 7 1113 17 19 23 2931 37 41 43 4753 59 61 67 7173 79 83 89 97

    7

  • 8/2/2019 38 programs (2) (1)

    8/75

    Program no.6Write a program in C to generate prime nos. between 1

    and 100 exceptthose divisible by 5.

    /* GENERATE PRIME NOS. BETWEEN 1 AND 100 EXCEPTTHOSE DIVISIBLE BY 5*/#include#includevoid main(){

    clrscr();int n1=1,n2=100,j,i,temp,k=0,flag=0;for (i=n1;i

  • 8/2/2019 38 programs (2) (1)

    9/75

    }if (flag==1)

    {printf ("\t %d",temp);}

    }}OUTPUT:

    2 3 7 11 13 17 19 23 2931 37 41 43 47 53 59 61 6771 73 79 83 89 97

    9

  • 8/2/2019 38 programs (2) (1)

    10/75

    Program no. 7

    /* to sort a list of 5 numbers in ascending order*/

    #include#include#includevoid main(){

    int i,j,t;int n[5]; clrscr();//to enter the dataprintf("\n enter the 5 numbers to be sorted\n");for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    11/75

    enter the 5 numbers to be sorted231421189output:914182123

    11

  • 8/2/2019 38 programs (2) (1)

    12/75

    Program no 8

    /*CALCULATION OF THE VALUE OF nCr*/#include#include#includevoid main(){

    int n,r,y;clrscr();printf("\nCALCULATION OF nCr");printf("\nenter the value of n ,r");scanf("%d%d",&n,&r);y=fact(n)/(fact(r)*fact(n-r));printf("\n%dC%d = %d",n,r,y);getch();

    }int fact (int a)

    {int f=1,j;if((a==0)&&(a==1))return(1);

    else{

    for(j=1;j

  • 8/2/2019 38 programs (2) (1)

    13/75

    8C3 = 56

    13

  • 8/2/2019 38 programs (2) (1)

    14/75

    Program no. 9/*SUM OF DIGITS OF A GIVEN NUMBER*/

    #include#include#includevoid main(){

    int num,p,sum=0;clrscr();printf("\nenter any number == ");scanf("%d",&num);while(num!=0)

    {p=num%10;

    sum=sum+p;num=num/10;

    }printf("\nsum of digit of given number == ");printf("%d",sum);getch();}

    OUTPUT:

    enter any number == 2319sum of digit given number == 15

    14

  • 8/2/2019 38 programs (2) (1)

    15/75

    program no. 10

    /* CALCULATION OF SINE SERIES */#include#include#includeint fact (int a);void main(){

    double x,sum1=0.0,sum2=0.0,sum=0.0;int y,i;clrscr();printf("\nenter the value for x:");scanf("%lf",&x);for(i=1;i

  • 8/2/2019 38 programs (2) (1)

    16/75

    }

    OUTPUT:

    enter the value for x:1sum of sine series sin(1.000000)==0.841471

    16

  • 8/2/2019 38 programs (2) (1)

    17/75

    Program no 11/*CALCULATION OF COSINE SERIES*/

    #include#include#includelong fact (int a);void main(){

    double x,sum1=0.0,sum2=0.0,sum=0.0;int y,i;clrscr();printf("\nenter the value for x:");scanf("%lf",&x);for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    18/75

    sum of cosine series cos(2.000000)==-1.350759

    18

  • 8/2/2019 38 programs (2) (1)

    19/75

    Program no.12Write a program in C to find out the average marks of

    students and print the marks of the topper.

    /* PROGRAM TO FIND OUT AVERAGE MARKS OF STUDENTS */#include#include#includevoid main(){

    int stu[20][20],i,j,m,n,roll=0;float avg[20],sum=0.0,max=0.0;clrscr();printf("\n Enter the number of students =");scanf("%d",&n);printf ("\n Enter the number of subjects =");scanf ("%d",&m);for(i=1;i

  • 8/2/2019 38 programs (2) (1)

    20/75

    {max=avg[i];roll=i;

    }}

    printf("\n The topper of the class is student %d with average=%f",roll,max);

    getch();}

    20

  • 8/2/2019 38 programs (2) (1)

    21/75

    OUTPUT:Enter the number of students =3Enter the number of subjects =5Enter marks for 1 studentSubject 1=69Subject 2=58Subject 3=45Subject 4=10Subject 5=35Enter marks for 2 studentSubject 1=47Subject 2=25Subject 3=16Subject 4=97Subject 5=46Enter marks for 3 studentSubject 1=30Subject 2=90Subject 3=76Subject 4=58Subject 5=47The average of the 1 student = 42.400002The average of the 2 student = 45.200001The average of the 3 student = 59.200001The topper of the class is student 3 with average = 59.200001

    21

  • 8/2/2019 38 programs (2) (1)

    22/75

    Program no.13Write a program in C to reverse the digits of a number andfind the sum of its digits./* PROGRAM TO REVERSE THE DIGITS OF A NUMBER ANDFIND THE SUM OF ITS DIGITS */#include#includevoid main(){int n1,n2=0,rem,sum=0;printf ("\n Enter the number to be reversed=");scanf ("%d",&n1);while (n1>0)

    {rrem=n1%10;n1=n1/10;n2=n2*10+rem;

    }printf ("\n The reversed number is %d",n2);while (n2>0)

    {rem=n2%10;n2=n2/10;sum=sum+rem;}

    printf ("\n The sum of digits of reversed number is %d",sum);}

    OUTPUT:

    Enter the number to be reversed =4567The reversed number is 7654The sum of digits of reversed number is 22

    22

  • 8/2/2019 38 programs (2) (1)

    23/75

    Programme no. 14Write a program in C to find the sum, mean standard deviation

    andvariance of any numbers.

    /*......STANDARD DEVIATION AND VARIATION ........*/#include#include#includevoid main(){

    clrscr();int n;float x[20],sum;float am,var,sd;int i;printf("\n enter the no. of terms=");scanf("%d",&n);printf("\n enter %d values \n",n);for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    24/75

    Enter 5 values498612Arithmetic MEAN, STD. DEVIATION and VARIANCE =7.8000007.360000 2.712932

    24

  • 8/2/2019 38 programs (2) (1)

    25/75

    Programme no. 15Write a program in C to convert binary number to decimal number./* CONVERT BINARY NUMBER TO DECIMAL NUMBER */

    #include#include#includevoid main(){int num[10];int i,x=2,j,y=0,n;printf ("\n Enter the number of elements of binary number=");scanf ("%d",&n);j=n-1;for (i=0;i=0)

    {for (i=0;i

  • 8/2/2019 38 programs (2) (1)

    26/75

    26

  • 8/2/2019 38 programs (2) (1)

    27/75

    Programme no.16

    Write a program in C to convert decimal number to binary number./* CONVERT DECIMAL NUMBER TO BINARY NUMBER */

    #include#includevoid main(){int i=0,j,n;int rem[10];printf ("\n Enter the number=");scanf ("%d",&n);do

    {rem[i]=n%2;i=i+1;n=n/2;}

    while (n>0);for (j=i-1;j>=0;j--)

    {printf ("The binary number is %d",rem[j]);}

    }OUTPUT:Enter the number= 123The binary number is 1111011

    27

  • 8/2/2019 38 programs (2) (1)

    28/75

    program no.17Write a program in C to add two rectangular matrices./* ADDTION OF MATRIX */#include#includevoid main(){int mat1[10][10],mat2[10][10],mat3[10][10];int i,j,m,n;clrscr();printf ("\n The number of rows are=");scanf ("%d",&m);printf ("\n The number of columns are=");scanf ("%d",&n);

    for (i=0;i

  • 8/2/2019 38 programs (2) (1)

    29/75

    {for (j=0;j

  • 8/2/2019 38 programs (2) (1)

    30/75

    Program no. 18

    /*MULTIPLICATION OF MATRIX*/#include#includevoid main(){int m,n,p,q,i,j,k;int a[10][10],b[10][10],c[10][10];clrscr();printf("\nenter no. of row and col of matrix a:");scanf("%d%d",&m,&n);printf("\nenter no. of row and col of matrix b:");scanf("%d%d",&p,&q);if(n!=p){ printf("\nmatrix can't be multiplied\n");

    goto end;}

    printf("\nenter matrix a\n");for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    31/75

    }31

  • 8/2/2019 38 programs (2) (1)

    32/75

    output:enter no. of row and col of matrix a:3 3enter no. of row and col of matrix b:3 2enter matrix a0 1 21 2 32 3 4enter matrix b1 -2

    -1 02 -1

    the product ofmatrix is:3 -25 -57 -8

    32

  • 8/2/2019 38 programs (2) (1)

    33/75

    program no.19/*BISECTION METHOD*/

    #include#include#includevoid main(){ float fun(float m);

    float x1,x2,x3,p,q,r;int i=0;

    clrscr();l10: printf("\nequation:x*(exp(x)-1) ");

    printf("\nenter the app value of x1,x2:");scanf("%f %f",&x1,&x2);

    if(fun(x1)*fun(x2)>0){ printf("\n wrong values entered...enter again:\n");goto l10;}

    elseprintf("\n the root lies b/w %f & %f",x1,x2);

    printf("\n n x1 x2 x3 f(x1) f(x2)f(x3)");l15: x3=(x1+x2)/2;p=fun(x1);q=fun(x2);r=fun(x3);i=i++;printf("\n%d %f %f %f %f %f %f",i,x1,x2,x3,p,q,r);

    if((p*r)>0)x1=x3;

    elsex2=x3;

    if((fabs((x2-x1)/x2))

  • 8/2/2019 38 programs (2) (1)

    34/75

    /*output:equation:x*(exp(x)-1)enter the app value of x1,x2:0.5 1the root lies b/w 0.500000 & 1.000000n x1 x2 x3 f(x1) f(x2) f(x3)1 0.500000 1.000000 0.750000 -0.175639 1.718282 0.587750

    2 0.500000 0.750000 0.625000 -0.175639 0.587750 0.1676543 0.500000 0.625000 0.562500 -0.175639 0.167654-0.0127824 0.562500 0.625000 0.593750 -0.012782 0.167654 0.0751425 0.562500 0.593750 0.578125 -0.012782 0.075142 0.0306196 0.562500 0.578125 0.570312 -0.012782 0.030619 0.0087807 0.562500 0.570312 0.566406 -0.012782 0.008780-0.0020358 0.566406 0.570312 0.568359 -0.002035 0.008780 0.0033649 0.566406 0.568359 0.567383 -0.002035 0.003364 0.00066210 0.566406 0.567383 0.566895 -0.002035 0.000662-0.000687root of the equ is 0.566895

    34

  • 8/2/2019 38 programs (2) (1)

    35/75

    programme no 20/*NEWTON RALPHSON*/

    #include#include#includevoid main(){float f(float a);float df(float a);int i=1;float x0,x1,p,q;float error =0.0001,delta =0.001;clrscr();printf("\n\nThe equation is X^3+1.2X^2-5X-7.2");printf("\nenter the initial value of x0:");scanf("%f",&x0);printf("\n i x0 x1 f(x0) df(x0)\n ");if (fabs (df(x0))

  • 8/2/2019 38 programs (2) (1)

    36/75

    /*output:The equation is X^3+1.2X^2-5X-7.2enter the initial value of x0:2i x0 x1 f(x0) df(x0)2 2.000000 2.372881 -4.400000 11.8000003 2.372881 2.313010 1.052938 17.5866154 2.313010 2.311227 0.029603 16.6012655 2.311227 2.311225 0.000026 16.572248

    The root of equation X^3+1.2X^2-5X-7.2 is 2.311227

    36

  • 8/2/2019 38 programs (2) (1)

    37/75

    Programme no.21/*REGULA-FALSE METHOD */

    #include#include#includefloat f(float x){return(3*x-cos(x)-1);}

    void main(){ float f(float x);

    double x1,x2,m;int c=0;clrscr();printf("\n enter the first approximation :");scanf("%f",&x1);printf("\n enter the second approximation :");scanf("%f",&x2);if(f(x1)*f(x2)=0.0001){c++;if(f(x1)*f(m)

  • 8/2/2019 38 programs (2) (1)

    38/75

    OUTPUT:Enter the first approximation : 0Enter the second approximation : 1The 1,ilteration is 0.605959The 2,ilteration is 0.607057The 3,ilteration is 0.607100The answer is repeated at 3 ilteration is 0.607100

    38

  • 8/2/2019 38 programs (2) (1)

    39/75

    Program no.22

    /*NEWTON GREGORY FORWARD INTERPOLATION*/

    #include#include#includevoid main(){

    int n,i,j,k;float mx[10],my[10],x,y=0,h,p,diff[20][20],y1,y2,y3,y4;clrscr();printf("\nenter no. of terms:");scanf("%d",&n);printf("\nenter values x\ty:\n");for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    40/75

    }

    OUTPUT:

    enter no. of terms:5enter values x y:3 135 2311 89927 1731534 35606enter value of x at which y is to be calculated:7when x=7.0000,y=899.00000000programme no.24

    40

  • 8/2/2019 38 programs (2) (1)

    41/75

    Program no.23/*NEWTON GREGORY BACKWARD INTERPOLATION*/

    #include#include#includevoid main(){

    int n,i,j,k;float mx[10],my[10],x,x0=0,y0,sum=0,fun=1,h,p,diff[20]

    [20],y1,y2,y3,y4;clrscr();printf("\nenter no. of terms:");scanf("%d",&n);printf("\nenter values x\ty:\n");for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    42/75

    printf("\nwhen x=%6.4f,y=%6.8f",x,sum);getch();}OUTPUT:enter no. of terms:5enter values x y:20 4140 10360 16880 218100 235enter value of x at which y is to be calculated:70when x=70.0000,y=196.00000000

    42

  • 8/2/2019 38 programs (2) (1)

    43/75

    program no.24/*LAGRANGE METHOD OF INTERPOLATION*/

    #include#include#include#define max 50void main(){

    float ax[max],ay[max],nr,dr,x,y=0;int i,j,n;clrscr();printf("\nEnter No. of Points:");scanf("%d",&n);printf("\nEnter the given set of values:\nx\ty\n");for(i=0;i

  • 8/2/2019 38 programs (2) (1)

    44/75

    7 29410 90011 121013 2028Enter the value of x at which f(x)is required:8when x= 8.00 then y=445.62

    44

  • 8/2/2019 38 programs (2) (1)

    45/75

    Programme no.25Write a program in C/C++ which can calculate the value of a function ata point using Newton Divided Difference method./* NEWTON DIVIDED DIFFERENCE METHOD */

    #include#include#includevoid main(){float ax[20], ay[20], diff[30],temp=1;int n,j,m,z=0,A=0,k=0;clrscr();coutn;for (int i=0;i

  • 8/2/2019 38 programs (2) (1)

    46/75

  • 8/2/2019 38 programs (2) (1)

    47/75

    265457709243242110The value of y for x = 9 is: 810

    47

  • 8/2/2019 38 programs (2) (1)

    48/75

    Program no.26

    /******BESSEL'S METHOD OFINTERPOLATION******/#include#include#includevoid main(){ int n , i , j ;

    float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 , y3 , y4 ;clrscr();printf("\nEnter the noumber of item : ");scanf("%d" , &n);printf("\nEnter the value in the form of x\n");for(i = 0 ; i < n ; i++){ pr intf("\nEnter the value of x%d\t" , i+1) ;

    scanf("%f" , &ax[i]); }printf("\nEnter the value in the form of y\n");for(i = 0 ; i < n ; i++){ pr intf("\nEnter the value of y%d\t" , i+1) ;

    scanf("%f" , &ay[i]) ; }printf("\nEnter the value of x for which you want the value of y :-

    "); scanf("%f" , &x);h = ax[1] - ax[0];for(i = 0 ; i < n-1 ; i++)

    diff[i][1] = ay[i+1] - ay[i];for(j = 2 ; j

  • 8/2/2019 38 programs (2) (1)

    49/75

    printf("\nWhen x = %6.4f , y = %6.8f" , x , y);getch();

    }

    49

  • 8/2/2019 38 programs (2) (1)

    50/75

    OUTPUT :-Enter number of item : 4Enter the value in the form of xEnter the value of x1 20Enter the value of x2 24Enter the value of x3 28Enter the value of x4 32Enter the value in the form of yEnter the value of y1 24Enter the value of y2 32Enter the value of y3 35Enter the value of y4 40Enter the value of x for which you want the value of y :-

    25When x = 25.0000 , y = 32.945313

    50

  • 8/2/2019 38 programs (2) (1)

    51/75

    Programe no 27/******STIRLING METHOD OF

    INTERPOLATION******/#include#include#includevoid main(){ int n , i , j ;

    float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 ,y3 ,y4;

    clrscr();printf("\nEnter the noumber of item : ");scanf("%d" , &n);printf("\nEnter the value in the form of x\n");for(i = 0 ; i < n ; i++){printf("\nEnter the value of x%d\t" , i+1);

    scanf("%f" , &ax[i]); }printf("\nEnter the value in the form of y\n");for(i = 0 ; i < n ; i++){printf("\nEnter the value of y%d\t" , i+1);scanf("%f" , &ay[i]); }

    51

  • 8/2/2019 38 programs (2) (1)

    52/75

    printf("\nEnter the value of x for which you want thevalue of y :- ");

    scanf("%f" , &x);h = ax[1] - ax[0];for(i = 0 ; i < n-1 ; i++)

    diff[i][1] = ay[i+1] - ay[i];for(j = 2 ; j

  • 8/2/2019 38 programs (2) (1)

    53/75

    Enter the value of x1 20Enter the value of x2 24Enter the value of x3 28Enter the value of x4 32Enter the value in the form of yEnter the value of y1 24Enter the value of y2 32Enter the value of y3 35Enter the value of y4 40Enter the value of x for which you want the value of y :-

    25When x = 25.0000 , y = 33.31251144

    53

  • 8/2/2019 38 programs (2) (1)

    54/75

    Program no.28/* TRAPEZOIDAL RULE */

    #include#include#includevoid main(){

    float fun(float);float h , k1=0.0 ;float x[20] , y[20];int n , i;clrscr();printf("\nEnter number of parts : ");scanf("%d" , &n);printf("\nEnter lower and upper limits : ");scanf("%f %f" , &x[0] , &x[n]);y[0] = fun(x[0]);h = (x[n] - x[0])/n ;printf("\nx y");printf("\n %8.5f %8.5f " , x[0] ,y[0]);for(i=1 ; i < n ; i++){ x[i] = x[0] + i * h ;

    y[i] = fun(x[i]);printf("\n %8.5f %8.5f " , x[i] , y[i]);k1 = k1 + 2 * y[i];

    }y[n] = fun(x[n]);printf("\n %8.5f %8.5f " , x[n] , y[n]);y[0] = (h / 2.0 ) * (y[0] + y[n] + k1 );printf("\nresult = %f \n" , y[0]);getch();

    }float fun(float x){

    54

  • 8/2/2019 38 programs (2) (1)

    55/75

    float g;g = log(x);return g;

    }

    OUTPUT :-Enter number of parts : 6lower and upper limits : 4 5.2x y4.00000 1.386294.24000 1.444564.48000 1.499624.72000 1.551814.96000 1.601415.20000 1.64866result = 1.827570

    55

  • 8/2/2019 38 programs (2) (1)

    56/75

    program no.29

    /* SIMPSION 1/3 RULE */#include#include#includevoid main(){ float fun(float);

    float h , k1=0.0 , k2=0.0 ;float x[20] , y[20];int n , i;clrscr();printf("\nEnter number of parts : ");

    scanf("%d" , &n);printf("\nEnter lower and upper limits :");scanf("%f %f" , &x[0] , &x[n]);y[0] = fun(x[0]);h = (x[n] - x[0])/n ;printf("\nx y");printf("\n%8.5f %8.5f" , x[0] ,y[0]);for(i=1 ; i < n ; i++){ x[i] = x[0] + i * h ;y[i] = fun(x[i]);

    printf("\n %8.5f %8.5f " , x[i] , y[i]);if(i % 2 == 0)

    k1 = k1 + 2 * y[i];else

    k2 = k2 + 4 * y[i];}

    y[n] = fun(x[n]);printf("\n %8.5f %8.5f " , x[n] , y[n]);y[0] = (h / 3.0 ) * (y[0] + y[n] + k1 + k2 );printf("\nresult =%f \n" , y[0]);

    getch();}float fun(float x){ float g;

    g = sin(x) - log(x) + exp(x);

    56

  • 8/2/2019 38 programs (2) (1)

    57/75

    return g;}

    OUTPUT :-Enter number of parts : 6Enter lower and upper limits :0.2 1.4x y0.20000 3.029510.40000 2.797530.60000 2.897590.80000 3.166041.00000 3.559751.20000 4.069831.40000 4.70418result = 4.052133

    57

  • 8/2/2019 38 programs (2) (1)

    58/75

    program no.30

    /*SIMPSION 3/8 RULE */#include#include#includevoid main(){ float fun(float);

    float h , k1=0.0 , k2=0.0 ;float x[20] , y[20];int n , i;clrscr();printf("\nEnter number of parts : ");scanf("%d" , &n);printf("\nEnter lower and upper limits : ");scanf("%f %f" , &x[0] , &x[n]);y[0] = fun(x[0]);h = (x[n] - x[0])/n ;printf("\nx y");printf("\n%8.5f %8.5f" , x[0] ,y[0]);for(i=1 ; i < n ; i++){ x[i] = x[0] + i * h ;

    y[i] = fun(x[i]);printf("\n %8.5f %8.5f " , x[i] , y[i]);if(i % 3 == 0)

    k1 = k1 + 2 * y[i];else

    k2 = k2 + 3 * y[i];}

    y[n] = fun(x[n]);printf("\n %8.5f %8.5f " , x[n] , y[n]);y[0] = ((3 *h) / 8.0 ) * (y[0] + y[n] + k1 + k2 );printf("\nresult =%f \n" , y[0]);

    getch();}float fun(float x){ float g;

    g = sin(x) - log(x) + exp(x);return g;

    }

    58

  • 8/2/2019 38 programs (2) (1)

    59/75

  • 8/2/2019 38 programs (2) (1)

    60/75

    program no.31

    /* BOOLS RULE */#include#include#includevoid main(){ float fun(float);

    float h , k1=0.0 , k2=0.0 , k3=0.0 , k4=0.0;float x[20] , y[20];int n , i;clrscr();printf("\nEnter number of parts");scanf("%d" , &n);printf("\nEnter lower and upper limits :");scanf("%f %f" , &x[0] , &x[n]);y[0] = fun(x[0]);h = (x[n] - x[0]) / n;printf("\nx y");printf("\n%8.5f %8.5f" , x[0] ,y[0]);for(i=1 ; i < n ; i++){ x[i] = x[0] + i * h ;

    y[i] = fun(x[i]);printf("\n %8.5f %8.5f " , x[i] , y[i]);if(i % 2 == 0)

    k1 = k1 + 12 * y[i];else

    k1 = k1 + 32 * y[i];}

    y[n] = fun(x[n]);printf("\n %8.5f %8.5f " , x[n] , y[n]);y[0] = ((2 * h)/45) * (7 * y[0] + 7 * y[n] + k1 + k2 + k3 +

    k4);printf("\nresult =%f \n" , y[0]);getch();

    }float fun(float x){ float g;

    g = log(x);

    60

  • 8/2/2019 38 programs (2) (1)

    61/75

    return g;}OUTPUT :-

    Enter number of parts : 6Enter lower and upper limits : 4 5.2x y4.00000 1.386294.20000 1.435084.40000 1.481604.60000 1.526064.80000 1.568625.00000 1.609445.20000 1.64866result = 1.814274

    61

  • 8/2/2019 38 programs (2) (1)

    62/75

    program no.32/* WEEDEL'S RULE */

    #include#include#includevoid main(){

    float fun(float);float h , k1=0.0 , k2=0.0 , k3=0.0 , k4=0.0;float x[20] , y[20];int n , i;clrscr();printf("\nEnter number of parts : ");scanf("%d" , &n);printf("\nEnter lower and upper limits : ");scanf("%f %f" , &x[0] , &x[n]);y[0] = fun(x[0]);h = (x[n] - x[0]) / n;printf("\nx y");printf("\n%8.5f %8.5f" , x[0] ,y[0]);for(i=1 ; i < n ; i++)

    { x[i] = x[0] + i * h ;y[i] = fun(x[i]);printf("\n %8.5f %8.5f " , x[i] , y[i]);if(i % 6 == 0)k1 = k1 + 2 * y[i];else if(i % 3 == 0)k1 = k1 + 6 * y[i];else if(i % 2 == 0)k1 = k1 + y[i];elsek4 = k4 + 5 * y[i];

    }y[n]= fun(x[n]);printf(\nf %8.5f " , x[n] , y[n]);

    y[0] = ((3 * h)/10) * (y[0] + y[n] + k1 + k2 + k3 + k4);

    62

  • 8/2/2019 38 programs (2) (1)

    63/75

    printf("\nresult =%f \n" , y[0]);getch();}float fun(float x){ float g;g = sin(x) - log(x) + exp(x);

    return g;}

    OUTPUT :-Enter number of parts : 6

    Enter lower and upper limits : 0.2 1.4x y0.20000 3.02951

    0.40000 2.797530.60000 2.897590.80000 3.166041.00000 3.559751.20000 4.069831.40000 4.70418result = 4.051446

    63

  • 8/2/2019 38 programs (2) (1)

    64/75

    program no.33/* GAUSS ELIMINATION METHOD */

    #include#include#include#define n 3void main(){

    float temp , s , matrix[n][n+1] , x[n];int i , j , k;clrscr();printf("\nEnter the elements of the augment matrix row

    wise :\n");for(i = 0 ; i < n ; i++){

    for(j=0 ; j

  • 8/2/2019 38 programs (2) (1)

    65/75

    {s = 0;for(j = i + 1 ; j < n ; j++)s += matrix[i][j] * x[j];x[i] = (matrix[i][n] - s) / matrix[i][i];}

    / /now printing the resultprintf("\nSolution is :-\n");for(i = 0 ; i < n ; i++)printf("\nx[%d]=%7.4f" , i+1 ,x[i]);

    getch();}

    OUTPUT :-Enter the elements of the augment matrix row

    wise :3 1 -1 32 -8 1 -51 -2 9 8matrix:-3.000000 1.000000 -1.000000 3.0000002.000000 -8.000000 1.000000

    -5.0000001.000000 -2.000000 9.000000 8.000000Solution is :-x[1]= 1.0000x[2]= 1.0000

    65

  • 8/2/2019 38 programs (2) (1)

    66/75

    x[3]= 1.0000

    66

  • 8/2/2019 38 programs (2) (1)

    67/75

    program no 34/* GAUSS JORDAN METHOD */

    #include#include#include#define n 3void main(){

    float temp , matrix[n][n+1];int i , j , k;clrscr();printf("\nEnter the elements of the augment matrix row

    wise :-\n");for(i = 0 ; i < n ; i++)

    {for(j=0 ; j

  • 8/2/2019 38 programs (2) (1)

    68/75

    printf("\nx[%d]=%7.4f" , i+1 ,matrix[i][n]/matrix[i][i]);getch();

    }OUTPUT :-

    Enter the elements of the augment matrix row wise :-3 1 -1 32 -8 1 -51 -2 9 8The digonal matrix is :--3.000000 0.000000 0.000000 3.0000000.000000 -8.666667 0.000000 -8.6666670.000000 0.000000 8.884615 8.884615Solution is :-x[1]= 1.0000x[2]= 1.0000x[3]= 1.0000

    68

  • 8/2/2019 38 programs (2) (1)

    69/75

    Programme no.35/* GAUSS SEIDAL METHOD */

    #include#include#include#includevoid main(){

    float a[10][10],x[10],aerr, maxerr, t, s, err;int i,j,itr,maxitr,n;clrscr();printf ("\n Enter the number of unknowns=");scanf ("%d",&n);for(i=1;i

  • 8/2/2019 38 programs (2) (1)

    70/75

    x[i]=t;}

    printf ("%d",itr);for (i=1;i

  • 8/2/2019 38 programs (2) (1)

    71/75

    Program no.36/* CURVE FITTING - STRAIGHT LINE */

    # include# include# includevoid main(){

    int i=0,ob;float

    x[10],y[10],xy[10],x2[10],sum1=0,sum2=0,sum3=0,sum4=0;clrscr();double a,b;printf("\n Enter the no. of observations :");scanf(%d,&ob);printf("\n Enter the values of x :\n");for (i=0;i

  • 8/2/2019 38 programs (2) (1)

    72/75

    sum4+=x2[i];}

    a=(sum2*sum4-sum3*sum1)/(ob*sum4-sum1*sum1);b=(sum2-ob*a)/sum1;printf("\n\n Equation of the STRAIGHT LINE of the formy=a+b*x is );printf("\n\n\t\t\t y=[%f] + [%f]x.a,b);getch();

    }

    OUTPUT:Enter the no. of observations : 5Enter the values of x :Enter the value of x1 : 1Enter the value of x2 : 2Enter the value of x3 : 3Enter the value of x4 : 4Enter the value of x5 : 5Enter the values of y :Enter the value of y1: 14Enter the value of y2: 27Enter the value of y3: 40Enter the value of y4: 55Enter the value of y5: 68

    Equation of the STRAIGHT LINE of the form y=a+b*x is :y=0 + 13.6 x

    72

  • 8/2/2019 38 programs (2) (1)

    73/75

    Program no.37/* RUNGA - KUTTA METHOD */

    #include#include#includevoid main(){

    float f(float x , float y);float x0 = 0.1 , y0 = 1 , xn = 2.1 , h =0.2 , k1 , k2 , k3 ,

    k4 ;int i , n ;clrscr();printf("\ndy/dx = (x^3 +y^2)/10 ");printf("\ngiven y0=1 && x0= 0.1 && h=0.2 (in the range

    x0 < x < 2.1)\n");n = (xn - x0) / h;for(i = 0 ; i

  • 8/2/2019 38 programs (2) (1)

    74/75

    /* Function sub program */float f(float x , float y){

    float g;g = (x*x*x + y*y) / 2 ;return g ;

    }

    OUTPUT :-dy/dx = (x^3 +y^2)/10given y0 = 1 && x0 = 0.1 && h = 0.2 (in the range x0

    < x < 2.1)The solution of differential equation is when x =

    0.100000 y = 1.053870The solution of differential equation is when x =

    0.300000 y = 1.116833The solution of differential equation is when x =0.500000 y = 1.194833

    The solution of differential equation is when x =0.700000 y = 1.297048

    The solution of differential equation is when x =0.900000 y = 1.436797

    The solution of differential equation is when x =1.100000 y = 1.633150

    The solution of differential equation is when x =1.300000 y = 1.914011The solution of differential equation is when x =

    1.500000 y = 2.322503The solution of differential equation is when x =

    1.700000 y = 2.931453The solution of differential equation is when x =

    74

  • 8/2/2019 38 programs (2) (1)

    75/75

    1.900000 y = 3.880822The solution of differential equation is when x =

    2.100000 y = 5.495997

    75