Top Banner

of 35

Solved Programs Computer Class 12

Feb 28, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/25/2019 Solved Programs Computer Class 12

    1/35

    Solved programs

    Question:

    Write a Program in Java to input 2 numbers and find their Greatest Common Divisor (GCD).

    Note:If the 2 numbers are 54 and 24, then the divisors (factors) of 54are: 1, 2, 3, 6, 9, 18, 27,

    54

    Similarly t!e divisors (factors) of 24are: 1, 2, 3, 4, 6, 8, 12, 24

    he numbers that these t!o lists share in "ommon are the common divisors (factors)of 54 and

    24: 1, 2, 3, 6

    he greatest #highest$ of these is 6. hat is the "reatest common divisoror the highest "ommon

    fa"tor of 54 and 24.

    importjava.io.*;classHcf { publicstaticvoidmain(String args[])throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r th$ irst no + ); intn,'nt$g$r.pars$nt(br.r$ad-in$()); Sst$m.out.print("nt$r th$ S$cond no + ); intn'nt$g$r.pars$nt(br.r$ad-in$()); inthcf'/; intmin ' 0ath.min(n,1n);

    for(inti'min; i 2' ,; i33) { if(n,4i '' /55 n4i '' /) { hcf ' i; br$a6; 7 7

    Sst$m.out.print(8n9h$ hcf of :n,: and :n: ' :hcf); 7 7

    #$t%$t:

    %nter the &irst no : 54%nter the Se"ond no : 24

    he h"f of 54 and 24 ' (

  • 7/25/2019 Solved Programs Computer Class 12

    2/35

    &$estion:

    Write a program to input a !ord from the user and remove the dupli"ate "hara"ters present in it.

    'am%e:

    I)P* + ab"ab"ab"*P* + ab"

    I)P* + -avafors"hool

    *P* + -avfors"hl

    I)P* + ississippi

    *P* + isp

    *ro"rammin" Code:

    /

    01

    11

    12

    1

    3

    14

    15

    1

    (1

    1

    /1

    02

    2

    12

    2

    2

    importjava.io.*;class&$mov$up

  • 7/25/2019 Solved Programs Computer Class 12

    3/35

    3

    2

    42

    5

    2(

    2

    2

    /

    2

    0

    #$t%$t:

    'am%e 1:%nter any !ord : ississippi

    Word after removing dupli"ate "hara"ters : isp

    'am%e 2:

    %nter any !ord : ttitude

    Word after removing dupli"ate "hara"ters : tiude

    &$estion:

    Write a Program in Java to input a number and "he"6 !hether it is a Disari$m N$m+eror not.

    Note: number !ill be "alled 7IS8I* if sum of its digits po!ered !ith their respe"tiveposition is e9ual to the original number.

    or eam%e135 is a 7IS8I*

    #Wor6ings 113253' 135, some other D-./0-are /0, 15, 51/ et"$

    *ro"rammin" Code:

    4

    5(

    /0

    1

    importjava.io.*;classisarium {

    publicstaticvoidmain(String[] args)throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r (n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r a numb$r + ); intn ' nt$g$r.pars$nt(br.r$ad-in$()); intcop ' n1 d ' /1 sum ' /; String s ' nt$g$r.toString(n); AAconv$rting th$ numb$r into aString intl$n ' s.l$ngth(); AAfinding th$ l$ngth of th$ numb$r i.$.

  • 7/25/2019 Solved Programs Computer Class 12

    4/35

    11

    12

    1

    31

    4

    15

    1

    (

    1

    1

    /

    10

    2

    2

    12

    2

    2

    32

    4

    25

    2

    (2

    2

    /

    no.of digits

    whil$(cop2/)

    {

    d ' cop 4 ,/; AA$#tracting th$ last digit sum ' sum : (int)0ath.pow(d1l$n); l$n33; cop ' cop A ,/; 7

    if(sum '' n)

    Sst$m.out.println(n: is a isarium Cumb$r.); $ls$ Sst$m.out.println(n: is not a isarium Cumb$r.); 7 7

    #$t%$t:

    %nter a )umber : 135

    135 is a 7isarium )umber.

  • 7/25/2019 Solved Programs Computer Class 12

    5/35

    %nter a )umber : 210

    210 is not a 7isarium )umber.

    %nter a )umber : /0/0 is a 7isarium )umber.

    &$estion:

    7efine a "lass named moviea"ic!ith the follo!ing des"ription:

    -nstance varia+esdata mem+ers:

    int year + to store the year of release of a movie

    String title + to store the title of the movie.

    float rating + to store the popularity rating of the movie. #minimum rating ' . andma;imum rating ' 5.$

    em+er et!ods:

    #i$ movieagi"#$ 7efault "onstru"tor to initiali

  • 7/25/2019 Solved Programs Computer Class 12

    6/35

    4

    5

    (

    /

    01

    11

    1213

    14

    15

    1(1

    1/

    10

    221

    2223

    24

    252(

    2

    2/

    203

    31

    3233

    34

    353(

    3

    3/

    304

    String titl$;floatrating;

    movi$0agic() AA d$fault constructor{

    $ar ' /;

    rating ' /./f; AA notic$ th$ @f@titl$ ' ;

    7

    voidacc$pt() throws!"#c$ption{

    %uff$r$d&$ad$r br ' n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));Sst$m.out.print("nt$r th$ titl$ of th$ movi$ + );

    titl$ ' br.r$ad-in$();Sst$m.out.print("nt$r th$ $ar of its r$l$as$ + );

    $ar ' nt$g$r.pars$nt(br.r$ad-in$());Sst$m.out.print("nt$r th$ movi$ rating + );

    rating ' loat.pars$loat(br.r$ad-in$());

    7

    voiddispla(){

    Sst$m.out.println(9h$ titl$ of th$ movi$ is + :titl$);if( rating 2' /./55 rating =' ./) {

    Sst$m.out.println(9h$ movi$ was a lop);7 $ls$if( rating 2' .,55 rating =' D.E) {

    Sst$m.out.println(9h$ movi$ was a S$mi3hit);7 $ls$if( rating 2' D.F55 rating =' E.F) {Sst$m.out.println(9h$ movi$ was a Hit);

    7 $ls$if( rating 2' E.G55 rating =' F./) {Sst$m.out.println(9h$ movi$ was a Sup$r Hit);

    7 $ls$ {Sst$m.out.println("nt$r a valid movi$ rating in b$tw$$n /./ and F./);77

    publicstaticvoidmain(String args[]) throws!"#c$ption{

    movi$0agic ob ' n$wmovi$0agic(); AA cr$ating obj$ct of th$ class movi$0agicob.acc$pt();ob.displa();

    77

    #$t%$t:

    %nter the title of the movie : Spiderman%nter the year of its release : 213

    %nter the movie rating : 4.2

  • 7/25/2019 Solved Programs Computer Class 12

    7/35

    he title of the movie is : Spiderman

    he movie !as a @it

    &$estion:

    spe"ial t!o?digit number is su"h that !hen the sum of the digits is added to the produ"t of itsdigits, the result is e9ual to the original t!o?digit number.

    'am%e:

    Aonsider the number 50.Sum of digits ' 50'14

    Produ"t of its digits ' 5 ; 0 ' 45

    Sum of the digits and produ"t of digits ' 14 45 ' 50

    Write a program to a""ept a t!o?digit number. dd the sum of its digits to the produ"t of its

    digits. If the value is e9ual to the number input, output the message =spe"ial?t!o digit number>

    other!ise, output the message =)ot a spe"ial t!o?digit number>.

    *ro"rammin" Code:

    12

    3

    45

    (

    /

    01

    11

    1

    21

    3

    14

    1

    51

    (

    1

    1

    /

    1

    importjava.io.*;classSp$cialIF

  • 7/25/2019 Solved Programs Computer Class 12

    8/35

    0

    2

    2

    1

    22

    2

    32

    4

    2

    52

    (

    2

    2

    /2

    0

    3

    7 7

    #$t%$t:

    1 %nter a 2 digit number : 0utput : he number 0 is a Spe"ial !o?7igit )umber.

    2 %nter a 2 digit number : 4

    utput : he number is )ot a Spe"ial !o?7igit )umber.

    &$estion:

    Write a program to input a !ord from the user and remove the "onse"utive repeated "hara"ters

    by repla"ing the se9uen"e of repeated "hara"ters by its single o""urren"e.

    'am%e:

    I)P* + Jaaavvvvvvvvaaaaaaaaaaa*P* + Java

    I)P* + @eeeiiiissggoiinggg

    *P* + @eisgoing

  • 7/25/2019 Solved Programs Computer Class 12

    9/35

    *ro"rammin" Code:

    1

    23

    45

    (

    /

    01

    111

    2

    13

    1

    4

    15

    1

    (1

    1

    /1

    0

    2

    2

    12

    2

    23

    2

    42

    5

    2

    (

    importjava.io.*;class&$mov$&$pdding a spac$ at th$ $nd of th$ word intl's.l$ngth(); AA inding th$ l$ngth of th$ word String ans'; AA Lariabl$ to stor$ th$ final r$sult charch,1ch;

    for(inti'/; i=l3,; i::)

    { ch,'s.char>t(i); AA "#tracting th$ first charact$r ch's.char>t(i:,); AA "#tracting th$ n$#t charact$r

    AA >dding th$ first $#tract$d charact$r to th$ r$sult if th$ curr$nt and th$

    n$#t charact$rs ar$ diff$r$nt

    if(ch,?'ch)

    { ans ' ans : ch,; 7 7 Sst$m.out.println(Bord aft$r r$moving r$p$at$d charact$rs ':ans); AA Mrinting th$ r$sult 77

  • 7/25/2019 Solved Programs Computer Class 12

    10/35

    #$t%$t:

    'am%e 1:

    %nter any !ord: JaaavvvvvvvvaaaaaaaaaaaWord after removing repeated "hara"ters ' Java

    'am%e 2:

    %nter any !ord: iiiiiisssssfffffffffffffuunnnnn

    Word after removing repeated "hara"ters ' isfun

    &$estion:

    Write a program to input a number. Aount and print the fre9uen"y of ea"h digit present in thatnumber. he output should be given as:

    .am%e -n%$t:44514(21

    .am%e #$t%$t:

    '''''''''''''''''''''7igit &re9uen"y

    '''''''''''''''''''''

    1 22 1

    4 3

    5 1( 1

    *ro"rammin" Code:

    123

    4

    5

    (

    /

    01

    111

    2

    1

    31

    4

    1

    importjava.io.*;classigitr$N{ publicstaticvoidmain(String args[])throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r an numb$r + ); intn ' nt$g$r.pars$nt(br.r$ad-in$());

    intfr$N[]'n$wint[,/]; AAarra for storing fr$Nu$nc of all digits

    for(inti'/; i=,/; i::)

    { fr$N[i]'/; AAintialiOing th$ count of $v$r digit with @/@ 7

    A*Cot$+ r$Nu$nc of digit @/@ is stor$d in fr$N[/]1 fr$Nu$nc of

    @,@ in fr$N[,] and so on*A

    Sst$m.out.println(!utput+);

    Sst$m.out.println(''''''''''''''''''''); AAthis is just for

  • 7/25/2019 Solved Programs Computer Class 12

    11/35

    5

    1

    (1

    1/

    1

    02

    2

    12

    2

    2

    32

    42

    5

    2(

    2

    2/

    2

    03

    31

    3

    2

    33

    3

    43

    5

    3(

    stling th$ loo6 of th$ output Sst$m.out.println(igit8tr$Nu$nc); Sst$m.out.println('''''''''''''''''''');

    intd; whil$(n2/) { d'n4,/; AA$#tracting digit from th$ $nd fr$N[d]::; AAincr$asing th$ fr$Nu$nc of that digit. n'nA,/; 7

    for(inti'/; i=,/; i::)

    { if(fr$N[i]?'/) AAprinting onl thos$ digits whos$ count isnot @/@ Sst$m.out.println( :i:8t :fr$N[i]); 7 7 7

    #$t%$t:

  • 7/25/2019 Solved Programs Computer Class 12

    12/35

    %nter any number : 03/25

    utput:

    '''''''''''''''''''''7igit &re9uen"y

    ''''''''''''''''''''' 1

    2 13 1

    5 1

    3/ 1

    0 1

    &$estion:

    Write a program to input a string #!ord$. Aonvert it into lo!er"ase letters. Aount and print thefre9uen"y of ea"h alphabet present in the string. he output should be given as:

    .am%e -n%$t:lphabets

    .am%e #$t%$t:

    ''''''''''''''''''''''''''lphabet &re9uen"y

    ''''''''''''''''''''''''''

    a 2

    b 1e 1

    h 1

    l 1p 1

    s 1

    t 1

    *ro"rammin" Code:

    1

    2

    3

    4

    5(

    /

    0

    1

    11

    importjava.io.*;class>lphab$tr$N{ publicstaticvoidmain(String args[])throws!"#c$ption {

    %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r an string+ ); String s ' br.r$ad-in$();

    s's.to-ow$r

  • 7/25/2019 Solved Programs Computer Class 12

    13/35

    1

    2

    13

    1

    41

    5

    1(

    1

    1/

    1

    0

    2

    21

    2

    22

    3

    2

    42

    5

    2(

    2

    2

    /

    2

    03

    31

    3

    23

    3

    3

    4

    Sst$m.out.println(''''''''''''''''''''''''''); AAthis is just forstling th$ loo6 of th$ output Sst$m.out.println(>lphab$t8tr$Nu$nc); Sst$m.out.println('''''''''''''''''''''''''');

    A* t(j); AA$#tracting charact$rs of th$ stringon$ b on$ if(ch''i) AAfirst ch$c6ing th$ whol$ string for @a@1th$n @b@ and so on count::; AAincr$asing count of thos$ aplhab$ts whichar$ pr$s$nt in th$ string 7 if(count?'/)AAprinting onl thos$ alphab$ts whos$ count is

    not @/@ { Sst$m.out.println(i:8t8t:count); 7 7 77

  • 7/25/2019 Solved Programs Computer Class 12

    14/35

    3

    5

    #$t%$t:

    %nter any string: ilove-avafors"hool

    utput:

    ''''''''''''''''''''''''''

    lphabet &re9uen"y''''''''''''''''''''''''''

    a 2

    " 1e 1

    f 1

    h 1i 1

    - 1

    l 2

    o 4r 1

    s 1

    v 2

    &$estion:

    .mit! n$m+eris a "omposite number, the sum of !hose digits is the sum of the digits of itsprime fa"tors obtained as a result of prime fa"tori

  • 7/25/2019 Solved Programs Computer Class 12

    15/35

    .am%e data:

    Input 04 utput SI@ )umber

    Input 12 utput ) SI@ )umber

    Input ((( utput SI@ )umber

    Input 000 utput ) SI@ )umber

    *ro"rammin" Code:

    1

    2

    34

    5(

    /

    0

    1

    11

    12

    1

    314

    1

    51

    (

    1

    1

    /

    10

    2

    2

    1

    22

    importjava.io.*;classSmith{ static%uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));

    AAfunction for finding sum of digits intsumig(intn) { ints'/; whil$(n2/) { s's:n4,/; n'nA,/; 7 r$turns; 7

    AAfunction for g$n$rating prim$ factors and finding th$ir sumintsumMrim$act(intn)

    { inti'1 sum'/; whil$(n2,) { if(n4i''/) { sum'sum:sumig(i); AAH$r$ @i@ is th$ prim$ factor of @n@ andw$ ar$ finding its sum n'nAi; 7 $ls$ i::; 7

    r$turnsum; 7

    publicstaticvoidmain(String args[]) throws!"#c$ption{ Smith ob'n$wSmith(); Sst$m.out.print("nt$r a Cumb$r + ); intn'nt$g$r.pars$nt(br.r$ad-in$()); inta'ob.sumig(n);AA finding sum of digit

  • 7/25/2019 Solved Programs Computer Class 12

    16/35

    2

    3

    24

    2

    52

    (

    2

    2

    /

    20

    3

    31

    32

    3

    33

    4

    3

    53

    (

    3

    3

    /3

    0

    4

    4

    1

    42

    4

    3

    intb'ob.sumMrim$act(n); AAfinding sum of prim$ factors

    Sst$m.out.println(Sum of igit ' :a); Sst$m.out.println(Sum of Mrim$ actor ' :b);

    if(a''b)

    Sst$m.out.print(t is a Smith Cumb$r); $ls$ Sst$m.out.print(t is Cot a Smith Cumb$r);77

    #$t%$t:

  • 7/25/2019 Solved Programs Computer Class 12

    17/35

    1%nter a )umber : 04

    Sum of 7igit ' 13

    Sum of Prime &a"tor ' 13It is a Smith )umber

    2%nter a )umber : 12Sum of 7igit ' 3

    Sum of Prime &a"tor ' 13It is )ot a Smith )umber

    3%nter a )umber : 4035

    Sum of 7igit ' 42Sum of Prime &a"tor ' 42

    It is a Smith )umber

    Question:

    Write a Program in Java to input a number in 7e"imal number system and "onvert it into its

    e9uivalent number in the @e;ade"imal number system.

    .o$tion:

    /

    0

    1

    11

    12

    1

    31

    4

    15

    1(

    1

    1

    /1

    0

    importjava.io.*;class$cH$#{ publicstaticvoidmain(String args[])throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r (n$w

    nputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r a d$cimal numb$r + ); intn'nt$g$r.pars$nt(br.r$ad-in$());

    intr;

    String s'; AAvariabl$ for storing th$ r$sult

    AAarra storing th$ digits (as charact$rs) in a h$#ad$cimal numb$r

    sst$m chardig[]'{@/@1@,@1@@1@D@1@E@1@F@1@G@1@P@1@Q@1@K@1@>@1@%@1@

  • 7/25/2019 Solved Programs Computer Class 12

    18/35

    2

    21

    2

    22

    3

    24

    2

    5

    2(

    2

    2/

    20

    #$t%$t:

    %nter a de"imal number : 4

    utput ' 2&

    %nter a de"imal number : 1243utput ' 47C

    Question:

    Write a Program in Java to input a number in 7e"imal number system and "onvert it into its

    e9uivalent number in the "tal number system.

    .o$tion:

    1

    23

    4

    5(

    /0

    importjava.io.*;

    class$c!ct{ publicstaticvoidmain(String args[])throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r (n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r a d$cimal numb$r + ); intn'nt$g$r.pars$nt(br.r$ad-in$());

    intr;

  • 7/25/2019 Solved Programs Computer Class 12

    19/35

    1

    111

    2

    13

    1

    41

    5

    1

    (1

    1

    /1

    02

    21

    2

    2

    23

    String s'; AAvariabl$ for storing th$ r$sult

    AAarra storing th$ digits (as charact$rs) in th$ octal numb$r

    sst$m chardig[]'{@/@1@,@1@@1@D@1@E@1@F@1@G@1@P@7;

    whil$(n2/)

    { r'n4Q; AAfinding r$maind$r b dividing th$ numb$r b Q s'dig[r]:s; AAadding th$ r$maind$r to th$ r$sult andr$v$rsing at th$ sam$ tim$

    n'nAQ; 7 Sst$m.out.println(!utput ' :s); 77

    #$t%$t:

    %nter a de"imal number : 25utput ' 31

    %nter a de"imal number : 4(5

    utput ' 21

    Question:

    Write a Program in Java to input a number in 7e"imal number system and "onvert it into its

    e9uivalent number in the Cinary number system.

    .o$tion:

    12

    importjava.io.*;class$c%in{

  • 7/25/2019 Solved Programs Computer Class 12

    20/35

    3

    4

    5(

    /0

    1

    11

    1

    2

    13

    1

    4

    15

    1(

    1

    1

    /

    1

    02

    21

    2

    2

    publicstaticvoidmain(String args[])throws!"#c$ption { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r (n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r a d$cimal numb$r + ); intn'nt$g$r.pars$nt(br.r$ad-in$());

    intr;

    String s'; AAvariabl$ for storing th$ r$sult

    chardig[]'{@/@1@,@7; AAarra storing th$ digits (as charact$rs) in

    a binar numb$r sst$mwhil$(n2/)

    { r'n4; AAfinding r$maind$r b dividing th$ numb$r b s'dig[r]:s; AAadding th$ r$maind$r to th$ r$sult andr$v$rsing at th$ sam$ tim$ n'nA; 7 Sst$m.out.println(!utput ' :s); 77

    #$t%$t:

    %nter a de"imal number : 25

    utput ' 111

    %nter a de"imal number : 4utput ' 11111

    %nter a de"imal number : (

    utput ' 11

    &$estion:

  • 7/25/2019 Solved Programs Computer Class 12

    21/35

    Write a Program in Java to input a number and "he"6 !hether it is a D$c N$m+eror not.

    Note: 7u"6 number is a number !hi"h has t(i); AA$#tracting $ach digit and ch$c6ing wh$th$r itis a @/@ or not if(ch''@/@) c::; 7

    charf'n.char>t(/); AAta6ing out th$ first digit of th$ inputt$dnumb$r

    if(c2/55 f?'@/@)

    Sst$m.out.println(t is a duc6 numb$r); $ls$ Sst$m.out.println(t is not a duc6 numb$r); 77

  • 7/25/2019 Solved Programs Computer Class 12

    22/35

    2

    5

    2(

    2

    #$t%$t:

    %nter any number : 413

    It is a du"6 number

    %nter any number : 134It is not a du"6 number

    %nter any number : (30/It is a du"6 number

    his is the Java programming "ode !ritten in ClueJ !hi"h s!aps the values of t!o Strings

    !ithout using any third #temp$ variable.

    *ro"rammin" Code:

    1

    2

    3

    45

    (

    /

    0

    1

    11

    1

    2

    13

    14

    1

    51

    (

    importjava.io.*;classSwapStrings{ publicstaticvoidmain(String args[])throws!"#c$ption

    { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r (n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r th$ ,st String + ); String s,'br.r$ad-in$(); intl$n,'s,.l$ngth();

    Sst$m.out.print("nt$r th$ nd String + );

    String s'br.r$ad-in$();

    Sst$m.out.println(3333333333333333333333333333333);

    Sst$m.out.println(Strings %$for$ Swapping + ); Sst$m.out.println(,st String ' :s,);

    Sst$m.out.println(nd String ' :s);

    A*Swapping Mroc$ss %$gins*A s,'s,:s; s's,.substring(/1l$n,); s,'s,.substring(l$n,); A*Swapping Mroc$ss "nds*A

    Sst$m.out.println(3333333333333333333333333333333);

  • 7/25/2019 Solved Programs Computer Class 12

    23/35

    1

    1/

    1

    02

    21

    2

    2

    23

    2

    4

    25

    2(

    Sst$m.out.println(Strings >ft$r Swapping + ); Sst$m.out.println(,st String ' :s,); Sst$m.out.println(nd String ' :s); 77

    #$t%$t:

    %nter the 1st String : Java &or

    %nter the 2nd String : S"hool is &unDDDDDDDDDD?

    Strings Cefore S!apping :1st String ' Java &or2nd String ' S"hool is &un

    DDDDDDDDDD?

    Strings fter S!apping :1st String ' S"hool is &un

    2nd String ' Java &or

    &$estion:

    Write a program to find the shortest and the longest !ord in a senten"e and print them along !ith

    their length.

    .am%e -n%$t:I am learning Java

    .am%e #$t%$t:

    Shortest !ord ' I

    Eength ' 1Eongest !ord ' learning

    Eength ' /

  • 7/25/2019 Solved Programs Computer Class 12

    24/35

    .o$tion:

    1

    23

    45

    (

    /

    01

    111

    2

    13

    1

    4

    15

    1

    (1

    1

    /1

    0

    2

    2

    12

    2

    23

    2

    42

    5

    2

    (2

    importjava.io.*;classShortlongword{publicstaticvoidmain(String args[])throws!"#c$ption

    { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); Sst$m.out.print("nt$r an s$nt$nc$ + ); AAinputting th$ s$nt$nc$ String s'br.r$ad-in$();

    s's: ; AAadding a spac$ at th$ $nd1 to $#tract th$ last word also

    intl$n's.l$ngth(); AAfinding th$ l$ngth of th$ s$nt$nc$ String #'1ma#w'1minw'; charch; intp1ma#l'/1minl'l$n;

    for(inti'/;i=l$n;i::)

    { ch's.char>t(i); AA$#tracting charact$rs of th$ string on$ at atim$ if(ch?'@ @) { #'#:ch; AAadding charact$rs to form word if charact$r is notspac$ 7 $ls$ { p'#.l$ngth();

    if(p=minl) AAch$c6ing for minimum l$ngth

    { minl'p; minw'#; 7

    if(p2ma#l) AAch$c6ing for ma#imum l$ngth

    { ma#l'p; ma#w'#; 7 #'; AA$mpting th$ t$mporar variabl$ to stor$ n$#t word 7 7 Sst$m.out.println(Short$st word ' :minw:n-$ngth ' :minl);

    Sst$m.out.println(-ong$st word ' :ma#w:n-$ngth ' :ma#l); 77

  • 7/25/2019 Solved Programs Computer Class 12

    25/35

    2

    /

    20

    3

    3

    1

    32

    3

    3

    34

    3

    5

    3(

    3

    3

    /3

    0

    #$t%$t:

    %nter any senten"e : I am learning JavaShortest !ord ' I

    Eength ' 1

    Eongest !ord ' learningEength ' /

    &$estion:

    ban6 intends to design a program to display the denomination of an input amount, up to 5

    digits. he available denomination !ith the ban6 are of rupees 1 , 5 , 1 , 5 , 2 , 1 , 5 ,

    2 , and 1.

    7esign a program to a""ept the amount from the user and display the brea6?up in des"ending

    order of denomination. #i.e. preferen"e should be given to the highest denomination available$

    along !ith the total number of notes. F)ote: nly the denomination used, should be displayedG.

    'am%e:

  • 7/25/2019 Solved Programs Computer Class 12

    26/35

    -N*: 14//

    #*:

    7%)I)I)S:

    1 ; 14 ' 145 ; 1 ' 5

    1 ; 2 ' 2

    5 ; 1 ' 5

    2 ; 1 ' 21 ; 1 ' 1

    5 ; 1 ' 5

    2 ; 1 ' 21 ; 1 ' 1

    DDDDDDDDDDDD+

    E ' 14//DDDDDDDDDDDD+

    otal )umber of )otes ' 23

    .o$tion:

    1

    23

    4

    5

    (

    /0

    1

    111

    2

    13

    1

    41

    5

    1(

    1

    importjava.io.*;class$nominations{publicstaticvoidmain(String args[])throws!"#c$ption{%uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));intd$n[]'{,///1F//1,//1F/1/1,/1F11,7; AAstoring all th$ d$nominations inan arraSst$m.out.print("nt$r an >mount+ ); AA"nt$ring an amountintamount'nt$g$r.pars$nt(br.r$ad-in$());

    intcop'amount; AA0a6ing a cop of th$ amountinttotalCot$s'/1count'/;

    Sst$m.out.println(8n"C!0C>9!CS+ 8n);

    for(inti'/;i=K;i::) AASinc$ th$r$ ar$ K diff$r$nt tp$s of not$s1 h$nc$ w$ch$c6 for $ach not$.{count'amountAd$n[i]; AA counting numb$r of d$n[i] not$sif(count?'/) AAprinting that d$nomination if th$ count is not O$ro{Sst$m.out.println(d$n[i]:8t#8t:count:8t' :d$n[i]*count);7totalCot$s'totalCot$s:count; AAfinding th$ total numb$r of not$samount'amount4d$n[i]; AAfinding th$ r$maining amount whos$ d$nomination isto b$ found7

  • 7/25/2019 Solved Programs Computer Class 12

    27/35

    1

    /

    10

    2

    2

    1

    22

    2

    3

    24

    2

    5

    2(

    2

    2

    /2

    0

    3

    Sst$m.out.println(33333333333333333333333333333333);Sst$m.out.println(9!9>-8t8t8t' :cop); AAprinting th$ total amountSst$m.out.println(33333333333333333333333333333333);Sst$m.out.println(9otal Cumb$r of Cot$s8t' :totalCot$s); AAprinting th$total numb$r of not$s77

    #$t%$t:

    %nter any mount: 14/5(

    7%)I)I)S:

    1 ; 14 ' 145 ; 1 ' 5

    1 ; 3 ' 3

    5 ; 1 ' 5

    5 ; 1 ' 51 ; 1 ' 1

    DDDDDDDDDDDD+

    E ' 14/5(DDDDDDDDDDDD+

    otal )umber of )otes ' 21

    &$estion:

  • 7/25/2019 Solved Programs Computer Class 12

    28/35

    Write a program in JH to find the Prime fa"tors of a number.

    Prime fa"tors of a number are those fa"tors !hi"h are prime in nature and by !hi"h the number

    itself is "ompletely divisible #1 !ill not be ta6en as prime number$.

    &e! su"h numbers are:Prime &a"tors of 24 are 2, 2, 2, 3

    Prime &a"tors of ( are 2, 3

    .o$tion:

    1

    11

    12

    13

    1415

    1(1

    1/

    102

    21

    2223

    24

    252(2

    2/

    203

    importjava.io.*;classMrim$actors{publicstaticvoidmain(String args[]) throws!"#c$ption{

    %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));intn;Sst$m.out.print("nt$r a Cumb$r + );n'nt$g$r.pars$nt(br.r$ad-in$());

    Sst$m.out.print(9h$ Mrim$ actors of :n: ar$ + );

    inti';whil$(n2,) { if(n4i '' /) { Sst$m.out.print(i: );

    n'nAi; 7 $ls$ i::; 777

    #$t%$t:

    %nter a )umber : 3/

    he Prime &a"tors of 3/ are: 2 3 3 3

    Program on Decimal to Roman Number Conversion

    [Method 2]

    .o$tion:

  • 7/25/2019 Solved Programs Computer Class 12

    29/35

    1

    2

    34

    5

    (

    /

    01

    11

    12

    1

    3

    14

    15

    1

    (1

    1

    /1

    0

    2

    2

    12

    2

    2

    32

    4

    25

    2

    (2

    2

    /

    importjava.io.*;class$c&oman0$thod{ publicstaticvoidmain(String args[]) throws!"#c$ption{ %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); intnum; String str'; Sst$m.out.print("nt$r a Cumb$r + ); num'nt$g$r.pars$nt(br.r$ad-in$()); AAacc$pting d$cimal numb$r

    A*>rras storing th$ uniNu$ smbols of &oman Cumb$r Sst$m

    and also th$ corr$sponding d$cimal $Nuival$nts in th$ s$cond arra*A String roman[] ' {01

  • 7/25/2019 Solved Programs Computer Class 12

    30/35

    2

    0

    3

    3

    13

    2

    33

    3

    4

    35

    3

    (

    3

    #$t%$t:

    %nter a )umber : /(3

    8oman %9uivalent ' 7AAAEIII

    Question:

    Write a program in Java to find the 8oman e9uivalent of any 7e"imal number entered by the

    user. Fhe number entered should be !ithin the 8ange 1?3000G

    .o$tion:

    1

    23

    4

    5(

    /

    01

    111

    2

    importjava.io.*;class$c&oman0$thod,{publicstaticvoidmain(String args[]) throws!"#c$ption{%uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));Sst$m.out.print("nt$r a Cumb$r + );intnum'nt$g$r.pars$nt(br.r$ad-in$()); AAacc$pting d$cimal numb$r

    if(num2/55 num=E///) AAch$c6ing wh$th$r th$ numb$r $nt$r$d is within th$rang$ [,3DKKK]{

    A*Saving th$ &oman $Nuival$nt of th$ thousand1 hundr$d1 t$n and units plac$of a d$cimal numb$r*AString thou[]'{1010010007;String hund[]'{1

  • 7/25/2019 Solved Programs Computer Class 12

    31/35

    1

    3

    14

    1

    51

    (

    1

    1

    /

    10

    2

    21

    22

    2

    32

    4

    2

    52

    (

    2

    2

    /2

    0

    3

    String t$n[]'{1R1RR1RRR1R-1-1-R1-RR1-RRR1R

  • 7/25/2019 Solved Programs Computer Class 12

    32/35

    Write a program to a""ept a senten"e and print only the first letter of ea"h !ord of the senten"e

    in "apital letters separated by a full stop.

    'am%e :

    I)P* S%)%)A% : =his is a "at>

    *P* : .I..A.

    .o$tion:

    /

    01

    11

    1

    21

    31

    4

    15

    1

    (

    1

    1/1

    0

    2

    2

    12

    2

    2

    32

    4

    25

    2

    (2

    importjava.io.*;classnitials{publicstaticvoidmain(String args[])throws!"#c$ption{%uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in));String s;char#;intl;

    Sst$m.out.print("nt$r an s$nt$nc$+ );s'br.r$ad-in$();s' :s; AAadding a spac$ infront of th$ inputt$d s$nt$nc$ or a nam$s's.toTpp$r

  • 7/25/2019 Solved Programs Computer Class 12

    33/35

    2

    /2

    0

    #$t%$t:

    %nter any senten"e: Java for s"hool studentsutput ' J.&.S.S.

    *ro"rammin" Code:

    1

    2

    3

    45

    (

    /

    0

    1

    11

    12

    13

    14

    1

    51

    (

    1

    1

    /1

    0

    2

    2

    1

    2

    importjava.io.*;classSwapping0$thod { publicstaticvoidmain(String args[])throws!"#c$ption

    { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); inta1b; Sst$m.out.print("nt$r th$ ,st no+ ); a'nt$g$r.pars$nt(br.r$ad-in$()); Sst$m.out.print("nt$r th$ nd no+ ); b'nt$g$r.pars$nt(br.r$ad-in$());

    Sst$m.out.println(3333333333333333333333333333333);

    Sst$m.out.println(9h$ numb$rs b$for$ swapping ar$); Sst$m.out.println(a ' :a); Sst$m.out.println(b ' :b);

    AA%$ginning of Swapping a'a:b; b'a3b; a'a3b;

    AA"nd of Swapping

    Sst$m.out.println(3333333333333333333333333333333); Sst$m.out.println(9h$ numb$rs aft$r swapping ar$); Sst$m.out.println(a ' :a); Sst$m.out.println(b ' :b); 7 7

  • 7/25/2019 Solved Programs Computer Class 12

    34/35

    2

    2

    32

    4

    25

    #$t%$t:

    %nter the 1st no: 25%nter the 2nd no: 13

    DDDDDDDDDD?

    he numbers before s!apping are

    a ' 25

    b ' 13DDDDDDDDDD?

    he numbers after s!apping area ' 13

    b ' 25

    *ro"rammin" Code:

    1

    23

    45

    (

    /

    01

    111

    2

    13

    1

    4

    15

    1

    (

    importjava.io.*;classSwapping0$thod, { publicstaticvoidmain(String args[])throws!"#c$ption

    { %uff$r$d&$ad$r br'n$w%uff$r$d&$ad$r(n$wnputStr$am&$ad$r(Sst$m.in)); inta1b; Sst$m.out.print("nt$r th$ ,st no+ ); a'nt$g$r.pars$nt(br.r$ad-in$()); Sst$m.out.print("nt$r th$ nd no+ ); b'nt$g$r.pars$nt(br.r$ad-in$());

    Sst$m.out.println(3333333333333333333333333333333);

    Sst$m.out.println(9h$ numb$rs b$for$ swapping ar$); Sst$m.out.println(a ' :a); Sst$m.out.println(b ' :b);

    AA%$ginning of Swapping a'aUb; b'aUb; a'aUb; AA"nd of Swapping

    Sst$m.out.println(3333333333333333333333333333333);

    Sst$m.out.println(9h$ numb$rs aft$r swapping ar$); Sst$m.out.println(a ' :a);

  • 7/25/2019 Solved Programs Computer Class 12

    35/35

    1

    1/

    1

    02

    21

    2

    2

    23

    2

    4

    25

    Sst$m.out.println(b ' :b); 7 7

    #$t%$t:

    %nter the 1st no: 25

    %nter the 2nd no: 13

    DDDDDDDDDD?

    he numbers before s!apping area ' 25

    b ' 13DDDDDDDDDD?he numbers after s!apping are

    a ' 13

    b ' 25