c lab programs 2

Upload: vempadareddy

Post on 03-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 C Lab Programs 2

    1/80

    Write a c program to evaluate the following series .Assume suitable valuesfor x.

    Y=1+x2/2!+x/!+x/!+""upto 1# terms

    $inclu%e&st%io.h'

    $inclu%e&conio.h'$inclu%e&math.h'voi% main()*int inx,float ts,clrscr(),printf(-enter the value of n n-),scanf(-0%0%-xn),t=1,s=1,

    for (i=1,i&=n1,i++)*t=((pow(x2)3(t3i))/((23i)3((23i1)3i)),s=s+t,printf(-s=0.f t=0.2fn-st),4printf(-n sum of the series=05.fn-s),getch(),4

    67897A: ;8 ? ;@ BC: 8< ;@ B> B7B

    DDE/E!+D5/5!DF/F!+""

    $inclu%e&st%io.h'$inclu%e&math.h'voi% main()* int i = 2 n s = 1 x pwr = 1 %r=1,

    float nr = 1 x1 sum, clrscr(), printf(-nnt >;7 ;@ A>9G...H -), scanf(-0%- x), x1 = E.12 3 (x / 1I#.#), sum = x1, printf(-nt >;7 ;@ >C:J7 8< ;7:B...H -), scanf(-0%- n),

  • 8/12/2019 C Lab Programs 2

    2/80

    while(i &= n) * pwr = pwr + 2, %r = %r 3 pwr 3 (pwr 1), sum = sum + (nr / %r) 3 s,

    s = s 3 (1), nr = nr 3 x1 3 x1, i+= 2, 4 printf(-nt ;@ BC: 8< ;@ B> B7B B..H 0#.Ef-sum), getch(),4

    /3 Write a K program to accept two matrices an% 3 3 fin% the sum an%%ifference of the matrices 3/

    $inclu%e &st%io.h'$inclu%e &st%lib.h'int AL1#ML1#M JL1#ML1#M sumatL1#ML1#M %iffmatL1#ML1#M,int i N 71 K1 72 K2,

    voi% main()*

    /3

  • 8/12/2019 C Lab Programs 2

    3/80

    rea%matA(),printf(-:A;7D A isn-),printmatA(),printf(-nter the elements of matrix Jn-),rea%matJ(),

    printf(-:A;7D J isn-),printmatJ(),sum(),%iff(),44 /3 main() 3/

    /3

  • 8/12/2019 C Lab Programs 2

    4/80

    printf(-0E%-ALiMLNM),4printf(-n-),44

    /3

  • 8/12/2019 C Lab Programs 2

    5/80

    voi% %iff()* for(i=#, i&71, i++)

    *

    for(N=#, N&K2, N++)*%iffmatLiMLNM = ALiMLNM JLiMLNM,

    44

    printf(-?ifference matrix isn-),for(i=#, i&71, i++)

    *for(N=#, N&K2, N++)*

    printf(-0E%-%iffmatLiMLNM),4printf(-n-),

    4return,4

    /38utputnter the or%er of the matrix A 2 2nter the or%er of the matrix J 2 2nter the elements of matrix A 1 2 E

    :A;7D A is 1 2 E nter the elements of matrix J 2 I:A;7D J is 2 IBum matrix is E O 12?ifference matrix is 1 2 E 3/

    Write a program in K to multiplP two matricesQ

    $inclu%e&st%io.h'$inclu%e&conio.h'

    voi% main()* // main int aL1#ML1#MbL1#ML1#McL1#ML1#MmnpRiNx,

    clrscr(),printf(-nter row an% column of 1st matrixn-),scanf(-%-mn),printf(-enter row an% column of 2n% matrixn-),

    scanf(-%-pR),

  • 8/12/2019 C Lab Programs 2

    6/80

  • 8/12/2019 C Lab Programs 2

    7/80

    44

    4 //ifelse*

    printf(-matrices cannot be a%%e%ncolumn of 1st matrix shoul% be eRual torow of 2n% matrix-),4getch(),

    4 //main

    /3Kprogram to a%% subtract an% multiplP two complex numbers

    int main (voi%)*

    int aTrealaTimaginarPN,int bTrealbTimaginarP,int complexTnumber,

    char enterToperator,printf (-a real = -),scanf (-0%-aTreal),

    printf (-a imaginarP = -),scanf (-0%-aTimaginarP),

    printf (-b real = -),scanf (-0%-bTreal),printf (-b imaginarP = -),scanf (-0%-bTimaginarP),printf (-nter operator = -),scanf (-0c-enterToperator),if (enterToperator == U+U)complexTnumber = (aTreal+bTreal)+N(aTimaginarP+bTimaginarP),

    printf (-ncomplex number is 0%n-complexTnumber),else if (enterToperator == UU),complexTnumber = (aTrealbTreal)+N(aTimaginarPbTimaginarP),printf (-ncomplex number is 0%n-complexTnumber),

    else if (enterToperator == U/U),complexTnumber = ((aTreal3bTreal+aTimaginarP3bTimaginarP)/

  • 8/12/2019 C Lab Programs 2

    8/80

    (bTreal3bTreal)+(bTimaginarP3bTimaginarP))+N((aTimaginarP3bTrealaTreal3bTimaginarP)/(bTreal3bTreal+bTimaginarP3bTimaginarP)),printf (-ncomplex number is 0%n-complexTnumber),

    else if (enterToperator == U3U),

    complexTnumber = ((aTreal3bTreal)(aTimaginarP3bTimaginarP))+N((aTreal3bTimaginarP)+(aTreal3bTreal)),printf (-ncomplex number is 0%n-complexTnumber),return #,4

    @ow to write a K program to perform matrix multiplication using pointerQ

    void main()

    {int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

    printf("Enter The o! #nd $%o&mn #nd 'f The irt atri*+");canf("d d",-m,-n);printf(".nEnter The o! #nd $%o&mn #nd 'f The /econd atri*+");canf("d d",-p,-q);

    printf(".nEnter E%ement 'f The irt atri*+.n");

    for(i0;i m;i22)

    {for(j0;j n;j22){canf("d",-a[i][j]);3

    3

    printf(".nEnter E%ement 'f The /econd atri*+.n");

    for(i0;i p;i22) {

    for(j0;j q;j22)canf("d",-b[i][j]);

    3

    printf("The irt atri* 4+.n"); 56 7rint the firt matri* 65

    for(i0;i m;i22) {

  • 8/12/2019 C Lab Programs 2

    9/80

    for(j0;j n;j22)printf(" d ",a[i][j]);printf(".n");

    3

    printf("The /econd atri* 4+.n"); 56 7rint the econd matri* 65

    for(i0;i p;i22) {for(j0;j q;j22)printf(" d ",b[i][j]);printf(".n");

    3

    if(n8p) {

    printf("#bortin9:5n&%tip%ication 'f The #bove atrice ot 7oib%e:");e*it(0);

    3

    e%e {for(i0;i m;i22) {for(j0;j q;j22) {c[i][j] 0;

    for(k0;k n;k22) {c[i][j] c[i][j] 2 a[i][k] 6 b[k][j];333

    printf(".nThe 7rod&ct 'f The T!o atrice 4+.n.n");

    for(i0;i m;i22) {

    for(j0;j q;j22) {printf(" d ",c[i][j]);3

    printf(".n");

    3

    3ret&rn 0;

  • 8/12/2019 C Lab Programs 2

    10/80

    3

    Write a program in K to multiplP two matricesQ

    $inclu%e&st%io.h'$inclu%e&conio.h'voi% main()* // mainint aL1#ML1#MbL1#ML1#McL1#ML1#MmnpRiNx,clrscr(),printf(-nter row an% column of 1st matrixn-),scanf(-%-mn),printf(-enter row an% column of 2n% matrixn-),scanf(-%-pR),

    if(m==R) //K@KS>9 8< :CG;6GK;Y*printf(-matrices can be multiplie%n-),printf(-resultant matrix is 0% 0%n-mp),

    printf(-enter elements of 1st matrixn-),for(i=#,i&m,i++)

    for(N=#,N&n,N++)scanf(-0%-aLiMLNM),

    printf(-nter elements of 2n% matrixn-),

    for(i=#,i&p,i++)for(N=#,N&R,N++)scanf(-0%-bLiMLNM),

    // :CG;6GKA;8> 8< :A;7KBfor(i=#,i&m,i++)

    for(N=#,N&p,N++)*

    cLiMLNM=#,for(x=#,x&p,x++)cLiMLNM=cLiMLNM+(aLiMLxM3bLxMLNM),

    4printf(-the resultant matrixHn-), H67>;>9 7BCG;

    for(i=#,i&m,i++)*

    for(N=#,N&R,N++)printf(- 05% -cLiMLNM),printf(-n-),

  • 8/12/2019 C Lab Programs 2

    11/80

    44 //if

    elseprintf(-matrices cannot be a%%e%ncolumn of 1st matrix shoul% be eRual torow of 2n% matrix-),

    getch(),

    4 //main

    8r

    $inclu%e&st%io.h'main()*int aLEMLEMbLEMLEMcLEMLEM%LEMLEMiNm=2n=2,printf(-enter the values of matrix an-),

    for(i=#,i&2,i++)

    for(N=#,N&2,N++)

    scanf(-0%-aLiMLNM),

    printf(-enter the values of matrix bn-),

    for(i=#,i&2,i++)

    for(N=#,N&2,N++)

    scanf(-0%-bLiMLNM),

    printf(-a%%ition of a an% b matrix isn-),for(i=#,i&2,i++)for(N=#,N&2,N++)cLiMLNM=aLiMLNM+bLiMLNM,for(i=#,i&2,i++)for(N=#,N&2,N++)printf(-0%n-cLiMLNM),printf(-:A;7K :CG;6KA;8> of a an% b matrix isn-),for(i=#,i&2,i++)*for(N=#,N&2,N++)*cLiMLNM=#,for(=#,&2,++)*

  • 8/12/2019 C Lab Programs 2

    12/80

    cLiMLNM+=aLiMLM3bLMLNM,4printf(-0%t-cLiMLNM),44

    4

    Write a K program to fin% the 9K? of two numbers.

    @ere is a K program ....

    $inclu%e & st%io.h '

    int gc%(int a int b),

    int gc%Trecurse(int a int b),

    int main()

    *

    printf(-n9K?(02%02%) = L0%M- gc%()),

    printf(-n9K?(02%02%) = L0%M- gc%()),

    printf(-n9K?(02%02%) = L0%M- E1F gc%(E1F)),

    printf(-n9K?(02%02%) = L0%M- 1FE gc%(1FE)),

    printf(-n9K?(02%02%) = L0%M- 1 gc%(1)),

    printf(-n9K?(02%02%) = L0%M- 1#1 gc%(1#1)),

    printf(-n9K?(02%02%) = L0%M- 1# gc%(1#)),

    printf(-n9K?(02%02%) = L0%M- gc%Trecurse()),

    printf(-n9K?(02%02%) = L0%M- gc%Trecurse()),

    printf(-n9K?(02%02%) = L0%M- E1F gc%Trecurse(E1F)),

    printf(-n9K?(02%02%) = L0%M- 1FE gc%Trecurse(1FE)),

    printf(-n9K?(02%02%) = L0%M- 1 gc%Trecurse(1)),

    printf(-n9K?(02%02%) = L0%M- 1#1 gc%Trecurse(1#1)),

    printf(-n9K?(02%02%) = L0%M- 1# gc%Trecurse(1#)),

    getch(),getch(),

    return(#),

    4

    // terative algorithm

    int gc%(int a int b)

    http://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-find-gcd-of-two.htmlhttp://vijayinterviewquestions.blogspot.com/2007/07/write-c-program-to-find-gcd-of-two.html
  • 8/12/2019 C Lab Programs 2

    13/80

    *

    int temp,

    while(b)

    *

    temp = a 0 b,

    a = b,

    b = temp,

    4

    return(a),

    4

    // 7ecursive algorithm

    int gc%Trecurse(int a int b)

    *int temp,

    temp = a 0 b,

    if (temp == #)

    *

    return(b),

    4

    else

    *

    return(gc%Trecurse(b temp)),

    44

    An% here is the output ...

    terative

    9K?( ) = L2M

    9K?( ) = L2M

    9K?( E1F) = L1M

    9K?(1F E) = L1M9K?( 1 ) = L1M

    9K?(1# 1) = L1M

    9K?(1# ) = L2M

    7ecursive

  • 8/12/2019 C Lab Programs 2

    14/80

    9K?( ) = L2M

    9K?( ) = L2M

    9K?( E1F) = L1M

    9K?(1F E) = L1M

    9K?( 1 ) = L1M

    9K?(1# 1) = L1M

    9K?(1# ) = L2M

    >ote that Pou shoul% a%% error han%ling to chec if someone has passe%

    negative numbers an% Vero.

    /3Kprogram to a%% subtract an% multiplP two complex numbers

  • 8/12/2019 C Lab Programs 2

    15/80

    c%rcr();

    printf(".n.n .t.t.t66666 #4 E= 66666");

    printf(".n.n /e%ect >o&r option+ .n 1 + #??.n @ + =AT47AB.n 0 + EC4T .n.n.t.t

    Enter >o&r 'ption [ ].b.b");

    canf("d",-opern);

    !itch(opern)

    {

    cae 0+

    e*it(0);

    cae 1+

    cae @+

    arithmetic(opern);

    defa&%t+

    main();

    3

    3

    void arithmetic(int opern)

    {

    tr&ct comp !1, !@, !;

    printf(".n Enter t!o $omp%e* &mber (*2i>)+.n ea% 7art of irt &mber+");

    canf("%f",-!1:rea%part);

  • 8/12/2019 C Lab Programs 2

    16/80

    printf(".n 4ma9inar> 7art of irt &mber+");

    canf("%f",-!1:im9part);

    printf(".n ea% 7art of /econd &mber+");

    canf("%f",-!@:rea%part);

    printf(".n 4ma9inar> 7art of /econd &mber+");

    canf("%f",-!@:im9part);

    !itch(opern)

    {

    56addition of comp%e* n&mber65

    cae 1+

    !:rea%part !1:rea%part2!@:rea%part;

    !:im9part !1:im9part2!@:im9part;

    break;

    56m&%tip%ication of comp%e* n&mber65

    cae @+

    !:rea%part(!1:rea%part6!@:rea%part)D(!1:im9part6!@:im9part);

    !:im9part(!1:rea%part6!@:im9part)2(!1:im9part6!@:rea%part);

    break;

    3

    if (!:im9part0)

    printf(".n #n!er %f2%fi",!:rea%part,!:im9part);

  • 8/12/2019 C Lab Programs 2

    17/80

    e%e

    printf(".n #n!er %f%fi",!:rea%part,!:im9part);

    9etch();

    main();

    3

    Komplex >umbers

    int main (voi%)*

    int aTrealaTimaginarPN,int bTrealbTimaginarP,int complexTnumber,

    char enterToperator,printf (-a real = -),scanf (-0%-aTreal),

    printf (-a imaginarP = -),scanf (-0%-aTimaginarP),

    printf (-b real = -),scanf (-0%-bTreal),printf (-b imaginarP = -),scanf (-0%-bTimaginarP),printf (-nter operator = -),scanf (-0c-enterToperator),if (enterToperator == U+U)complexTnumber = (aTreal+bTreal)+N(aTimaginarP+bTimaginarP),

    printf (-ncomplex number is 0%n-complexTnumber),

    else if (enterToperator == UU),complexTnumber = (aTrealbTreal)+N(aTimaginarPbTimaginarP),printf (-ncomplex number is 0%n-complexTnumber),

    else if (enterToperator == U/U),complexTnumber = ((aTreal3bTreal+aTimaginarP3bTimaginarP)/(bTreal3bTreal)+(bTimaginarP3bTimaginarP))+N((aTimaginarP3bTrealaTreal3bTimaginarP)/(bTreal3bTreal+bTimaginarP3bTimaginarP)),

  • 8/12/2019 C Lab Programs 2

    18/80

    printf (-ncomplex number is 0%n-complexTnumber),

    else if (enterToperator == U3U),complexTnumber = ((aTreal3bTreal)(aTimaginarP3bTimaginarP))+N((aTreal3bTimaginarP)+(aTreal3bTreal)),

    printf (-ncomplex number is 0%n-complexTnumber),return #,4

    int main (void){

    int aFrea%,aFima9inar>,j; int bFrea%,bFima9inar>; int comp%e*Fn&mber;

    char enterFoperator; printf ("a rea% "); canf ("d",-aFrea%);

    printf ("a ima9inar> "); canf ("d",-aFima9inar>);

    printf ("b rea% "); canf ("d",-bFrea%); printf ("b ima9inar> ");

    canf ("d",-bFima9inar>); printf ("Enter operator "); canf ("c",-enterFoperator); if (enterFoperator G2G){comp%e*Fn&mber (aFrea%2bFrea%)2j(aFima9inar>2bFima9inar>);

    printf (".ncomp%e* n&mber i d.n",comp%e*Fn&mber);3 e%e if (enterFoperator GDG){ comp%e*Fn&mber (aFrea%DbFrea%)2j(aFima9inar>DbFima9inar>); printf (".ncomp%e* n&mber i d.n",comp%e*Fn&mber);3 e%e if (enterFoperator G5G){ comp%e*Fn&mber ((aFrea%6bFrea%2aFima9inar>6bFima9inar>)5(bFrea%6bFrea%)2(bFima9inar>6bFima9inar>))2j((aFima9inar>6bFrea%DaFrea%6bFima9inar>)5(bFrea%6bFrea%2bFima9inar>6bFima9inar>));

  • 8/12/2019 C Lab Programs 2

    19/80

    printf (".ncomp%e* n&mber i d.n",comp%e*Fn&mber);3 e%e if (enterFoperator G6G){ comp%e*Fn&mber ((aFrea%6bFrea%)D(aFima9inar>6bFima9inar>))

    2j((aFrea%6bFima9inar>)2(aFrea%6bFrea%)); printf (".ncomp%e* n&mber i d.n",comp%e*Fn&mber);

    3 ret&rn 0;3

    irt, if in an iftatement >o& have more than one in9%e intr&ction, >o& have to

    p&t thee intr&ction bet!een { and 3/econd, else if (expr)doe not ever end !ith a ,

    A??;8> BCJ;7AK;8> 8< ;W8 K8:6GD >C:J7B CB>9 K67897A:

    voi% main()*int abc%xP,clrscr(),printf(-nnter the first complex numberH-),scanf(-0%0%-ab),

    printf(-nnter the secon% complex numberH-),scanf(-0%0%-c%),if(b) x=-a+c,- P=-b+%,-'#)printf(-0%i0%-xP),elseprintf(-0%+i0%-x+P),printf(-nnBCJ;7AK;8> -),x=ac,P=b%,if(P)printf(-0%i0%-xP),

    elseprintf(-0%+i0%-x+P),getch(),4

    c22 pro9ram to add @ comp%e* n&mber &in9 operator over%oadin9 techniq&e

  • 8/12/2019 C Lab Programs 2

    20/80

    &in9 namepace td;

    c%a comp%e*Fn&mber{ int rea%Fpart;

    int ima9inar>Fpart; p&b%ic+ void et?ata(int va%,int f%a9) { rea%Fpart 0; ima9inar>Fpart 0; if(f%a9 0) { rea%Fpart va%; ima9inar>Fpart 0; 3

    e%e if(f%a9 1) {rea%Fpart 0;

    ima9inar>Fpart va%; 3

    3void et?ata(int rp, int ip, int f%a9)

    { rea%Fpart 0; ima9inar>Fpart 0; if(f%a9 D1) {

    rea%Fpart rp; ima9inar>Fpart ip; 3

    3 void ho!() { if(ima9inar>Fpart0) co&t"$omp%e* &mberi+ "rea%Fpart"2"ima9inar>Fpart"i"end%; e%e co&t"$omp%e* &mberi+ "rea%Fpartima9inar>Fpart"i"end%;

    3comp%e*Fn&mber operator2 (comp%e*Fn&mber op@);

    comp%e*Fn&mber operatorD (comp%e*Fn&mber op@); comp%e*Fn&mber operator22(); comp%e*Fn&mber operator22(int *);3;

    comp%e*Fn&mber comp%e*Fn&mber++ operator2(comp%e*Fn&mber

  • 8/12/2019 C Lab Programs 2

    21/80

    op@){ comp%e*Fn&mber temp; temp:rea%Fpart op@:rea%Fpart 2 rea%Fpart; temp:ima9inar>Fpart op@:ima9inar>Fpart 2

    ima9inar>Fpart; ret&rn temp;3

    comp%e*Fn&mber comp%e*Fn&mber++ operatorD(comp%e*Fn&mberop@){ comp%e*Fn&mber temp; temp:rea%Fpart rea%Fpart D op@:rea%Fpart; temp:ima9inar>Fpart ima9inar>Fpart Dop@:ima9inar>Fpart;

    ret&rn temp;3

    comp%e*Fn&mber comp%e*Fn&mber++ operator22(){ rea%Fpart22; ima9inar>Fpart22; ret&rn 6thi;3comp%e*Fn&mber comp%e*Fn&mber++ operator22(int *){

    22rea%Fpart; 22ima9inar>Fpart; ret&rn 6thi;3int main(){ comp%e*Fn&mber ob1; ob1:et?ata(H,0);

    comp%e*Fn&mber ob@; ob@:et?ata(10,1); 55co&t"Iefore addition"ob@:ho!(); comp%e*Fn&mber obJ; obJ:et?ata(@,J,D1); comp%e*Fn&mber obK; obK:et?ata(K,H,D1); comp%e*Fn&mber obH; co&t"Iefore addition, comp%e* n&mber are +"end%; obK:ho!();

  • 8/12/2019 C Lab Programs 2

    22/80

    obJ:ho!(); obH obK2obJ; co&t"#fter #ddition"; obH:ho!(); co&t"Iefore addition, comp%e* n&mber are +"end%;

    ob1:ho!(); obK:ho!(); obH ob1 2 obK; co&t"#fter #ddition"; obH:ho!(); co&t"Iefore addition, comp%e* n&mber are +"end%; ob@:ho!(); obK:ho!(); obH ob@2 obK; co&t"#fter #ddition"; obH:ho!();

    co&t"Iefore preincrement"end%; obH:ho!(); 22obH; co&t"#fter preincrement"end%; obH:ho!(); co&t"Iefore potincrement"end%; obH:ho!(); obH22; co&t"#fter potincrement"end%; obH:ho!(); co&t"Iefore /&btraction, comp%e* n&mber are +"end%;

    obK:ho!(); obJ:ho!(); obH obKDobJ; co&t"#fter /&btraction"; obH:ho!(); co&t"Iefore /&btraction, comp%e* n&mber are +"end%; ob1:ho!(); obK:ho!(); obH ob1 D obK; co&t"#fter /&btraction"; obH:ho!(); co&t"Iefore /&btraction, comp%e* n&mber are +"end%; ob@:ho!(); obK:ho!(); obH ob@D obK; co&t"#fter &btraction"; obH:ho!();

  • 8/12/2019 C Lab Programs 2

    23/80

    int i; cini; ret&rn 0;3

    print sum of two complex numbers

    56 The fo%%o!in9 code e*amp%e i taken from the book6 "The $22 /tandard Aibrar> D # T&toria% and eference"6 b> ico%ai : Lo&tti, #ddionDMe%e>, 1NNN66 ($) $op>ri9ht ico%ai : Lo&tti 1NNN:6 7ermiion to cop>, &e, modif>, e%% and ditrib&te thi oft!are6 i 9ranted provided thi cop>ri9ht notice appear in a%% copie:6 Thi oft!are i provided "a i" !itho&t e*pre or imp%ied

    6 !arrant>, and !ith no c%aim a to it &itabi%it> for an> p&rpoe:65 part+ J:0

    65 comp%e*%ouble c1(K:0,J:0);

    co&t "c1+ " c1 end%;

    56 create comp%e* n&mber from po%ar coordinate 6 D ma9nit&de+ H:0 6 D phae an9%e+ 0:OH 65 comp%e*float c@(po%ar(H:0,0:OH));

    56 print &m of c1 and c@+ 6 D note+ different t>pe 65 co&t "c1 2 c@+ " c1 2 comp%e*%ouble(c@:rea%(),c@:ima9()) end%;3

    c1+ (K,J)

  • 8/12/2019 C Lab Programs 2

    24/80

    c1 2 c@+ (O:PHQKK,P:K0Q1N)

    Write a program in c++ that %ivi%es two complex numbersQ

    class Komplex*privateH%ouble real imag,

    publicHKomplex () *real = #.#, imag = #.#,4

    Komplex (%ouble r %ouble i) *real = r, imag = i,4Komplex A%% (Komplex a Komplex b) *return Komplex (a.real + b.real a.imag + b.imag),4%ouble abs () *return sRrt (this'real 3 this'real + this'imag 3 this'imag),4Komplex ?ivision( Komplex a Komplex b)*write the fowrmula here4voi% show()*cout && real && - + - && imag && -i- &&en%l,4

    4,

    voi% main()*Komplex >um1(2E),

    Komplex >um2(E),Komplex 7esult,>um1.show(),>um2.show(),7esult.A%%(>um1>um2),7esult.show(),4

  • 8/12/2019 C Lab Programs 2

    25/80

    Write a K program to reverse the string without using strrev() functionQ

    mbo% in an arra> and &in9 the arra> andon of the %oop operator (for, !hi%e, do:::!hi%e and o on) p&t the arra> in o&tp&tin revere order:

    !ork on !indo!9et(a);for (i0;a[i]8G.0G;i22);iDD;for (j0;j i5@ ;j22){temp a[j];

  • 8/12/2019 C Lab Programs 2

    26/80

    a[j] a[iDj];a[iDj] temp;3printf("",a);ret&rn(0);

    3

    56$22 pro9ram to divide t!o comp%e* n&mber &in9 operator over%oadin965im9;3void dip%a>(){co&t*" 2 ">"i"end%;3void operator5(comp%e*);3;void comp%e*++operator5(comp%e* c){

    comp%e* temp; 55&meratortemp:**6c:*2>6(Dc:>)6(D1); 55ea% n&mber of the n&meratortemp:>*6(Dc:>)2>6c:*; 554ma9inar> n&mber of the n&meratorf%oat deno; 55?enominatordenoc:*6c:*Dc:>6c:>6(D1); 55/imi%ar to (a2b)(aDb)a@Db@

    co&ttemp:*" 2 "temp:>"i 5 "deno;3void main(){c%rcr();comp%e* c1(H,K),c@(J,@);c1:dip%a>();c@:dip%a>();co&t"?ividin9 the comp%e* n&mber"end%;c15c@;9etch();3

  • 8/12/2019 C Lab Programs 2

    27/80

    '=T7=T+H 2 KiJ 2 @i?ividin9 the comp%e* n&mber@J 2 @i 5 1J

    write program for palin%rome

  • 8/12/2019 C Lab Programs 2

    28/80

    trcp>(trtmp,trrc);trrev(trtmp);

    if(trcmp(trrc,trtmp)0)printf(".n Entered trin9 ."." ipa%indrome",trrc);

    e%eprintf(".n Entered trin9 ."." i notpa%indrome",trrc);9etch();3

    Write a K program to chec whether the string is palin%rome or notQ#include #include

    int main(int argc,char *argv[])

    {int strLen=,ind!"i(argv[$]==%&LL){return()"

    'hile((argv[$][strLen])= +){strLen"

    'or(ind! = "ind! < strLen-"ind!){

    i(argv[$][ind!] = argv[$][strLen/$/ind!]){0rint(12s %ot 0alindrome+n1,argv[$])"return()"

    ''0rint(12s 3alindrome1,argv[$])"return "

    '

    4r

    #include#include#deine si5e 6

    void main(){

  • 8/12/2019 C Lab Programs 2

    29/80

    char strsrc[si5e]"char strtm0[si5e]"clrscr()"

    0rint(1+n 7nter 8tring9= 1)"gets(strsrc)"

    strc0:(strtm0,stru0r(strsrc))"strrev(strtm0)"i(strcm0(strsrc,strtm0)==)

    0rint(1+n 7ntered string 12s1 is 0alindrome1,strsrc)"else

    0rint(1+n 7ntered string 12s1 is not 0alindrome1,strsrc)"getch()"

    '

    6rogram to Kalculate the 6ascal triangle

    $inclu%e&st%io.h'$inclu%e&conio.h'voi% main()*int aL1#ML1#M,int iNcn,clrscr(),printf(-nter how manP lines %o Pou want-),scanf(-0%-n),aL1ML1M=1,printf(-05%-aL1ML1M),

    aL2ML1M=1,aL2ML2M=2,aL2MLEM=1,printf(-0% 0% 0%-aL2ML1MaL2ML2MaL2MLEM),for(i=E,i&=n,i++) * aLiML1M=1, printf(-0%-aLiML1M), N=2,c=E, while(N&=i) * aLiMLNM=aLi1MLc1M+aLi1MLc2M, printf(-05%-aLiMLNM), c=c+1, N=N+1, 4aLiMLNM=1,printf(-0%-aLiMLNM),44

  • 8/12/2019 C Lab Programs 2

    30/80

    K program to print the 6ascals triangle

    $inclu%evoi% main()*int aL1#ML1#M,int iNcn,printf(-nter how manP lines %o Pou want-),scanf(-0%-n),aL1ML1M=1,printf(-05%-aL1ML1M),aL2ML1M=1,aL2ML2M=2,aL2MLEM=1,printf(-0% 0% 0%-aL2ML1MaL2ML2MaL2MLEM),for(i=E,i&=n,i++)*aLiML1M=1,printf(-0%-aLiML1M),

    N=2,c=E,while(N&=i)*aLiMLNM=aLi1MLc1M+aLi1MLc2M,printf(-05%-aLiMLNM),c=c+1,

    N=N+1,4aLiMLNM=1,printf(-0%-aLiMLNM),

    44

    @ow to write c co%e to generate pascal triangleQ@i the waP u explaine% the logic an% the program You have %one isperfect.Jut Nus two things You nee% to concentrate for printing the pPrami%in a right shape. have inclu%e% changes ..@ope it helps You.

    $inclu%e&st%io.h'$inclu%e&conio.h'

    /3program to generate pascal triangle no. of lines r to be entere% bP theuser3/voi% main()

  • 8/12/2019 C Lab Programs 2

    31/80

    *int abciNnrsncrfactrfactnfactnrline,printf(-please enter the number of lines of the pascal trianglen-),scanf(-0%-line), //inputs the no. of lines we wantclrscr(),

    for(i=#,i&=line1,i++)//outer loop for the value of n*factn=1,// reinitialiVing the factorial values to avoi% garbage values...factr=1,factnr=1,

    for(s=line1,s'=i,s) 55 L&t 4nc%&ded a or %oop T' have %eadin9 /pace*printf(- -),

    4

    for(N=#,N&=i,N++) //outer loop for the values of r*n=i, //here we have assigne% i to n an% N to r for our nKr calculation an%conviniencer=N,factn=1, // again we have re initialise% factorial values to avoi% garbagevalues

    factr=1,factnr=1,if(n==#) //we now factorial of # =1factn=1,else*for(a=n,a'=1,a) //factorial calculation of n is n!=#factn=factn3a,4if(r==#)factr=1,else*for(b=r,b'=1,b) //factorial calculation of r if r!=#factr=factr3b,4if((nr)==#)factnr=1,else

  • 8/12/2019 C Lab Programs 2

    32/80

    *for(c=(nr),c'=1,c) //factorial calculation of nr if (nr)!=#factnr=factnr3c,4ncr=factn/(factr3factnr), //ncr calculation

    printf(-02%-ncr), 55emove that tab and S4ven @d to have pace bet!eeneach character and print the va%&e of ncr4printf(-n-), //cursor get to the next line when the control exits the innerloop44

    K language program to print 6ascal ;riangleQ

  • 8/12/2019 C Lab Programs 2

    33/80

    K program of matrix a%%itionsubstractionan% multiplication

    /3 6rogram of a%%itionsubstractionan% multiplication of matrix 3/

    $inclu%e$inclu%evoi% rea%Tmat(int mint nint aL5ML5M)*int iN,for(i=#,i*for(N=#,N*printf(-n nter element H-),scanf(-0%-aLiMLNM),

    44voi% printTmat(int mint nint aL5ML5M)*int iN,for(i=#,i*for(N=#,Nprintf(- 0%-aLiMLNM),printf(-n-),4

    4voi% a%%Tmat(int mint nint aL5ML5Mint bL5ML5Mint sL5ML5M)*int iN,for(i=#,i*for(N=#,N*sLiMLNM=aLiMLNM+bLiMLNM,444voi% subTmat(int mint nint aL5ML5Mint bL5ML5Mint s%L5ML5M)*int iN,for(i=#,i*for(N=#,N*

  • 8/12/2019 C Lab Programs 2

    34/80

    s%LiMLNM=aLiMLNMbLiMLNM,444voi% mulTmat(int mint nint Rint aL5ML5Mint bL5ML5Mint mtL5ML5M)

    *int iN,for(i=#,i*for(N=#,N*mtLiMLNM=#,for(=#,*mtLiMLNM=mtLiMLNM+aLiMLM3bLMLNM,4

    444voi% main()*int mnpRiNaL5ML5MbL5ML5MsL5ML5Ms%L5ML5MmtL5ML5M,printf(-n nter or%er of first matrix H-),scanf(-0%0%-mn),printf(-n 8r%er of secon% matrix H-),scanf(-0%0%-pR),if((m!=p)(n!=R))

    printf(-n :atrices are imcompatable for a%%otion an% substraction-),else*printf(-n 7ea% the element of first matrix -),rea%Tmat(mna),printf(-n 7ea% the element of secon% matrix -),rea%Tmat(pRb),printf(-n ? :A;7D B ....n-),printTmat(pRb),a%%Tmat(mnabs),printf(-n A :A;7D B ....n-),printTmat(mns),subTmat(mnabs%),printf(-n A :A;7D B ....n-),printTmat(mns%),4if(p==R)

  • 8/12/2019 C Lab Programs 2

    35/80

    *mulTmat(mnRabmt),printf(-n A :A;7D B ....n-),printTmat(mRmt),4

    elseprintf(-n :atrices are incompatible for multiplication -),getch(),4

    8r

    a11 a1@ a1J #11 #1@ #1J a11*#112a1@*#@12a1J*#J1a11*#1@2a1@*#@@2a1J*#J@

    a@1 a@@ a@J * #@1 #@@ #@J a@1*#112a@@*#@12a@J*#J1a@1*#1@2a@@*#@@2a@J*#J@aJ1 aJ@ aJJ #J1 #J@ #JJ aJ1*#112aJ@*#@12aJJ*#J1

    :ore specific %etails are bePon% the scope of this article but Pou can refer:atrix multiplicationin wiipe%ia.

    The $ode

    Ko%eH Kvoid main(){ int m1[10][10],i,j,k,m@[10][10],add[10][10],m&%t[10][10],r1,c1,r@,c@; printf("Enter n&mber of ro! and co%&mn of firt matri* #C 10n"); canf("dd",-r1,-c1); printf("Enter n&mber of ro! and co%&mn of econd matri* #C 10n"); canf("dd",-r@,-c@); if(r@c1) { printf("Enter ro! and co%&mn of irt matri*n");

    printf("o! !ien"); for(i0;ir1;i22) { for(j0;jc1;j22) canf("d",-m1[i][j]); 3 printf("Bo& have entered the firt matri* a fo%%o!+n");

    http://en.wikipedia.org/wiki/Matrix_multiplicationhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://en.wikipedia.org/wiki/Matrix_multiplicationhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 8/12/2019 C Lab Programs 2

    36/80

    for(i0;ir1;i22) { for(j0;jc1;j22) printf("dt",m1[i][j]); printf("n");

    3 printf("Enter ro! and co%&mn of /econd matri*n"); printf("#9ain ro! !ien"); for(i0;ir@;i22) { for(j0;jc@;j22) canf("d",-m@[i][j]); 3 printf("Bo& have entered the econd matri* a fo%%o!+n"); for(i0;ir@;i22) {

    for(j0;jc@;j22) printf("dt",m@[i][j]); printf("n"); 3 if(r1r@--c1c@) { printf("o! !e add both the above matri*n"); printf("The re&%t of the addition i a fo%%o!;n"); for(i0;ir1;i22) { for(j0;jc1;j22)

    { add[i][j]m1[i][j]2m@[i][j]; printf("dt",add[i][j]); 3 printf("n"); 3 3 e%e { printf("#ddition cannot be done a ro! or co%&mn are not eq&a%n"); 3

    printf("o! !e m&%tip%> both the above matri*n"); printf("The re&%t of the m&%tip%ication i a fo%%o!+n"); -*a$$!;$$a$!;$a$!;$ a$$!;$a$!;a$!;a$$!;$a$!;a$!;*- for(i0;ir1;i22) { for(j0;jc@;j22)

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 8/12/2019 C Lab Programs 2

    37/80

    { m&%t[i][j]0; for(k0;kr1;k22) { m&%t[i][j]2m1[i][k]6m@[k][j];

    -*mult[][]=m$[][]*m[][]m$[][$]*m[$][]m$[][]*m[][]"*- 3 printf("dt",m&%t[i][j]); 3 printf("n"); 3 9etch(); 3 e%e {

    printf("atri* m&%tip%ication cannot be done"); 33

    K program for an Armstrong number

    voi% main()*int nb=#t,clrscr(),

    printf(Xnter the no),scanf(X0%n),t=n,while(n'#)*a=n01#,b=b+a3a3a,n=n/1#,4if(b==t)*

    printf(XArmstrong no),4else*printf(X>ot an armstrong no),4getch(),

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 8/12/2019 C Lab Programs 2

    38/80

    4

    write a program to fin% a given number is arms...

    main()

    {

    int amm 0 temp 0 n *;

    printf( enter thea%&e of n );

    canf( d -n);

    * n;

    for(;n0;)

    {

    temp n 10;

    amm amm2(temp6temp6temp);

    n n510;

    3

    if(* n)

    {

    printf( TUE S4VE =IE 4/ #/T'S =IE );

    3

    e%e

    printf( 'T #/T'S =IE );

    3

  • 8/12/2019 C Lab Programs 2

    39/80

    Write a program to print all Armstrong number between 1 an% 5## f sum ofcubes of each %igit of the number it self then the number is calle% anArmstrong numberQ

    567ro9ram to find armtron9 n&mber bet!een 1 to H0065

    int main() {int i,a,b,c,*,>;c%rcr();for (i0;iH00;i22){ci10;*i510;b*10;**510;a*10;

    d(a6a6a)2(b6b6b)2(c6c6c);if (id)printf("d.n",i);39etch();3

    Armstrong number program in c languageQ

  • 8/12/2019 C Lab Programs 2

    40/80

    e%eprintf("not amtron9");9etch();3

    Write a program in c to sort an unsorte% arraP using bubble sort metho%Q

  • 8/12/2019 C Lab Programs 2

    41/80

    voi% bubble(int aLMint n) * int iNt, for(i=n2,i'=#,i) *

    for(N=#,N&=i,N++)

    * if(aLNM'aLN+1M) * t=aLNM, aLNM=aLN+1M, aLN+1M=t, 4 4

    4//en% for 1.

    4//en% function.

    voi% main() *

    int aL1##Mni,

    clrscr(),

    printf(-nn nter integer value for total no.s of elements to be sorte%H-), scanf(-0%-n),

    for( i=#,i&=n1,i++) * printf(-nn nter integer value for element no.0% H -i+1), scanf(-0%-aLiM), 4

    bubble(an),

    printf(-nn

  • 8/12/2019 C Lab Programs 2

    42/80

    /3

    BA:6G 8C;6C;

    nter integer value for total no.s of elements to be sorte%H

    nter integer value for element no.1 H IO

    nter integer value for element no.2 H

    nter integer value for element no.E H F

    nter integer value for element no. H 5

    nter integer value for element no.5 H FI

    nter integer value for element no. H 11

  • 8/12/2019 C Lab Programs 2

    43/80

    binarP search program in c language

    $inclu%e$inclu%e

    voi% main()*int arraPL1#M,int i N > temp ePnum,int lowmi%high,

    clrscr(),

    printf(-nter the value of >n-),scanf(-0%->),

    printf(-nter the elements one bP onen-),for(i=#, i*scanf(-0%-arraPLiM),4printf(-nput arraP elementsn-),for(i=#, i*printf(-0%n-arraPLiM),4

    /3 Jubble sorting begins 3/

    for(i=#, i& > , i++)*for(N=#, N& (>i1) , N++)*if(arraPLNM ' arraPLN+1M)*temp = arraPLNM,arraPLNM = arraPLN+1M,arraPLN+1M = temp,444printf(-Borte% arraP is...n-),for(i=#, i*printf(-0%n-arraPLiM),4

    printf(-nter the element to be searche%n-),

  • 8/12/2019 C Lab Programs 2

    44/80

    scanf(-0%- ePnum),

    /3 JinarP searching begins 3/

    low=1,

    high=>,

    %o*mi%= (low + high) / 2,if ( ePnum & arraPLmi%M )high = mi% 1,else if ( ePnum ' arraPLmi%M)low = mi% + 1,4 while( ePnum!=arraPLmi%M low &= high), /3 n% of %o while 3/

    if( ePnum == arraPLmi%M )*printf(-BCKKBB9 K 67897A:

    voi% main()*int aL1#Minmclu,clrscr(),printf(-nter the siVe of an arraP'-),scanf(-0%-n),printf(-nnter the elements of the arraP'-),for(i=#,i-),for(i=#,i-),

    scanf(-0%-m),l=#u=n1,c=binarP(anmlu),if(c==#)printf(-n;he number is not in the list-),elseprintf(-n;he number is foun%-),getch(),

    http://cquestionbank.blogspot.com/2008/01/c-program-for-binary-search.htmlhttp://cquestionbank.blogspot.com/2008/01/c-program-for-binary-search.html
  • 8/12/2019 C Lab Programs 2

    45/80

    4int binarP(int aLMint nint mint lint u)*int mi%c=#,if(l&=u)

    *mi%=(l+u)/2,if(m==aLmi%M)*c=1,4elseif(m*return binarP(anmlmi%1),4

    elsereturn binarP(anmmi%+1u),4elsereturn c,4

    C PROGRAM FOR BINARY SEARCH

    void main(){int a[10],i,n,m,c,%,&;c%rcr();printf("Enter the iRe of an arra>D");canf("d",-n);printf(".nEnter the e%ement of the arra>D");for(i0;i{canf("d",-a[i]);3printf(".nThe e%ement of an arra> areD");for(i0;i{printf(" d",a[i]);3printf(".nEnter the n&mber to be earchD");canf("d",-m);%0,&nD1;cbinar>(a,n,m,%,&);if(c0)printf(".nThe n&mber i not in the %it");

  • 8/12/2019 C Lab Programs 2

    46/80

    e%eprintf(".nThe n&mber i fo&nd");9etch();3int binar>(int a[],int n,int m,int %,int &)

    {int mid,c0;if(%&){mid(%2&)5@;if(ma[mid]){c1;3e%eif(m

    {ret&rn binar>(a,n,m,%,midD1);3e%eret&rn binar>(a,n,m,mid21,&);3e%eret&rn c;3

    Iinar> /earch $ 7ro9ram

    $inclu%e &st%io.h'

    $%efine ;7C #$%efine

  • 8/12/2019 C Lab Programs 2

    47/80

    printf(-nBearch for >umberH -),scanf(-0%- number),

    while(bsearch == umber

  • 8/12/2019 C Lab Programs 2

    48/80

    mergeTsort(arP # :ADTA7Y 1),

    /3 arraP after mergesort 3/printf(-After :erge Bort H-),for(N = #, N & :ADTA7Y, N++)

    printf(- 0%- arPLNM),

    printf(-n-),getch(),4

    /3 :etho% to implement :erge Bort3/voi% mergeTsort(int xLM int en% int start) *int N = #,const int siVe = start en% + 1,int mi% = #,

    int mrg1 = #,int mrg2 = #,int executingL:ADTA7YM,

    if(en% == start)return,

    mi% = (en% + start) / 2,

    mergeTsort(x en% mi%),mergeTsort(x mi% + 1 start),

    for(N = #, N & siVe, N++)executingLNM = xLen% + NM,

    mrg1 = #,mrg2 = mi% en% + 1,

    for(N = #, N & siVe, N++) *if(mrg2 &= start en%)if(mrg1 &= mi% en%)if(executingLmrg1M ' executingLmrg2M)xLN + en%M = executingLmrg2++M,elsexLN + en%M = executingLmrg1++M,elsexLN + en%M = executingLmrg2++M,elsexLN + en%M = executingLmrg1++M,4

  • 8/12/2019 C Lab Programs 2

    49/80

    4

    er9e /ort $ 7ro9ram

    $inclu%e &st%io.h'

    $inclu%e &st%lib.h'

    struct no%e *int number,struct no%e 3next,4,

    /3 a%% a no%e to the line% list 3/struct no%e 3a%%no%e(int number struct no%e 3next),

    /3 preform merge sort on the line% list 3/struct no%e 3mergesort(struct no%e 3hea%),

    /3 merge the lists.. 3/struct no%e 3merge(struct no%e 3hea%Tone struct no%e 3hea%Ttwo),

    int main(voi%) *struct no%e 3hea%,struct no%e 3current,struct no%e 3next,int testLM = *I E 2 1 5 F O #4,int i,

    hea% = >CGG,

    /3 insert some numbers into the line% list 3/for(i = #, i & 1#, i++) hea% = a%%no%e(testLiM hea%),

    /3 sort the list 3/hea% = mergesort(hea%),

    /3 print the list 3/printf(- before aftern-) i = #,for(current = hea%, current != >CGG, current = current'next) printf(-0%t0%n- testLi++M current'number),

    /3 free the list 3/for(current = hea%, current != >CGG, current = next) next = current'next free(current),

    /3 %one... 3/return #,4

  • 8/12/2019 C Lab Programs 2

    50/80

    /3 a%% a no%e to the line% list 3/struct no%e 3a%%no%e(int number struct no%e 3next) *struct no%e 3tno%e,

    tno%e = (struct no%e3)malloc(siVeof(3tno%e)),

    if(tno%e != >CGG) * tno%e'number = number, tno%e'next = next,4

    return tno%e,4

    /3 preform merge sort on the line% list 3/

    struct no%e 3mergesort(struct no%e 3hea%) *struct no%e 3hea%Tone,struct no%e 3hea%Ttwo,

    if((hea% == >CGG) [[ (hea%'next == >CGG))return hea%,

    hea%Tone = hea%,hea%Ttwo = hea%'next,while((hea%Ttwo != >CGG) (hea%Ttwo'next != >CGG)) * hea% = hea%'next,

    hea%Ttwo = hea%'next'next,4hea%Ttwo = hea%'next,hea%'next = >CGG,

    return merge(mergesort(hea%Tone) mergesort(hea%Ttwo)),4

    /3 merge the lists.. 3/struct no%e 3merge(struct no%e 3hea%Tone struct no%e 3hea%Ttwo) *struct no%e 3hea%Tthree,

    if(hea%Tone == >CGG)return hea%Ttwo,

    if(hea%Ttwo == >CGG)return hea%Tone,

    if(hea%Tone'number & hea%Ttwo'number) *

  • 8/12/2019 C Lab Programs 2

    51/80

    hea%Tthree = hea%Tone, hea%Tthree'next = merge(hea%Tone'next hea%Ttwo),4 else * hea%Tthree = hea%Ttwo, hea%Tthree'next = merge(hea%Tone hea%Ttwo'next),

    4

    return hea%Tthree,4

    Write a K program to convert %ecimal number into binarP numberQhave here co%e to convert %ecimal to binarP number octal an% hexa inK..here is the co%eH

    $inclu%e &st%io.h'

    voi% main()*voi% welcome(),voi% co%e(),int %,int i=#nNbL1##M,

    printf(-nnttKhooseHn-),printf(-nnttL1M ?ecimal to JinarPn-),printf(-ttL2M ?ecimal to 8ctaln-),

    printf(-ttLEM ?ecimal to @exa%ecimaln-),printf(-ttLM xitn-),

    printf(-nnttnter Pour choiceH -),scanf(-0%- %),

    switch(%)*case 1H

    printf(-nttnter %ecimal numberH -),scanf(-0%- n),

    while (n'#)*bLiM=n02,n=n/2,i++,

  • 8/12/2019 C Lab Programs 2

    52/80

    4

    printf(-nttJinarP isH -),N=i1,

    for (i=N,N'=#,N)*printf(-0%- bLNM),

    4

    brea,

    case 2Hprintf(-nttnter %ecimal numberH -),scanf(-0%- n),

    while (n'#)*bLiM=n0I,n=n/I,i++,4

    printf(-ntt8ctal isH-),N=i1,

    for (i=N,N'=#,N)*printf(-0%- bLNM),4

    brea,

    case EHprintf(-nttnter %ecimal numberH -),scanf(-0%- n),

    while (n'#)*bLiM=n01,n=n/1,i++,4

    printf(-ntt@exa%ecimal isH-),

  • 8/12/2019 C Lab Programs 2

    53/80

    N=i1,

    for (i=N,N'=#,N)*printf(-0%- bLNM),

    if(bLNM&1#)*printf(-0%- bLNM),4else*switch(bLNM)*case 1#Hprintf(-A-),

    brea,case 11Hprintf(-J-),brea,case 12Hprintf(-K-),brea,case 1EHprintf(-?-),brea,case 1H

    printf(--),brea,case 15Hprintf(- trin9@: 55 added a tet printf() >o& can remove %aterJ: 55 T&rbo $ modified for 7e%%e $ ve9aeat 1Nnov@00K

  • 8/12/2019 C Lab Programs 2

    54/80

    K:H: );Q:

    O. intmain()1#.*11. longdecima%;12. charbinar>LQ0M;1J:1. printf("nnEnter an inte9er va%&e + ");15. canf("%d",-decima%);1. dec@bin(decima%,binar>);1F. printf("nThe binar> va%&e of %d i n",decima%,binar>);1Q:1O. 9etchar(); 55 trap enter

    2#. 9etchar(); 55 !ait21. return0;22.4@J:@K:55@H:55 accept a decima% inte9er and ret&rn a binar> coded trin9@P:552F.voi%dec@bin(longdecima%, char6binar>)2I.*2O. int k 0, n 0;E#. int ne9Ff%a9 0;

    E1. int remain;E2. int o%dFdecima%; 55 for tetEE. chartempLQ0M;JK:JH: 55 take care of ne9ative inp&tE. if(decima% 0)EF. *JQ: decima% Ddecima%;JN: ne9Ff%a9 1;#. 41. %o2. *KJ: o%dFdecima% decima%; 55 for tetKK: remain decima% @;KH: 55 !hitt%e do!n the decima% n&mberKP: decima% decima% 5 @;KO: 55 thi i a tet to ho! the actionI. printf("d5@ d remainder dn", o%dFdecima%, decima%, remain);KN: 55 convert di9it 0 or 1 to character G0G or G1G

  • 8/12/2019 C Lab Programs 2

    55/80

    5#. tempLk22M remain 2 G0G;51. 4while(decima% 0);H@:5E. if(ne9Ff%a9)5. tempLk22M GDG; 55 add D i9n

    55. else5. tempLk22M G G; 55 paceHO:HQ: 55 revere the pe%%in95O. while(k 0)#. binar>Ln22M tempLDDkM;P1:2. binar>LnD1M 0; 55 end !ith =AA

    E.4

    Write a program to convert the binarP to %ecimal conversion bP using whilestatementQ

    $inclu%e&st%io.h'$inclu%e&conio.h'$inclu%e&math.h'voi% main()*long int aL2#Mincount=#bL2#McL2#Msum=#,printf(->ter the number in binarP form=t-),scanf(-0l%-n), // 9et a binarP number from the user

    for (i=#,n'=1,i++)*aLiM=n01#,n=n/1#, // Goop ;o reverse the number An% put all reverse% numbers in arrPaLiMcount=count + 1, // count to count the number of times this loop runs4for (i=#,i&=count1,i++) // count 1 con%ition is use% to run the loop till theprevious loop run*bLiM=pow(2i), // ;his is to raise the power of 2 to no of times previous looprunne%.4for (i=#,i&=count1,i++)*cLiM=aLiM 3 bLiM, // :ultiplP aLiM or reverese% binarP no with bLiM or increasingpow of 2 to count1sum=sum +cLiM, // it is to a%% the cLiM elements with each other n put intosum variable.

  • 8/12/2019 C Lab Programs 2

    56/80

    4printf(-?ecimal form =0l%-sum), // printing the sum to get the %ecimalformgetch(),4

    K programming co%e for newton raphson metho% to solve Rua%raticeRuationQ

    >eThi i the code for e!tonDaphon method for o%vin9 W&adratic eq&ation

  • 8/12/2019 C Lab Programs 2

    57/80

    39etch();3

    3 6rogram to fin% the roots of an Ruation f(x)=# using >ewton 7aphson:etho% 3/$inclu%e$inclu%e$inclu%efloat f(float),float %f(float),voi% main()*float x#x1%eps,int in,

    clrscr(),printf(-nnput the intial values H -),scanf(-0f-x#),printf(-nnter the number of terations -),scanf(-0%-n),printf(-nnput the error of tolerancen-),scanf(-0f-eps),printf(-terationt x#tt x1-),for(i=#,i&>,++)*x1=x#f(x#)/%f(x#),

    printf(-nt 0%t0ft0fn-ix#x1),%=x1x#,x#=x1,if(%==eps)*brea,44printf(-n7oots of given eRuation = 0f-x#),getch(),4

    float f(float x)*float f,f=cos(x)x3exp(x),return(f),4

  • 8/12/2019 C Lab Programs 2

    58/80

    float %f(float x)*float %f,%f=sin(x)x3exp(x)exp(x),return(%f),

    4What is the c programming co%e for newton raphson eRuationQ

    56pro9ramm for modified ne!ton raphon method65#include

    #include

    #define epsilon 1e-6

    main()

    {

    /*This is a programm ased on modified

    ne!ton raphson method to

    find out root of the e"uation $%-&*$'*-%+*/

    doule g,g1,,1,',,d

    int conerged+,i

    printf(pl0 enter the guess aluen)

    scanf(2f,3g1)

    i1

    !hile (conerged+)

    {

    printf(n iteration no2dn,i)

    g1*g1*g1-&*g1*g1*g1-%

    printf(2lfn,)

    1%*g1*g1-1+*g1printf(12lfn,1)

    '6*g1-1+

    printf('2lfn,')

    (*1)/(1*1-*')

    printf(alue2lfn,)

    gg1-((*1)/(1*1-*'))

    printf(ne! guess is2lfn,g)

    d((g-g1)/g)

    printf(error2lfn,d)

    g1g

    if (fas(d)4less than4epsilon)

    {conerged15

    printf(the root of e"uation is2lf

    i2dn,g,i)

    ii1

    5

    5

  • 8/12/2019 C Lab Programs 2

    59/80

    6rg. to convert upper case to lower case or lower case to upper case%epen%ing on the name it is invoe% with as foun% in argument.

  • 8/12/2019 C Lab Programs 2

    60/80

    3printf("

    U#VE # 4$E ?#B8 IBE:");9etch();3void &pperFtoF%o!er(){ int i,j; char cK[Q0],cJ; for (i0;(cJ9etchar())8E';i22)

    cK[i](cJG#G -- cJGXG)(GaG 2 cJ DG#G)+cJ; printf("

    The %o!er cae eq&iva%ent i "); for (j0;ji;j22)p&tchar(cK[j]);

    ret&rn;3

    void %o!erFtoF&pper(){ int i,j; char c@[Q0],c1; for (i0;(c19etchar())8E';i22)

    c@[i](c1GaG -- c1GRG)(G#G 2 c1 DGaG)+c1; printf("The &pper cae eq&iva%ent i "); for (j0;ji;j22)

    p&tchar(c@[j]); ret&rn;3

    Konvert string to upper case an% lower case

    $inclu%e &ctPpe.h'$inclu%e &st%io.h'

    int main(voi%)*

    char strLI#M, int i,

  • 8/12/2019 C Lab Programs 2

    61/80

    printf(-nter a stringH -), gets(str),

    for( i = #, strL i M, i++) strL i M = toupper( strL i M ),

    printf(-0sn- str), /3 uppercase string 3/

    for(i = #, strL i M, i++) strLiM = tolower(strL i M),

    printf(-0sn- str), /3 lowercase string 3/

    return #,}

    @ow %o Pou write a K program to input a string of lowercase alphabets an%convert it to uppercaseQUere i pro9ram code !ritten in p%ain $+

  • 8/12/2019 C Lab Programs 2

    62/80

  • 8/12/2019 C Lab Programs 2

    63/80

    /3 f K@ is alrea%P in lower case return it 3/if ( Tch == #x [[ Tch == #xF )

    return ch,

    /3 convert it to lower case 3/

    if ( Tch == #x )return (int)( ( (unsigne% char)ch #x#f ) [ #x# ),elseif ( Tch == #x5 )

    return (int)( ( (unsigne% char)ch #x#f ) [ #xF# ),else

    return ch,4

    int Ttoupper ( int ch )*

    unsigne% char Tch = (unsigne% char)ch '' ,

    /3 f K@ is alrea%P in upper case return it 3/if ( Tch == #x [[ Tch == #x5 )

    return ch,

    /3 convert it to upper case 3/if ( Tch == #x )

    return (int)( ( (unsigne% char)ch #x#f ) [ #x# ),elseif ( Tch == #xF )

    return (int)( ( (unsigne% char)ch #x#f ) [ #x5# ),elsereturn ch,

    4

    int main ( voi% )*

    printf ( -0cn- Ttoupper ( U+U ) ),printf ( -0cn- Ttoupper ( UrU ) ),printf ( -0cn- Ttoupper ( UlU ) ),printf ( -0cn- Ttoupper ( U6U ) ),

    printf ( -0cn- Ttolower ( U\U ) ),printf ( -0cn- Ttolower ( U7U ) ),printf ( -0cn- Ttolower ( U8U ) ),printf ( -0cn- Ttolower ( UOU ) ),

  • 8/12/2019 C Lab Programs 2

    64/80

    return #,4

    K program to implement the linear regression algorithm

    $inclu%e$inclu%e$inclu%e$inclu%e

    float mean(float 3a int n),voi% %eviation(float 3a float mean int n float 3% float 3B),

    voi% main()*float aL2#MbL2#M%xL2#M%PL2#M,float sP=#sx=#meanTx=#meanTP=#sumTxP=#,float corrTcoff=#regTcoffTxP=# regTcoffTPx=#,char tPpeTcoffLFM,int n=#i=#,

    clrscr(),

    printf(-nter the value of nH -),scanf(-0%-n),printf(-nter the values of x an% PHn-),

    for(i=#,i&>,++)scanf(-0f0f-aLiMbLiM),meanTx=mean(an),meanTP=mean(bn),%eviation(ameanTxn%xsx),%eviation(bmeanTPn%PsP),

    for(i=#,i&>,++)sumTxP=sumTxP+%xLiM3%PLiM,corrTcoff=sumTxP/(n3sx3sP),printf(-nter the tPpe of regression coefficient as Ux on PU or UP on xUH -),

    fflush(st%in),gets(tPpeTcoff),

    if(strcmp(tPpeTcoff-x on P-)==1)*regTcoffTxP=corrTcoff3(sx/sP),printf(-n;he value of linear regression coefficient is 0f-regTcoffTxP),4

  • 8/12/2019 C Lab Programs 2

    65/80

    else if(strcmp(tPpeTcoff-P on x-)==1)*regTcoffTPx=corrTcoff3(sP/sx),printf(-n;he value of linear regression coefficient is 0f-regTcoffTPx),4

    elseprintf(-nnter the correct tPpe of regression coefficient.-),getch(),4

    float mean(float 3a int n)*float sum=# i=#,for(i=#,i&>,++)sum=sum+aLiM,

    sum=sum/n,return (sum),4

    voi% %eviation(float 3a float mean int n float 3% float 3s)*float sum=#t=#,int i=#,for(i=#,i&>,++)*%LiM=aLiMmean,

    t=%LiM3%LiM,sum=sum+t,4sum=sum/n,3s=sRrt(sum),4

    K program that implement nsertion sort to sort a given list of integers in

    ascen%ing or%er

    $inclu%e$inclu%e

    voi% instTsort(int LM),

    voi% main()*

  • 8/12/2019 C Lab Programs 2

    66/80

    int numL5Mcount,clrscr(),printf(-nnter the

  • 8/12/2019 C Lab Programs 2

    67/80

    6 the %inear >tem !ind &p tored in the arra> rh; create a cop> to pa to6 the f&nction if >o& !ih to retain the ori9ina% U/ arra>:66 7ain9 the identit> matri* a the rh ar9&ment re&%t in the invere of6 matri* #, if it e*it:

    66 o %ibrar> or header dependencie, b&t req&ire the f&nction !apro!, !hich6 i inc%&ded here:

    65

    55 !apro! D e*chan9e the content of ro!0 and ro!1 in a @d arra>void !apro!(do&b%e66 arr, %on9 ro!0, %on9 ro!1) { do&b%e6 temp; temparr[ro!0]; arr[ro!0]arr[ro!1]; arr[ro!1]temp;

    3

    55 9je%imvoid 9je%im(do&b%e66 %h, do&b%e66 rh, %on9 nro!, %on9 nco%rh) {

    55 a&9ment %h arra> !ith rh arra> and tore in arr@ do&b%e66 arr@ne! do&b%e6[nro!]; for (%on9 ro!0; ro!nro!; 22ro!) arr@[ro!]ne! do&b%e[nro!2nco%rh];

    for (%on9 ro!0; ro!nro!; 22ro!) {

    for (%on9 co%0; co%nro!; 22co%) { arr@[ro!][co%]%h[ro!][co%]; 3 for (%on9 co%nro!; co%nro!2nco%rh; 22co%) { arr@[ro!][co%]rh[ro!][co%Dnro!]; 3 3

    55 perform for!ard e%imination to 9et arr@ in ro!Deche%on form for (%on9 dinde*0; dinde*nro!; 22dinde*) { 55 r&n a%on9 dia9ona%, !appin9 ro! to move Rero in !orkin9 poition

    55 (a%on9 the dia9ona%) do!n!ard if ( (dinde*(nro!D1)) -- (arr@[dinde*][dinde*]0)) { ret&rn; 55 no o%&tion 3 e%e if (arr@[dinde*][dinde*]0) { !apro!(arr@, dinde*, dinde*21); 3 55 divide !orkin9 ro! b> va%&e of !orkin9 poition to 9et a 1 on the 55 dia9ona%

  • 8/12/2019 C Lab Programs 2

    68/80

    if (arr@[dinde*][dinde*] 0:0) { ret&rn; 3 e%e { do&b%e tempva%arr@[dinde*][dinde*]; for (%on9 co%0; co%nro!2nco%rh; 22co%) {

    arr@[dinde*][co%]5tempva%; 3 3

    55 e%iminate va%&e be%o! !orkin9 poition b> &btractin9 a m&%tip%e of55 the c&rrent ro!

    for (%on9 ro!dinde*21; ro!nro!; 22ro!) { do&b%e !va%arr@[ro!][dinde*]; for (%on9 co%0; co%nro!2nco%rh; 22co%) { arr@[ro!][co%]D!va%6arr@[dinde*][co%]; 3

    3 3

    55 back!ard &btit&tion tep for (%on9 dinde*nro!D1; dinde*0; DDdinde*) { 55 e%iminate va%&e above !orkin9 poition b> &btractin9 a m&%tip%e of

    55 the c&rrent ro! for (%on9 ro!dinde*D1; ro!0; DDro!) { do&b%e !va%arr@[ro!][dinde*]; for (%on9 co%0; co%nro!2nco%rh; 22co%) { arr@[ro!][co%]D!va%6arr@[dinde*][co%];

    3 3 3

    55 ai9n re&%t to rep%ace rh for (%on9 ro!0; ro!nro!; 22ro!) { for (%on9 co%0; co%nco%rh; 22co%) { rh[ro!][co%]arr@[ro!][co%2nro!]; 3 3

    for (%on9 ro!0; ro!nro!; 22ro!) de%ete[] arr@[ro!]; de%ete[] arr@;3

    566666666666666666666666666666666666666666666666666666666666666666666666666666 Sa&F/eide%@:cpp 66 6

  • 8/12/2019 C Lab Programs 2

    69/80

    6 /o%ve a >tem of %inear eq&ation &in9 Sa&D/eide% iteration 66 666 amp%e tet that i &e i matri* # O D@ 1 @ I J 6

    6 @ Q J 1 D@ 6

    6 D1 0 H @ H 66 0 @ D1 K K 66 66 the o%&tion for * ho&%d be print %ike thi + D0:@ D0:H 0:K 1:K 666666666666666666666666666666666666666666666666666666666666666666666666666665

    Frank, int proc){ int i, j; f%oat temp[#C][#C]; int nFbar;

    nFbar n5proc;

    55 i%% d&mm> entrie in temp !ith Reroe for (i 0; i n; i22) for (j n; j #C; j22) temp[i][j] 0:0;

    if (m>Frank 0) {

  • 8/12/2019 C Lab Programs 2

    70/80

    printf(".n", prompt); for (i 0; i n; i22) for (j 0; j n; j22) canf("f",-temp[i][j]); 3

    55/catter temp to # from proce 0 74F/catter(temp, nFbar6#C, 74FA'#T, #, nFbar6#C, 74FA'#T, 0,74F$'FM'A?);355Thi !i%% read the vector *void eadFvector(char6 prompt, f%oat *[], int n, int m>Frank, int proc){ int i; f%oat temp[#C]; int nFbar;

    nFbar n5proc;

    if (m>Frank 0){

    printf(".n", prompt); for (i 0; i n; i22) canf("f", -temp[i]); 3 55/catter temp to * from proce 0 74F/catter(temp, nFbar, 74FA'#T, *, nFbar, 74FA'#T, 0,74F$'FM'A?);

    3

    void main(int ar9c, char6 ar9v[]){

    55ain 7roce int proc; int m>Frank; f%oat #[#C][#C]; f%oat *[#C]; f%oat I[#C]; int n; 55%oat to%erance; int ma*Fiteration;

    55Sa&D/eide% 4teration &nction int iteration 0; int i, j; f%oat %atFiteration[#C]; f%oat i9ma1, i9ma@;

  • 8/12/2019 C Lab Programs 2

    71/80

    f%oat preceion; int k;

    55?ip%a>in9 arra>int i;

    f%oat temp[#C];

    74F4nit(-ar9c, -ar9v); 74F$ommFiRe(74F$'FM'A?, -proc); 74F$ommFrank(74F$'FM'A?, -m>Frank);

    if (m>Frank 0){

    >tem("c%"); printf("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD.n"); printf("7ro9ram !i%% perform Sa&D/eide% 4teration ethod to o%ve

    Ainear Eq&ation .n"); printf("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD.n");printf("Enter variab%e n and ma* n&mber of iteration+ .n.n");

    canf("d d", -n, -ma*Fiteration); 3 55Iroadcat n - ma*im&m iteration to a%% other proce 74FIcat(-n, 1, 74F4T, 0, 74F$'FM'A?); 74FIcat(-ma*Fiteration, 1, 74F4T, 0, 74F$'FM'A?);

    nFbar n5proc;

    55i%% the matri* and the vector eadFmatri*(".n4nert the variab%e for the matri* # .n", #, n, m>Frank, proc); eadFvector(".nEnter the ri9htDhand ide of I .n", I, n, m>Frank, proc);

    55 4nitia%iRe *

    74F#%%9ather(I, nFbar, 74FA'#T, k, nFbar, 74FA'#T,74F$'FM'A?);

    55$a%c&%ate req&eted preciion and ma*im&m iteration preceion po!(10:0, D(f%oat)k); ma*Fiteration (int)po!(10:0, (f%oat)k);

    55 4nitia%iRe %atFiteration matri* to Rero for (i0; i#C; i22) %atFiteration[i] 0:0;

    55$a%c&%ate iteration

  • 8/12/2019 C Lab Programs 2

    72/80

    do { 55$a%c&%ate the ne*t iteration for (i0; i#C; i22) {

    55$omp&te /i9ma(j+ji)(#(i,j) 6 *(j)(n)) i9ma1 0:0; for (j0; ji; j22) i9ma1 2 #[i][j] 6 *[j];

    55$omp&te /i9ma(j+ji)(#(i,j) 6 *(j)(nD1)) i9ma@ 0:0; for (ji21; j#C; j22) i9ma@ 2 #[i][j] 6 %atFiteration[j];

    55$omp&te the va%&e of thi iteration

    *[i] (I[i] D i9ma1 D i9ma@) 5 #[i][i]; 3

    55i9&re o&t if !eGre done >etdone T=E;

    for (i0; i#C; i22) done - fab(*[i] D %atFiteration[i]) (preceion 6 fab(*[i]));

    55if an> entr> i 9reater than EAEETF#C, an overf%o! ha occ&rred,oabort

    for (i0; i#C; i22) if ((*[i] EAEETF#C) YY (*[i] DEAEETF#C)) ret&rn 0;

    554ncrement the iteration co&ntiteration22;

    554f iteration co&nt e*ceed ma*im&m n&mber of iteration, abort if (iteration ma*Fiteration) ret&rn 0;

    55$op> the c&rrent iteration to the %at iteration vector (void)memcp>(%atFiteration, *, iReof(f%oat) 6 #C); 3 !hi%e (8done);

    56 !e !ere &ccef&% 65 ret&rn iteration; 3

  • 8/12/2019 C Lab Programs 2

    73/80

    74F#%%9ather(*, nFbar, 74FA'#T, k, nFbar, 74FA'#T,74F$'FM'A?); 74FSather(*, nFbar, 74FA'#T, temp, nFbar, 74FA'#T, 0,74F$'FM'A?); if(m>Frank0)

    { printf(".nd) ",iterationFn&m);for (i 0; i n; i22)

    printf(".tK:Hf ", temp[i]);printf(".n");

    3

    3!hi%e ((iterationFn&m ma*Fiteration) -- (?itance(k,n) preceion));

    if (?itance(k,n) preceion)

    { 74FSather(*, nFbar, 74FA'#T, temp, nFbar, 74FA'#T, 0,74F$'FM'A?);

    if (m>Frank 0){

    printf(".nThe o%&tion for Sa&D/eide% 4teration ethodi.n"); 55?ip%a> /o%&tion

    for (i 0; i n; i22)printf("K:1f ", temp[i]);

    printf(".n");

    3 3

    e%e if (m>Frank 0)

    printf("ai%ed to conver9e in d iteration.n", ma*Fiteration);

    74Fina%iRe(); 55End 7roce3

    Write a c program to implement tower of hanoiQ

  • 8/12/2019 C Lab Programs 2

    74/80

    if(*1){printf("ove ?ik rom c to c.n",from,to);3e%e

    {hanoi(*D1,from,a&*,to);printf("ove ?ik rom c to c.n",from,to);hanoi(*D1,a&*,to,from);3

    3void main(){int dik;int move;

    c%rcr();printf("Enter the n&mber of dik >o& !ant to p%a> !ith+");canf("d",-dik);movepo!(@,dik)D1;printf(".nThe o of move req&ired id .n",move);hanoi(dik,G#G,G$G,GIG);9etch();3

    Write the program of tower of @anoi in c languageQ56 hanoi:c 65

  • 8/12/2019 C Lab Programs 2

    75/80

    if (ar9c1 YY (n atoi(ar9v[1]))0) n H;

    tep 0;Uanoi (n, 1, @, J);ret&rn 0;

    3

    K programming of 7ungeSutta metho%Q

    67897A: H

    /3 7unge Sutta for a set of first or%er %ifferential eRuations 3/

    $inclu%e &st%io.h'$inclu%e &math.h'

    $%efine > 2 /3 number of first or%er eRuations 3/$%efine %ist #.1 /3 stepsiVe in t3/$%efine :AD E#.# /3 max for t 3/

    M,int N,

    voi% runge(%ouble x %ouble PLM %ouble step), /3 7ungeSutta function 3/

    %ouble f(%ouble x %ouble PLM int i), /3 function for %erivatives 3/

    output=fopen(-osc.%at- -w-), /3 external filename 3/

    PL#M=1.#, /3 initial position 3/PL1M=#.#, /3 initial velocitP 3/fprintf(output -#t0fn- PL#M),

    for (N=1, N3%ist&=:AD ,N++) /3 time loop 3/*t=N3%ist,runge(t P %ist),

    fprintf(output -0ft0fn- t PL#M),4

  • 8/12/2019 C Lab Programs 2

    76/80

    fclose(output),4

    voi% runge(%ouble x %ouble PLM %ouble step)*

    %ouble h=step/2.# /3 the mi%point 3/t1L>M t2L>M tEL>M /3 temporarP storage arraPs 3/1L>M 2L>M EL>ML>M, /3 for 7ungeSutta 3/int i,

    for (i=#,i&>,i++) t1LiM=PLiM+#.53(1LiM=step3f(x P i)),for (i=#,i&>,i++) t2LiM=PLiM+#.53(2LiM=step3f(x+h t1 i)),for (i=#,i&>,i++) tELiM=PLiM+ (ELiM=step3f(x+h t2 i)),for (i=#,i&>,i++) LiM= step3f(x+step tE i),

    for (i=#,i&>,i++) PLiM+=(1LiM+232LiM+23ELiM+LiM)/.#,

    4

    %ouble f(%ouble x %ouble PLM int i)*if (i==#) return(PL1M), /3 %erivative of first eRuation 3/if (i==1) return(#.23PL1MPL#M), /3 %erivative of secon% eRuation 3/4

    1. -* ; unge/utta Methodforsolving ?ierential 7@uations*-. -* d:-dt = /:(t), :()=$,

  • 8/12/2019 C Lab Programs 2

    77/80

    @@: >1; -* Cnitial condition *-2E. fprintf(o&tp&t, "0tfn", >);@K:25. for(n0;dit6n*f;n22) -* Dhe time loo0 *-2. *

    @O: *n6dit;2I. >Drk&tta(*, >, dit);@N:E#. fprintf (o&tp&t, "ftfn", *, >);E1. 4J@:EE. fc%oe(o&tp&t);E.4 -* 7nd o main unction*-JH:E.%oublerk&tta(%ouble*, %ouble>, %oubleh)-*Ealled on unction*-EF.*

    EI. %ouble>n, k1, k@, kJ, kK;EO. %oubleU h5@:0;K0:1. k1 (h6(*, >));2. k@ (h6(*2U, >2(k15@)));E. kJ (h6(*2U, >2(k@5@)));. kK (h6(*2h, >2kJ));5. return(>2(>2(k12@6k@2@6kJ2kK)615P));.4KO:KQ:

    O.%ouble(%ouble*, %ouble>)-*Ealled on derivative*-

    H0:51.*52. return(>);HJ:HK:HH:HP:HO:HQ:HN:P0:1.4

    /3 7unge Sutta for a set of first or%er %ifferential eRuations 3/

  • 8/12/2019 C Lab Programs 2

    78/80

    $%efine > 2 /3 number of first or%er eRuations 3/$%efine %ist #.1 /3 stepsiVe in t3/$%efine :AD E#.# /3 max for t 3/4AE 6o&tp&t; 56 interna% fi%ename 65

    voi% runge(%ouble x %ouble PLM %ouble step); 56 &n9eDZ&tta f&nction 65

    %ouble f(%ouble x %ouble PLM int i); 56 f&nction for derivative 65

    main(){do&b%e t, >[];int j;o&tp&tfopen("oc:dat", "!"); 56 e*terna% fi%ename 65

    >[0]1:0; 56 initia% poition 65>[1]0:0; 56 initia% ve%ocit> 65fprintf(o&tp&t, "0.tf.n", >[0]);for (j1; j6dit#C ;j22) 56 time %oop 65{ tj6dit; r&n9eK(t, >, dit);

    fprintf(o&tp&t, "f.tf.n", t, >[0]);

    3

    fclose(output);3

    voi% runge(%ouble x %ouble PLM %ouble step){do&b%e htep5@:0, 56 the midpoint 65 t1[], t@[], tJ[], 56 temporar> tora9e arra> 65 k1[], k@[], kJ[],kK[]; 56 for &n9eDZ&tta 65int i;for (i0;i;i22) t1[i]>[i]20:H6(k1[i]tep6f(*, >, i));for (i0;i;i22) t@[i]>[i]20:H6(k@[i]tep6f(*2h, t1, i));for (i0;i;i22) tJ[i]>[i]2 (kJ[i]tep6f(*2h, t@, i));for (i0;i;i22) kK[i] tep6f(*2tep, tJ, i);

    for (i0;i;i22) >[i]2(k1[i]2@6k@[i]2@6kJ[i]2kK[i])5P:0;3

  • 8/12/2019 C Lab Programs 2

    79/80

    %ouble f(%ouble x %ouble PLM int i){if (i0) ret&rn(>[1]); 56 derivative of firt eq&ation 65if (i1) ret&rn(D0:@6>[1]D>[0]); 56 derivative of econd eq&ation 65

    3

    ;he K co%e to implement Simpo!" Ru#e. ;he number of steps is taen tobe 2#.

  • 8/12/2019 C Lab Programs 2

    80/80

    printf("d .t.t%f .t.t%f.n", , h, /= ); 56 print the an!er 65

    56 the G.tG here i j&t to format the o&tp&t: 4t correpond to hittin9 the T#I ke> on the ke>board ie: kip a n&mber of pace before printin9

    the ne*t o&tp&t: 65

    3

    %ouble f(%ouble x){

    do&b%e f&nction; f&nction *6*6*6*; ret&rn(f&nction);

    3