Basic C Prgrams

download Basic C Prgrams

of 21

Transcript of Basic C Prgrams

  • 8/13/2019 Basic C Prgrams

    1/21

    Area of Circle

    /*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/

    #include-$)

    sc"n&+&-(&$)celcius0?.@/7.@$*&14$)

    'int&+Celcius0d-(celcius$)

    'int&+,nEnte' the celcius ="lue>-$)sc"n&+&-(c$)

    &"h'en0?.@/7.@$*cB14)

    'int&+F"h'enheit0d-(&"h'en$)

    etch$)

    5

    OUTPUT:

  • 8/13/2019 Basic C Prgrams

    2/21

    Ente' the &"h'enheit ="lue>7

    Celsius02.7

    Ente' the celcius ="lue>23

    F"h'enheit07@.@@

    Evaluate the iven E!pression

    LOGIN: "U#E"$

    /* E'ession E="lu"tion*/

    #include

  • 8/13/2019 Basic C Prgrams

    3/21

  • 8/13/2019 Basic C Prgrams

    4/21

  • 8/13/2019 Basic C Prgrams

    5/21

    %in& the Given num)er is palin&rome or Not

    /* :RORAM TO FIND THE UM AND RE;ERE OF THE I;EN NUMKER*/

    #include

  • 8/13/2019 Basic C Prgrams

    6/21

    5

    nu!e'BB)

    5

    5

    OUTPUT:

    :'intin "ll A'!st'on nu!e's eteen 2 "nd 7@@

    A!st'on Nu!e'>2

    A!st'on Nu!e'>271

    A!st'on Nu!e'>16@

    A!st'on Nu!e'>162

    ,ultiplication of t+o matrices

    // MULT:LICATION OF TPO MATRIQ

    #include%

    &o'0@)

    5

    'int&+,nThe ele!ents o& A !"t'i-$)&o'i0@)i

    %

    'int&+,n-$)

    &o'0@)

  • 8/13/2019 Basic C Prgrams

    7/21

    'int&+,n-$)

    &o'0@)'int&+,n-$)&o'0@)

    %

    'int&+,n-$)&o'0@)

  • 8/13/2019 Basic C Prgrams

    8/21

    ch"' o)

    cl'sc'$)

    'int&,n A'ithe!etic Oe'"tions usin sitch c"se$)

    'int&,nEnte' the ="lue o& " "nd $)

    sc"n&dd("($)'int&,nelect the oe'"tion u "nt$)

    'int&,nB.Add,n.u,n*.Mul$)'int&,n/.Di=,n.Mod$)

    'int&,nEnte' u' choice$)

    sc"n&d(o$)

    sitcho$

    %

    c"se VB>

    c0"B)

    'int&,nThe u! is d(c$)

    'e")

    c"se V>

    c0")

    'int&,nThe di&&e'ence is d(c$)'e")

    c"se V*>

    c0"*)

    'int&,nThe 'oduct is d(c$)'e")

    c"se V/>

    c0"/)

    'int&,nThe Wuotient is d(c$)

    'e")

    c"se V>

    c0")

    'int&,nThe Re!inde' is d(c$)'e")

    de&"ult>

    'int&,nEnte' the oe'"to' to e'&o'! A'ith!etic oe'"tions$)

    'e")

    5

    etch$)5

    OUTPUT:

    A'ithe!etic Oe'"tion

    Ente' the ="lue o& " "nd 3@ 7@

    elect the oe'"tion u "nt

    B.Add

    .u

    *.Mul

    /.Di=

    .Mod

    Ente' u' choice*

  • 8/13/2019 Basic C Prgrams

    9/21

    The Di&&e'ence is 4@@@

    Generatin Pascal trianle//CONTRUCT :ACAL TRIANLE

    #include

  • 8/13/2019 Basic C Prgrams

    10/21

    &lo"t netXs"l"')

    5)

    =oid !"in$

    %

    st'uct e!lo e!)

    &lo"t h'"(d"(det)

    cl'sc'$)

    'int&,nE!loee Det"ils$)

    'int&,nEnte' the e! n"!e$)

    sc"n&s(e!.n"!e$)

    'int&,nEnte' the e!loee no$)

    sc"n&d(e!.E!Xno$)'int&,nEnte' the "sic s"l"'$)

    sc"n&&(e!."sicXs"l$)

    h'"027*e!."sicXs"l$/2@@$)

    d"02@*e!."sicXs"l$/2@@$)

    det07*e!."sicXs"l$/2@@$)

    e!.netXs"l"'0e!."sicXs"lBh'"Bd"det)

    'int&,nE!loee n"!e>s(e!.n"!e$)

    'int&,nE!loee no>d(e!.E!Xno$)

    'int&,nE!loee K"sic s"l"'>&(e!."sicXs"l$)

    'int&,nHRA>&(h'"$)

    'int&,nDA>&(d"$)

    'int&,nDetection>&(det$)

    'int&,n'oss s"l"'>&(e!.netXs"l"'$)

    etch$)

    5

    OUTPUT:

    E!loee Det"ils

    Ente' the e!loee n"!e """

    Ente' the e!loee no2@@@

    Ente' the "sic s"l"' 7@@@

    E!loee n"!e>"""

  • 8/13/2019 Basic C Prgrams

    11/21

    E!loee no>2@@@

    E!loee K"sic s"l"'>7@@@.@@@@@@

    HRA>67@.@@@@@

    DA>7@@.@@@@@@

    Detection>47@.@@@@@@

    'oss s"l"'>97@@.@@@@@@

    %in& "tu&ent ,ar-s usin Union

    //TUDENT RECORD UIN :OINTER AND UNION

    #include,n-(iB2$)

    'int&+Ente' the n"!e-$)

    sc"n&+s-(studiS.n"!e$)

    'int&+,nEnte' the 'oll no o& the student-$)

    sc"n&+s-(studiS.'eno$)

    'int&+,nEnte' the "=e'"e ="lue o& the student-$)

    sc"n&+d-(studiS."=$)

    5

    t0stud)&o't0stud)t

  • 8/13/2019 Basic C Prgrams

    12/21

    t'"de0A)

    5

    'int&+,n-$)

    'int&+NAME REITERNO A;ERAE RADE,n-$)

    &o't0stud)t

    Ente' the n"!e MUNI

    Ente' the 'oll no o& the student 2@@

    Ente' the "=e'"e ="lue o& the student ?7student4S in&o'!"tion>

    Ente' the n"!e LAY

    Ente' the 'oll no o& the student 4@@

    Ente' the "=e'"e ="lue o& the student 77student1S in&o'!"tion>

    Ente' the n"!e RAZA

    Ente' the 'oll no o& the student 1@@

    Ente' the "=e'"e ="lue o& the student 47

    NAME REITERNO A;ERAE RADE

    MUNI 2@@ ?7 A

    LYA 4@@ 77 K

    RAJA 300 25 D"+appin T+o Num)ers .+ithout thir& /aria)le0 1 CALL B( /ALUE

    #include

  • 8/13/2019 Basic C Prgrams

    13/21

    0)

    'int&,n A&te' s"in the ="lues o& '2 0 d "nd '4 0 d(($)

    5

    OUTPUT:

    Ente' the to nu!e's

    27 1@Ke&o'e s"in the ="lues o& '2 027 "nd '4 01@

    A&te' s"in the ="lues o& '2 01@ "nd '4 0 27

    "+appin T+o Num)ers 1 Call B' #eference

    #include

  • 8/13/2019 Basic C Prgrams

    14/21

    %

    int nu!(")

    'int&+Ente' the nu!e'-$)

    sc"n&+d-(nu!$)

    "0'ecu'nu!$)'int&+The &"cto'i"l o& the nu!e' d is d-(nu!("$)

    5'ecu'int no$

    %

    int &"ct02)

    i&no002$

    'etu'n2$)

    else

    &"ct0no*'ecu'no2$)

    5

    OUTPUT:

    Ente' the nu!e' 7The &"cto'i"l o& the nu!e' 7 is 24@

    ifference between Post Increment (n++) and Pre Increment (++n)

    ost Increment (n++): It increases the value of variable by 1 after execution of the statement.

    re Increment (++n): It increases the value of variable by 1 before execution of the statement.

    Program Output

    class Demo

    public static oi! mai"#$t%i"&

    a%&s'()

    {

    i"t "=10*

    $+stem.out.p%i"tl"#")*

    $+stem.out.p%i"tl"#",

    ,)*

    $+stem.out.p%i"tl"#")*

    -

    10

    10

    11

    Program Output

    class Demo

  • 8/13/2019 Basic C Prgrams

    15/21

    public static oi! mai"#$t%i"&

    a%&s'()

    {

    i"t "=10*

    $+stem.out.p%i"tl"#")*

    $+stem.out.p%i"tl"#,,")*

    $+stem.out.p%i"tl"#")*

    -

    10

    11

    11

    rime Number

    ogic:Prime Number are divisible by itself only.

    Not divisible by any

    Number

    Divisible by !!!no need to

    c"ec# furt"er

    Divisible by $ !!!no need to

    c"ec# furt"er&'&$'&'$&*'&'

    ,&'-,&$',&',&*',&',&%'

    .&'

    .&$'-

    .&

    .&*

    .&

    .&%

    .&,

    Numbers are not divisible by more than half of the number

    No need to check upto 6heck upto 3 only

    No need to check upto 7check upto only

    No need to check upto !check upto only

    Program Output

    /ote $ca""e% class o% it JD1.5 o% aboempo%t aa.util.*

    class %ime

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t " i %es*

    boolea" la&=t%ue*

    $ca""e% sca"= "e $ca""e%#$+stem.i")*

    $+stem.out.p%i"tl"#:lease ;"te% a /o.:)*

    "=sca"."e

  • 8/13/2019 Basic C Prgrams

    16/21

    $+stem.out.p%i"tl"#" , : is "ot %ime

    /umbe%:)*

    -

    ibonacci 0eries ( $ * , $!!!)

    ogic:"um of previous t#o numbers #ill $ive us next number.

    prev ne1t sum

    shifted to prev shifted to ne1t

    1 1 %

    1 % 3

    % 3 &

    3 & !

    & ! 13

    ! 13 ...

    13 ... ...

    rev will give you fibonacci series

    Program Output

    class @ibo"acci

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t p%e "e

  • 8/13/2019 Basic C Prgrams

    17/21

    1& 6 %1

    %1 7 %!

    %! ! 36

    36 ) &

    & 1( &&

    Program Output

    class $um10

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t " sum=0*

    o%#"=1*">=10*",,)

    {

    sum,="* o% sum=sum,"*

    -

    $+stem.out.p%i"tl"#sum)*

    -

    55

    um of 02uare of st - Natural Numbers

    ogic:"um of previous t#o numbers #ill $ive us next number.

    sum n3n sum

    sum'n3n

    ( 1*1 1

    1 %*% &

    & 3*3 1

    1 * 3(

    3( &*& &&

    && 6*6 )1

    )1 7*7 1(

    1( !*! %(

    %( )*) %!&

    %!& 1(*1( 3!&

    Program Output

    class $um$10

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t " sum=0*

    o%#"=1*">=10*",,)

    {

    sum,="*" o% sum=sum,"*"

    -

    $+stem.out.p%i"tl"#sum)*

    -

    385

  • 8/13/2019 Basic C Prgrams

    18/21

    actorial

    ogic:+actorial of & , & x x 3 x % x 1

    prod n prod

    prod*n

    1 & &

    & %(

    %( 3 6(

    6( % 1%(

    1%( 1 1%(

    Program Output

    /ote $ca""e% class o% it JD1.5 o% aboempo%t aa.util.*

    class @acto%ial

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t " i p%o!=1*

    $ca""e% sca"= "e $ca""e%#$+stem.i")*

    $+stem.out.p%i"tl"#:lease ;"te% a /o.:)*

    "=sca"."e

  • 8/13/2019 Basic C Prgrams

    19/21

    "3=sca"."e

  • 8/13/2019 Basic C Prgrams

    20/21

    & %3 &

    %3 %3 &

    %3 & &

    Program Output

    /ote $ca""e% class o% it JD1.5 o% aboempo%t aa.util.*

    class $ap

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t "1 "2 temp*

    $ca""e% sca"= "e $ca""e%#$+stem.i")*

    $+stem.out.p%i"tl"#:lease ;"te% /o 1 :)*

    "1=sca"."e

  • 8/13/2019 Basic C Prgrams

    21/21

    $+stem.out.p%i"tl"#:@i%st /o : , "1)*

    $+stem.out.p%i"tl"#:$eco"! /o : , "2)*

    -

    um of Digits

    ogic:&13 - &'1'3,)

    N res n sum

    &13 (

    &13/1( 3 3

    &1301( &1 3

    &1/1( 1

    &101( &

    &/1( & )

    &01( ( )

    Program Output

    /ote $ca""e% class o% it JD1.5 o% aboe

    mpo%t aa.util.*

    class $umDi&its

    public static oi! mai"#$t%i"& a%&s'()

    {

    i"t " %es*

    $ca""e% sca"= "e $ca""e%#$+stem.i")*

    $+stem.out.p%i"tl"#:lease ;"te% /o. :)*

    "1=sca"."e