Computer Proj

download Computer Proj

of 74

Transcript of Computer Proj

  • 8/9/2019 Computer Proj

    1/74

    Program 1:To Sort an Array UsingSelection Sort import java.io.*;

    class SelectionSort{int n,i;int a[] ne! int[1""];p#$lic SelectionSort%int nn& ''parameteri(e) constr#ctor{n nn;

    p#$lic voi) inp#t%& t+ro!s - /ception ''0#nction accepting array elements{ #2ere)3ea)er $r ne! #2ere)3ea)er%ne!

    np#tStream3ea)er%System.in&&;System.o#t.println%4enter elements4&;0or%i ";i5n;i66&

    {a[i] nteger.parse nt%$r.rea)7ine%&&;p#$lic voi) )isplay%& ''0#nction )isplaying array elements{System.o#t.println%&;0or%i ";i5n;i66&{System.o#t.print%a[i]64 4&;

    p#$lic voi) sort%& ''0#nction sorting array elements #sing selection sorttec+ni8#e{int j,temp,min;0or%i ";i5n91;i66&

    {min i;0or%j i61;j5n;j66&{i0%a[j]5a[min]&min j;

    i0%min i&{temp a[i];a[i] a[min];a[min] temp;

    p#$lic static voi) main%String args[]& t+ro!s - /ception ''main 0#nction

    {SelectionSort / ne! SelectionSort% &;/.inp#t%&;System.o#t.print%4 e0ore sorting 9 4&;/.)isplay%&;System.o#t.print%4A0ter sorting 9 4&;/.sort%&;/.)isplay%&;

  • 8/9/2019 Computer Proj

    2/74

    Sample -#tp#t

  • 8/9/2019 Computer Proj

    3/74

    Program romntere) =ay ?#m$er

    import java.io.*;

    class =aye$4,4Car4,4Apr4,4Cay4,4B#n4,4B#l4,4A#g4,4Sep4,4-ct4,4?ov4,4=ec4 ;i0 % yr D E "&a[1]

  • 8/9/2019 Computer Proj

    4/74

    Sample -#tp#t

  • 8/9/2019 Computer Proj

    5/74

    Program @: To =isplay t+e ntere)Stringin Alp+a$etical -r)erimport java.io.*;class Alp+a{String str;int l;c+ar c[] ne! c+ar[1""];p#$lic Alp+a%& ''Alp+a%& constr#ctor{str 44;l ";

    p#$lic voi) rea)!or)%& t+ro!s - /ception ''0#nction to rea) inp#t string

    {System.o#t.println%4enter !or) 9 4&;#2ere)3ea)er $r ne! #2ere)3ea)er%ne!np#tStream3ea)er%System.in&&;

    str $r.rea)7ine%&;l str.lengt+%&;

    p#$lic voi) arrange%& ''0#nction to arrange string in ascen)ing or)er{int i,j;c+ar temp;0or%i ";i5l;i66&{c[i] str.c+arAt%i&;

    0or%i ";i5l91;i66& ''loops 0or s!apping o0 c+aracters{0or%j ";j5l919i;j66&{i0%c[j] H c[j61]&{temp c[j];c[j] c[j61];c[j61] temp;

    p#$lic voi) )isplay%& ''0#nction to )isplay t+e rearrange) string{System.o#t.println%&;0or%int i ";i5l;i66&

    {System.o#t.print%c[i]&;p#$lic static voi) main%String args[]& t+ro!s - /ception ''main 0#nction{Alp+a o$j ne! Alp+a%&;o$j.rea)!or)%&;o$j.arrange%&;o$j.)isplay%&;

  • 8/9/2019 Computer Proj

    6/74

    Sample -#tp#t

  • 8/9/2019 Computer Proj

    7/74

    Program E: To print T!in Prime?#m$ers !it+in a rangeimport java.io.*;class TwinPrimeRange{

    boolean isPrime(int n) //funton for chec ing prime { int count!"; for(int i!#; i$!n; i%%) { if(n&i !! ") count%%; '

    if(count !! ) return true; else return false; '

    public static voi main( tring args+,) throws - 0ception { TwinPrimeRange ob ! new TwinPrimeRange(); 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter the lower range 4 3); int p ! -nteger.parse-nt(br.rea 5ine()); 2stem.out.print(3 nter the upper range 4 3); int 6 ! -nteger.parse-nt(br.rea 5ine());

    if(p76) 2stem.out.println(3-nvali Range 83); else { 2stem.out.println(39nThe Twin Prime :umbers within the given range are 4 3); for(int i!p; i$!(6 ); i%%) { if(ob.isPrime(i) !! true

  • 8/9/2019 Computer Proj

    8/74

    Sample -#tp#tEnter t he lower range : 1Enter the upper range : 200

    The Twin P rime N umbers w ithin the given range are :(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73) (101,103) (107,109) (137,139) (149,151)(179,181) (191,193) (197,199)

    Program : To generate >i$onaccistrings

  • 8/9/2019 Computer Proj

    9/74

    import java.io.*;class >ibo tring{ tring 0=2=?; int n;

    >ibo tring() // @onstructor { 0 ! 3a3; 2 ! 3b3; ? ! 3ba3; // mentione in the 6uestion otherwise not re6uire . ? ! 33 is sufficient '

    voi accept()throws - 0ception { 1uffere Rea er br ! new 1uffere Rea er(new -nput treamRea er( 2stem.in)); 2stem.out.print(39n nter the number of terms 4 3); n ! -nteger.parse-nt(br.rea 5ine()); '

    voi generate() { 2stem.out.print(39nThe >ibonacci tring eries is 4 3); if(n $! #) // -f no of terms is less than or e6ual to # 2stem.out.print(0); else // -f no of terms is more than or e6ual to { 2stem.out.print(0%3= 3%2); for(int i!A; i$!n; i%%) {

    ? ! 2%0; 2stem.out.print(3= 3%?); 0 ! 2; 2 ! ?; ' ' '

    public static voi main( tring args+,) throws - 0ception { >ibo tring ob ! new >ibo tring(); ob.accept();

    ob.generate(); ''

    Sample -#tp#t

  • 8/9/2019 Computer Proj

    10/74

    1) Enter the number of terms : 2The Fibonacci String Series i s : a, b

    2) Enter the number of terms : 5The Fibonacci String Series i s : a, b, ba, bab, babba

  • 8/9/2019 Computer Proj

    11/74

    Program I: To print a composite magicn#m$erimport java.io.*;class Bagic@ompositeC- @ "#D{

    boolean is@omposite(int n) // >unction to chec for @omposite number { int count!"; for(int i!#;i$!n;i%%) { if(n&i!!") count%%;

    ' if(count7 ) return true; else return false; '

    int sumEig(int n) // >unction to return sum of igits of a number { int s ! ";

    while(n7") { s ! s % n"; n ! n/#"; ' return s; '

    boolean isBagic(int n) // >unction to chec for Bagic number {

    int a ! sumEig(n); while(a7F) { a ! sumEig(a); '

    if(a !! #)

  • 8/9/2019 Computer Proj

    12/74

    return true; else return false; '

    public static voi main( tring args+,)throws - 0ception { Bagic@ompositeC- @ "#D ob ! new Bagic@ompositeC- @ "#D(); 1uffere Rea er br!new 1uffere Rea er (new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter the lower limit(m) 4 3); int m!-nteger.parse-nt(br.rea 5ine()); 2stem.out.print(3 nter the upper limit(n) 4 3); int n!-nteger.parse-nt(br.rea 5ine());

    int c!"; if (m$n) { 2stem.out.println(3The @omposite Bagic -ntegers are4 3); for(int i!m; i$!n; i%%) { if(ob.is@omposite(i)!!true

  • 8/9/2019 Computer Proj

    13/74

  • 8/9/2019 Computer Proj

    14/74

    Program J: To c+ecK 0or Leit+ n#m$erA Keith Number is a n integer N with d digits w ith the following property:

    If a F ibonacci-like sequence (in which each term in t he s equence is t he sum of the d previousterms) is f ormed, with the rst d terms b eing the decimal digits o f the number N, then N itself occursas a term in the sequence.

    For example, 197 is a Keith number since it generates the s equence1, 9, 7, 17, 33, 57, 107, 197, ..

    import java.io.*;class Jeith{

    public static voi main( tring args+,)throws - 0ception { 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in)); 2stem.out.print(3 nter the number 4 3); //inputting the number int n!-nteger.parse-nt(br.rea 5ine());

    int cop2!n; tring s!-nteger.to tring(n); int !s.length(); //fin ing the number of igits ( ) in the number int arr+,!new int+n,; //arra2 for storing the terms of the series

    for(int i! #; i7!"; i ) { arr+i,!cop2"; //storing the igits of the number in the arra2

    cop2!cop2/#";

    '

    int i! =sum!"; while(sum$n) //fin ing the sum till it is less than the number {

  • 8/9/2019 Computer Proj

    15/74

    sum ! "; for(int j!#; j$! ; j%%) //loop for generating an a ing the previous K K terms { sum!sum%arr+i j,; ' arr+i,!sum; //storing the sum in the arra2 i%%; '

    /* Lhen the control comes out of the while loop= either the sum is e6ual to the number or greater than it */

    if(sum!!n) //if sum is e6ual to the number= then it is a Jeith number 2stem.out.println(3The number is a Jeith :umber3); else

    2stem.out.println(3The number is a not a Jeith :umber3); ''

    Sample -#tp#tEnter the number : 197The number is a K eith Number

    Enter the number : 14The number is a K eith Number

    Enter the number : 53The n umber is a not a Keith Number

  • 8/9/2019 Computer Proj

    16/74

    Program : To print t+e possi$leconsec#tive n#m$er com$inationsimport java.io.*;

    class - @"MPN# { public static voi main( tring args+,)throws - 0ception { 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in)); 2stem.out.print(3 nter a number 4 3); //inputting the number int n!-nteger.parse-nt(br.rea 5ine());

    int sum!"=c!"=j!"; for(int i!#;i$n;i%%)

    { sum!i; j!i%#; /* a ing consecutive natural numbers till sum is less than the given number */

    while(sum$n) {sum!sum%j; j%%; ' /* when the above while con ition is false= then either the sum is e6ual to

    that number or is greater than that number o= we will be printing the series of consecutive numbers onl2 if the sum ise6ual to that number */

    if(sum!!n) { for(int !i; $j; %%) { if( !!i)

    2stem.out.print( ); else 2stem.out.print(3 % 3% ); ' 2stem.out.println(); ' ' ' '

  • 8/9/2019 Computer Proj

    17/74

    Sample -#tp#t1. Enter a n umber : 15

    1 + 2 + 3 + 4 + 54 + 5 + 67 + 8

    2. Enter a n umber : 95

    5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 1417 + 18 + 19 + 20 + 2147 + 48

    Program F: To remove repeate)

    c+aracters in a !or)import java.io.*;class RemoveEup@har { public static voi main( tring args+,)throws - 0ception { 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

  • 8/9/2019 Computer Proj

    18/74

    2stem.out.print(3 nter an2 wor 4 3); // -nputting the wor tring s ! br.rea 5ine();

    s ! s % 3 3; // H ing a space at the en of the wor int l!s.length(); // >in ing the length of the wor tring ans!33; // Oariable to store the final result char ch#=ch ;

    for(int i!"; i$l #; i%%) { ch#!s.charHt(i); // 0tracting the first character ch !s.charHt(i%#); // 0tracting the ne0t character // H ing the first e0tracte character to the result if the current an the ne0t characters are

    ifferent

    if(ch#8!ch )

    {ans ! ans % ch#;

    ' ' 2stem.out.println(3Lor after removing repeate characters ! 3%ans); // Printing the result ''

    Sample -#tp#tExample 1:Enter any word: JaaavvvvvvvvaaaaaaaaaaaWord a fter removing repeated characters = Java

    Example 2:Enter any word: iiiiiisssssfffffffffffffuunnnnnWord a fter r emoving repeated characters = isfun

  • 8/9/2019 Computer Proj

    19/74

    Program 1": To c+ecK 0or Smit+ n#m$erA Smith number is a composite number, the sum of whose digits i s t he sum of the digits o f itsprime factors o btained as a result of prime factorization (excluding 1). The rst few such numbers a re4, 22, 27, 58, 85, 94, 121 ..

    import java.io.*;class mith{ static 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in)); //function for fin ing sum of igits int sumEig(int n) { int s!"; while(n7") { s!s%n"; n!n/#"; ' return s; ' //function for generating prime factors an fin ing their sum int sumPrime>act(int n) { int i! = sum!"; while(n7#) { if(n&i!!")

  • 8/9/2019 Computer Proj

    20/74

    { sum!sum%sumEig(i); // ere KiK is the prime factor of KnK an we are fin ing its sum n!n/i; ' else i%%; ' return sum; ' public static voi main( tring args+,) throws - 0ception{ mith ob!new mith(); 2stem.out.print(3 nter a :umber 4 3); int n!-nteger.parse-nt(br.rea 5ine()); int a!ob.sumEig(n);// fin ing sum of igit int b!ob.sumPrime>act(n); //fin ing sum of prime factors

    2stem.out.println(3 um of Eigit ! 3%a); 2stem.out.println(3 um of Prime >actor ! 3%b);

    if(a!!b) 2stem.out.print(3-t is a mith :umber3); else 2stem.out.print(3-t is :ot a mith :umber3);''

    Sample -#tp#t1. Enter a Number : 94Sum of Digit = 13Sum of Prime Factor = 13It is a S mith Number

    2. Enter a Number : 102Sum of Digit = 3Sum of Prime Factor = 13It is Not a S mith Number

    3. Enter a Number : 4937775

    Sum of Digit = 42Sum of Prime Factor = 42It is a S mith Number

  • 8/9/2019 Computer Proj

    21/74

    Program 11: To create a PascalMstriangleimport java.io.*;class Pascal{p#$lic voi) pascal!%&t+ro!s - /ception ''pascal!%& 0#nction{ #2ere)3ea)er $r ne! #2ere)3ea)er%ne!

    np#tStream3ea)er%System.in&&;System.o#t.println%N nter a no.O&;int n nteger.parse nt%$r.rea)7ine%&&; ''accepting val#eint [ ] pas ne! int [n61];

    pas["] 1;0or %int i "; i5n; i66& ''loop eval#ating t+e elements{0or %int j "; j5 i; 66j&System.o#t.print%pas[j]64 4&; ''printing t+e Pascal Triangle elementsSystem.o#t.println% &;0or %int j i61; jH"; j99&pas[j] pas[j]6pas[j91];

  • 8/9/2019 Computer Proj

    22/74

    Program 1

  • 8/9/2019 Computer Proj

    23/74

    {

    1uffere Rea er br!new 1uffere Rea er (new -nput treamRea er ( 2stem.in));

    2stem.out.print(3 nter an2 sentence 4 3); // -nputting the sentence

    tring s ! br.rea 5ine();

    s ! s.toGpper@ase(); // @onverting the sentence into Gpper @ase

    int l ! s.length();

    tring ans!33; // tring variable to store the final result

    char last ! s.charHt(l #); // 0tracting the last character

    /* @hec ing whether the sentence en s with K.K= KQK or a K8K or not */

    if(last !! K.K last !! KQK last !! K8K)

    {

    tring wor +,!s.split(3+.Q8 ,%3); // aving the wor s in an arra2 using split()

    int c ! wor .length; // >in ing the number of wor s

    2stem.out.print(3 nter the wor to elete 4 3);

    tring el ! br.rea 5ine();

    el ! el.toGpper@ase();

    2stem.out.print(3 nter the wor position in the sentence 4 3);

    int 0 ! -nteger.parse-nt(br.rea 5ine());

    if(0$# 07c) // @hec ing whether integer inputte is acceptable or not

    {

    2stem.out.println(3 orr28 The wor position entere is out of range3);

    '

    else

    {

    for(int i!"; i$c; i%%)

  • 8/9/2019 Computer Proj

    24/74

    {

    /* ipping if the wor to elete an the position matches */

    if(wor +i,.e6uals( el)!!true

  • 8/9/2019 Computer Proj

    25/74

    Program 1@:To )eco)e an encrypte)co)eA simple encryption system uses a shifting process to hide a message. The value of the shift can be

    in the range 1 to 2 6. For example a shift of 7 means that A = U, B =V,C = W, etc.i e.

    Text : A B C D E F G H I J K L M N O P Q R S T U V W X Y ZCode: U V W X Y Z A B C D E F G H I J K L M N O P Q R S T

    Fist an extra space is a dded to the end of the string. To make things l ittle more difficult, spaces w ithinthe original text are replaced with QQ before the text is encrypted. D ouble Q (QQ) was selectedbecause no English w ord e nds in Q or contains Q Q.

  • 8/9/2019 Computer Proj

    26/74

    Additionally the coded message is p rinted in blocks of six c haracters s eparated by spaces. The lastblock might not contain six characters.

    import java.io.*;

    public class Eeco eC- @ ""A

    {

    public static voi main( tring args+,)throws - 0ception

    {

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter @o e Te0t 4 3); // inputting co e te0t

    tring s ! br.rea 5ine();

    int l ! s.length();

    s ! s.toGpper@ase(); // converting the co e te0t into Gppercase

    s ! s % 3 3; // a ing a space at the en

    if(l7!#"") // chec ing whether length of inputte co e is less than #""

    2stem.out.println(3888 -nvali 5ength of @o e Te0t 8883);

    else

    {

    2stem.out.print(3 nter the hift Oalue 4 3);

    int shift ! -nteger.parse-nt(br.rea 5ine());

    if(shift$# shift7 M) // chec ing whether shift value is between # an M

    2stem.out.println(3888 -nvali hift Oalue 8883);

    else

    {

    int a= b;

  • 8/9/2019 Computer Proj

    27/74

    char ch#= ch ;

    tring ec!33; //new tring for storing the eco e te0t

    for(int i!"; i$l; i%%)

    { ch# ! s.charHt(i); // e0tracting characters one b2 one

    ch ! s.charHt(i%#); // e0tracting the ne0t character

    /* 1elow we are a ing shift value to the characters

    * if ch# ! KHK an shift ! S=

    * then ch# % shift # will give us4 KHK%S # ! M %S # ! S#

    * which is the H @-- value of KIK

    */

    a ! ch# % shift #; // storing H @-- values after a ing shift to the current character

    b ! ch % shift #; // storing H @-- values after a ing shift to the ne0t character

    /* -f the currrent character an the ne0t character are both KNK then we have a KspaceK

    * hence the H @-- value shoul be A

    */

    if((char)a !! KNK

  • 8/9/2019 Computer Proj

    28/74

    a ! a M;

    if(ch# 8! K K)

    ec ! ec % (char)a; // finall2 a ing the eco e character to the new tring

    ' 2stem.out.println(3Eeco e Te0t 4 3% ec);

    ' ' ''

    Output:

    Example 1:Enter Coded Text : UHINBYLKKQCHHYLKKEnter the Shift Value : 7Decoded Text : ANOTHER WINNER

    Example 2:Enter Coded Text : RUIJGGEVGGBKSAGGEnter the Shift Value : 11Decoded Text : BEST OF LUCK

    Example 3:Enter Coded Text : UHINBYLKKQCHHYLKKEnter the Shift Value : 27

    Invalid Shift Value.

    Program 1E:To print given time in !or)simport java.io.*;

    public class Time-nLor sC- @ ""A

    { public static voi main( tring args+,)throws - 0ception

  • 8/9/2019 Computer Proj

    29/74

    { 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    /* -nputting hours an minutes */

    2stem.out.print(3 nter ours 4 3);

    int h!-nteger.parse-nt(br.rea 5ine());

    2stem.out.print(3 nter Binutes 4 3);

    int m!-nteger.parse-nt(br.rea 5ine());

    if((h7!#

  • 8/9/2019 Computer Proj

    30/74

    a ! wor s+#,; //storing KoneK when hour is #

    else

    a ! wor s+h%#,; //if hour is not # = then storing in wor s= an hour ahea of given hour

    /* The below co e chec s minutes an accor ingl2 prints the time in wor s using arra2. */

    2stem.out.print(3 utput 4 3%h%343%m%3 3); //printing the given time in numbers

    if(m!!")

    2stem.out.println(wor s+h,%3 K cloc 3);

    else if(m!!# )

    2stem.out.println(3Nuarter past 3%wor s+h,);

    else if(m!!A")

    2stem.out.println(3 alf past 3%wor s+h,);

    else if(m!!D )

    2stem.out.println(3Nuarter to 3%a);

    else if(m$A") // con ition for minutes between # F

    2stem.out.println(wor s+m,%3 3%plu%3 past 3%wor s+h,);

    else // con ition for minutes between A# F

    2stem.out.println(wor s+M" m,%3 3%plu%3 to 3%a);

    ' //en of outer if

    else

    2stem.out.println(3-nvali -nput 83); //printing error message for illegal input

    ''

    Output:

    Example 1:Enter Hours : 12Enter Minutes : 39Output : 12:39 Twenty one Minutes to One

  • 8/9/2019 Computer Proj

    31/74

    Example 2:Enter Hours : 12Enter Minutes : 39Output : 12:39 Twenty one Minutes to One

    Example 3:Enter Hours : 7Enter Minutes : 16Output : 7:16 S ixteen Minutes p ast Seven

    Example 4:Enter Hours : 5Enter Minutes : 30Output : 5:30 H alf past Five

  • 8/9/2019 Computer Proj

    32/74

    Program 1 :To print a magic s8#areA square m atrix is sai d to b e a Magic S quare, if the sum of each r ow, each co lumn and e achdiagonal is sa me. Write a program to enter an integer number n. Create a m agic sq uare of sizen*n.

    import java.io.*;

    class BagicCBatri0

    { public static voi main( tring args+,)throws - 0ception

    { 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(39n9n nter the si?e of the matri0 4 3);

    int n!-nteger.parse-nt(br.rea 5ine());

    if(n7 )

    2stem.out.println(3 nter a number between # to 3);

    else

    { int H+,+,!new int+n,+n,; // @reating the Bagic Batri0

    int i=j= =t;

    /*-nitiali?ing ever2 cell of the matri0 with " */

    for(i!";i$n;i%%)

    {

    for(j!";j$n;j%%)

    { H+i,+j, ! ";

    ' '

  • 8/9/2019 Computer Proj

    33/74

    /* Lhen the si?e of the matri0 is */

    if(n& 8!")

    { i!";

    j ! n/ ;

    ! #;

    while( $!n*n)

    { H+i,+j, ! %%;

    i ; // Ba ing one step upwar

    j%%; // Boving one step to the right

    if(i$"

  • 8/9/2019 Computer Proj

    34/74

    /* >illing the matri0 with natural numbers from # till n*n */

    for(i!";i$n;i%%)

    { for(j!";j$n;j%%)

    { H+i,+j, ! %%;

    ' '

    j ! n #;

    for(i!"; i$n/ ; i%%)

    { /* swapping corner elements of primar2 iagonal */

    t ! H+i,+i,;

    H+i,+i, ! H+j,+j,;

    H+j,+j, ! t;

    /* swapping corner elements of secon ar2 iagonal */

    t ! H+i,+j,;

    H+i,+j, ! H+j,+i,;

    H+j,+i, ! t;

    j ;

    ' '

    /* Printing the Bagic matri0 */

    2stem.out.println(3The Bagic Batri0 of si?e 3%n%303%n%3 is43);

    for(i!";i$n;i%%)

    { for(j!";j$n;j%%)

    { 2stem.out.print(H+i,+j,% 39t3);

    '

    2stem.out.println();

  • 8/9/2019 Computer Proj

    35/74

    ' ' ' '

    Output

  • 8/9/2019 Computer Proj

    36/74

    Program 1I:To c+ecK t+e vali)ity o0 an Cn#m$erThe International M obile Station Equipment Identity or IMEI is a number, u sually unique, toidentify m obile phones, as w ell as s ome satellite phones. It is u sually found printed inside the batterycompartment of the phone.

    The IMEI number is us ed by a GSM network to identify val id d evices an d therefore ca n b e u sed for

    stopping a stolen p hone from accessing that network.

    The IMEI (15 decimal digits: 14 digits p lus a check digit) includes i nformation on the origin, model,and s erial number of the d evice.

    The IMEI is v alidated in three steps:

    1. Starting from the right, double e very other digit (e.g., 7 b ecomes 1 4).

    2. Sum the digits (e.g., 14 1 + 4) .

    3. Check if the sum is divisible by 10.

    import java.io.*;

    class -B -

    { int sumEig(int n) // >unction for fin ing an returning sum of igits of a number

    {

    int a ! ";

    while(n7")

    { a ! a % n";

    n ! n/#";

    '

  • 8/9/2019 Computer Proj

    37/74

    return a;

    '

    public static voi main( tring args+,)throws - 0ception

    { -B - ob ! new -B -();

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter a # igit -B - co e 4 3);

    long n ! 5ong.parse5ong(br.rea 5ine()); // # igits cannot be store in KintK ata t2pe

    tring s ! 5ong.to tring(n); // @onverting the number into tring for fin ing length

    int l ! s.length();

    if(l8!# ) // -f length is not # then -B - is -nvali

    2stem.out.println(3 utput 4 -nvali -nput3);

    else

    {

    int ! "= sum ! ";

    for(int i!# ; i7!#; i )

    { ! (int)(n");

    if(i& !! ")

    { ! * ; // Eoubling ever2 alternate igit

    '

    sum ! sum % ob.sumEig( ); // >in ing sum of the igits

    n ! n/#";

    '

    2stem.out.println(3 utput 4 um ! 3%sum);

  • 8/9/2019 Computer Proj

    38/74

    if(sum"!!")

    2stem.out.println(3Oali -B - @o e3);

    else

    2stem.out.println(3-nvali -B - @o e3);

    ' ''

    Output:

    1. Enter a 15 digit IMEI code : 654122487458946Output : Sum = 80Valid IMEI Code

    2. Enter a 15 digit IMEI code : 799273987135461Output : Sum = 79Invalid IMEI Code

    3. Enter a 15 digit IMEI code : 79927398713Output : Invalid Input

  • 8/9/2019 Computer Proj

    39/74

    Program 1J:To c+ecK !+et+er a n#m$er is#ni8#eA Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7,135, 214 a re all unique numbers w hereas 33 , 3121, 300 are n ot.

    import java.io.*;

    class Gni6ue:umber

    {

    public static voi main( tring args+,)throws - 0ception

    {

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter an2 number 4 3);

    int n!-nteger.parse-nt(br.rea 5ine());

  • 8/9/2019 Computer Proj

    40/74

    tring s!-nteger.to tring(n); //converting the number into tring form

    int l!s.length();

    int flag!";

    /* loop for chec ing whether there are repeate igits */

    for(int i!";i$l #;i%%)

    {

    for(int j!i%#;j$l;j%%)

    {

    if(s.charHt(i)!!s.charHt(j)) //if an2 igits match= then we now it is not a Gni6ue :umber

    {

    flag!#;

    brea ;

    '

    '

    '

    if(flag!!")

    2stem.out.println(3**** The :umber is a Gni6ue :umber ****3);

    else

    2stem.out.println(3**** The :umber is :ot a Gni6ue :umber ****3);

    '

    '

    Output:

    Enter any n umber : 3121**** The Number is Not a Unique Number ****

  • 8/9/2019 Computer Proj

    41/74

    Enter any n umber : 5243**** The Number is a U nique Number ****

    Program 1 :To convert a )ecimal n#m$er tooctal n#m$erOctal Number system is a number system which can represent a number in any other numbersystem in terms of digits r anging from 0 to 7 only. This number system consists o f only e ight basicdigits i.e. 0, 1, 2, 3, 4, 5, 6 and 7.

    For Example: 25 in the Decimal number system can be represented as 31 in the Octal numbersystem.

    import java.io.*;

  • 8/9/2019 Computer Proj

    42/74

    class Eec ct

    { public static voi main( tring args+,)throws - 0ception

    { 1uffere Rea er br!new 1uffere Rea er (new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter a ecimal number 4 3);

    int n!-nteger.parse-nt(br.rea 5ine());

    int r;

    tring s!33; //variable for storing the result

    //arra2 storing the igits (as characters) in the octal number s2stem

    char ig+,!{K"K=K#K=K K=KAK=KDK=K K=KMK=KSK';

    while(n7")

    { r!n&U; //fin ing remain er b2 ivi ing the number b2 U

    s! ig+r,%s; //a ing the remain er to the result an reversing at the same time

    n!n/U;

    ' 2stem.out.println(3 utput ! 3%s); ''

    Output:

    Enter a d ecimal number : 25Output = 31

    Enter a d ecimal number : 465Output = 721

    Program 1F:To s!ap strings !it+o#t a t+ir)

    varia$leimport java.io.*;

    class wapC trings

    { public static voi main( tring args+,)throws - 0ception

  • 8/9/2019 Computer Proj

    43/74

    { 1uffere Rea er br!new 1uffere Rea er (new -nput treamRea er ( 2stem.in));

    2stem.out.print(3 nter the #st tring 4 3);

    tring s#!br.rea 5ine();

    int len#!s#.length();

    2stem.out.print(3 nter the n tring 4 3);

    tring s !br.rea 5ine();

    2stem.out.println(3 3);

    2stem.out.println(3 trings 1efore wapping 4 3);

    2stem.out.println(3#st tring ! 3%s#);

    2stem.out.println(3 n tring ! 3%s );

    /* wapping Process 1egins*/

    s#!s#%s ;

    s !s#.substring("=len#);

    s#!s#.substring(len#);

    /* wapping Process n s*/

    2stem.out.println(3 3);

    2stem.out.println(3 trings Hfter wapping 4 3);

    2stem.out.println(3#st tring ! 3%s#);

    2stem.out.println(3 n tring ! 3%s );

    '

    '

    Output:

    Enter the 1st String : Java ForEnter t he 2nd String : School is F un-Strings B efore S wapping :1st String = Java F or

  • 8/9/2019 Computer Proj

    44/74

    2nd String = School is F un-Strings A fter Swapping :1st String = School is F un2nd S tring = Java F or

  • 8/9/2019 Computer Proj

    45/74

    Program

  • 8/9/2019 Computer Proj

    46/74

    2stem.out.println(3 utput43);

    2stem.out.println(3!!!!!!!!!!!!!!!!!!!!!!!!!!3); //this is just for st2ling the loo of the output

    2stem.out.println(3Hlphabet9t>re6uenc23);

    2stem.out.println(3!!!!!!!!!!!!!!!!!!!!!!!!!!3);

    /* @ounting fre6uenc2 of alphabets begins below */

    for(int i!"; i$ M; i%%)

    { for(int j!"; j$l; j%%)

    { ch!s.charHt(j); //e0tracting characters of the string one b2 one

    if(ch!!alph+i,) //first chec ing the whole string for KaK= then KbK an so on

    fre6+i,%%; //increasing count of those aplhabets which are present in the string ' '

    for(int i!"; i$ M; i%%)

    {

    if(fre6+i,8!") //printing onl2 those alphabets whose count is not K"K

    2stem.out.println(3 3%alph+i,%39t9t 3%fre6+i,);

    ' ' '

    Output:

    Enter an y string: ilovejavaforschool

    Output:==========================Alphabet Frequency==========================

    a 2c 1e 1f 1h 1i 1

    j 1

  • 8/9/2019 Computer Proj

    47/74

    l 2o 4r 1s 1v 2

    Program

  • 8/9/2019 Computer Proj

    48/74

    int t!(n/#")"; //fin ing the igit at tenKs place

    int u!n"; //fin ing the igit at unitKs place

    2stem.out.print(3 utput ! 3);

    /*@on ition for printing igit at thousan Ks place= is that it shoul not be ?ero*/

    if(th8!")

    2stem.out.print(unit+th,%3 Thousan 3);

    /*@on ition for printing igit at hun re Ks place= is that it shoul not be ?ero*/

    if(h8!")

    2stem.out.print(3 3%unit+h,%3 un re 3);

    /*@on ition for printing the wor 3Hn 3*/

    if((t8!" u8!")

  • 8/9/2019 Computer Proj

    49/74

    3. Enter a Number : 54987Out of Range

    Program 22: To print a circular(spiral)matrixFor example: if n = 4, then n*n = 16, hence the array will be lled as g iven below.

    import java.io.*;

    class @ircularCBatri0

    {

  • 8/9/2019 Computer Proj

    50/74

    public static voi main( tring args+,)throws - 0ception

    {

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter the number of elements 4 3);

    int n!-nteger.parse-nt(br.rea 5ine());

    int H+,+,!new int+n,+n,;

    int !#= c#!"= c !n #= r#!"= r !n #;

    while( $!n*n)

    {

    for(int i!c#;i$!c ;i%%)

    {

    H+r#,+i,! %%;

    '

    for(int j!r#%#;j$!r ;j%%)

    {

    H+j,+c ,! %%;

    '

    for(int i!c #;i7!c#;i )

    { H+r ,+i,! %%;

    '

    for(int j!r #;j7!r#%#;j )

    { H+j,+c#,! %%;

    '

  • 8/9/2019 Computer Proj

    51/74

    c#%%;

    c ;

    r#%%;

    r ;

    '

    /* Printing the @ircular matri0 */

    2stem.out.println(3The @ircular Batri0 is43);

    for(int i!";i$n;i%%)

    { for(int j!";j$n;j%%)

    {

    2stem.out.print(H+i,+j,% 39t3);

    '

    2stem.out.println();

    ' ' '

    Output:

  • 8/9/2019 Computer Proj

    52/74

    Program i$onacci series #singrec#rsionimport java.io.*;

    class Recursion

    {

    static 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    int a=b=c=limit;

    Recursion() //@onstructor

    {

    a!";

  • 8/9/2019 Computer Proj

    53/74

    b!#;

    c!";

    limit!";

    '

    voi input()throws - 0ception //>unction to input the limit

    {

    2stem.out.print(3 nter the limit 4 3);

    limit!-nteger.parse-nt(br.rea 5ine());

    '

    int fib(int n) //Recursive function generating the KnthK term of >ibonacci eries

    {

    if(n$!#)

    return a;

    else if(n!! )

    return b;

    else

    return (fib(n #)%fib(n ));

    '

    voi generateCfibseries() //>unction generating all the >ibonacci eries numbers upto KnK terms

    {

    2stem.out.println(3The >ibonacci eries is43);

    for(int i!#;i$!limit;i%%)

    {

    c!fib(i);

    2stem.out.print(c%3 3);

    '

  • 8/9/2019 Computer Proj

    54/74

    '

    public static voi main( tring args+,)throws - 0ception

    { Recursion ob!new Recursion();

    ob.input();

    ob.generateCfibseries();

    '

    '

    Output:

    Enter the limit : 11The Fibonacci Series i s:0 1 1 2 3 5 8 13 21 34 55

    Enter the limit : 20The Fibonacci Series i s:0 1 1 2 3 5 8 13 21 34 55 89 44 233 377 610 987 1597 2584 4181

    Program

  • 8/9/2019 Computer Proj

    55/74

    int l= 2= = m;

    tring = mm= 22;

    //arra2 storing the ma0imum a2s of ever2 month

    int ma0 a2s+,!{"=A#= U=A#=A"=A#=A"=A#=A#=A"=A#=A"=A#'; //arra2 storing the month names

    tring month+,!{ 33= 3Wanuar23= 3>ebruar23= 3Barch3= 3Hpril3= 3Ba23= 3Wune3= 3Wul23= 3Hugust3=

    3 eptember3= 3 ctober3= 3:ovember3= 3Eecember3 ';

    2stem.out.print(3 nter an2 ate in U igits ( mm2222) format4 3);

    tring ate ! br.rea 5ine(); //inputting the ate in tring format

    l ! ate.length(); //fin ing number of igits in the given input

    if(l!!U) //performing the tas onl2 when number of igits is U

    {

    ! ate.substring("= ); //e0tracting the a2 in tring format

    mm ! ate.substring( =D); //e0tracting the month in tring format

    22 ! ate.substring(D); //e0tracting the 2ear in tring format

    ! -nteger.parse-nt( ); // a2 in -nteger format

    m ! -nteger.parse-nt(mm); //month in -nteger format

    2 ! -nteger.parse-nt(22); //2ear in -nteger format

    if((2&D""!!") ((2""8!")

  • 8/9/2019 Computer Proj

    56/74

    '

    else

    { /* >irst Part */ 2stem.out.println(3Eate in /mm/2222 format ! 3% %3/3%mm%3/3%22);

    /* econ Part */

    2stem.out.print(3Eate in = month name= 2222 format ! 3% %3 3%month+m,%3= 3%22);

    ' '

    else

    2stem.out.println(3Lrong -nput3);

    ''

    Output:

    1) Enter any d ate in 8 digits ( ddmmyyyy) format: 02052013Date in dd/mm/yyyy format = 02/05/2013Date in dd, month name, yyyy format = 02 May, 2013

    2) Enter any d ate in 8 digits ( ddmmyyyy) format: 12111963Date in dd/mm/yyyy format = 12/11/1963

    Date in dd, month name, yyyy format = 12 November, 1963

    3) Enter any d ate in 8 digits ( ddmmyyyy) format: 252013Wrong Input

    4) Enter any d ate in 8 digits ( ddmmyyyy) format: 29022013The day, month or year are outside acceptable limit

    Program < :To print t+e )i2erence $et!een

  • 8/9/2019 Computer Proj

    57/74

    class EateCEifference

    {

    static 1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    int month+,!{"=A#= U=A#=A"=A#=A"=A#=A#=A"=A#=A"=A#';

    //function for chec ing for 5eap Vear

    int is5eap(int 2)

    {

    if((2&D""!!") ((2""8!")

  • 8/9/2019 Computer Proj

    58/74

    {

    int n!";

    month+ ,!is5eap(2);

    for(int i!#;i$m;i%%)

    {

    n! n%month+i,;

    '

    n! n% ;

    for(int i!#;i$2;i%%)

    {

    if(is5eap(i)!! F)

    n! n%AMM;

    else

    n! n%AM ;

    '

    return n;

    '

    public static voi main( tring args+,)throws - 0ception

    {

    EateCEifference ob!new EateCEifference();

    2stem.out.print(3 nter the #st ate in ( /mm/2222) format4 3);

    tring ate#!br.rea 5ine().trim();

    int p=6;

  • 8/9/2019 Computer Proj

    59/74

    // 0tracting the a2

    p! ate#.in e0 f(3/3);

    int #!-nteger.parse-nt( ate#.substring("=p));

    // 0tracting the month

    6! ate#.last-n e0 f(3/3);

    int m#!-nteger.parse-nt( ate#.substring(p%#=6));

    // 0tracting the 2ear

    int 2#!-nteger.parse-nt( ate#.substring(6%#));

    2stem.out.print(3 nter the n ate in ( /mm/2222) format4 3);

    tring ate !br.rea 5ine().trim();

    p! ate .in e0 f(3/3);

    int !-nteger.parse-nt( ate .substring("=p));

    6! ate .last-n e0 f(3/3);

    int m !-nteger.parse-nt( ate .substring(p%#=6));

    int 2 !-nteger.parse-nt( ate .substring(6%#));

    //Oali ating both the ates

    if(ob. ateOali ate( #=m#=2#)!!true

  • 8/9/2019 Computer Proj

    60/74

    2stem.out.println(3-nvali Eate3);

    '

    '

    OUTPUT:

    Date 1 : 20/12/2012

    Date 2 : 11/02/2013

    Output:Difference = 54 days

  • 8/9/2019 Computer Proj

    61/74

    Program

  • 8/9/2019 Computer Proj

    62/74

    Enter a d ecimal number : 6Output = 110

    Program

  • 8/9/2019 Computer Proj

    63/74

    Enter a d ecimal number : 1243Output = 4DB

    Program < :To inp#t m#ltiple sentences an)reverse it !it+o#t p#nct#ation marKsThe input in this q uestion will consist of a number of lines o f English text consisting of the letters o fthe English alphabets, the punctuation marks () apostrophe, (.) full stop, (,) comma , (;) semicolon ,(:) colon and white space. Write a program to print the words o f the input in reverse order without anypunctuation m arks o ther than b lanks.

    import java.io.*;import java.util.*;

    class entCBergeCRev

    {

    public static voi main( tring args+,)throws - 0ception

    {

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    2stem.out.print(3 nter the number of sentences4 3); //inputting the number of sentences to accept

    int n ! -nteger.parse-nt(br.rea 5ine());

    tring s ! 33;

    for(int i!#; i$!n; i%%)

    {

    2stem.out.print(3 nter entence 3%i%34 3);

    s ! s % br.rea 5ine(); //inputting multiple sentences an joining them in the same tring

    '

  • 8/9/2019 Computer Proj

    64/74

    //converting the sentence into tringTo eni?er giving the given punctuation mar s

    tringTo eni?er str!new tringTo eni?er(s=3 K.=;48Q3);

    int c!str.countTo ens();

    tring w!33= rev!33;

    for(int i!#; i$!c; i%%)

    {

    w ! str.ne0tTo en(); //e0tracting one wor at a time

    rev ! w%3 3%rev; //joining the e0tracte wor s in reverse or er

    '

    2stem.out.println(3 utput4 3%rev);

    '

    '

    Output:

    Example 1:Enter the number of sentences: 2Enter Sentence 1: Emotions, controlled and directed to work, is character.Enter Sentence 2: By Swami Vivekananda.Output: Vivekananda Swami By ch aracter i s w ork to directed and controlled Emotions

    Example 2:Enter the number of sentences: 3Enter Sentence 1: Did you know?Enter Sentence 2: ICSE and ISC computer is really easy.Enter Sentence 3: Thanks to javaforschool!Output: javaforschool to Thanks easy r eally is co mputer ISC and ICSE know you Did

  • 8/9/2019 Computer Proj

    65/74

    Program

  • 8/9/2019 Computer Proj

    66/74

    2stem.out.println(3n-nputting the elements in the Batri04 n3);

    for(int i!";i$m;i%%)

    {

    for(int j!";j$m;j%%)

    {

    2stem.out.print(3 nter the elements 4 3);

    H+i,+j,!-nteger.parse-nt(br.rea 5ine());

    ' '

    /* Printing the riginal Batri0 */

    2stem.out.println(3nThe riginal Batri0 is 4 3);

    for(int i!";i$m;i%%)

    {

    for(int j!";j$m;j%%)

    {

    2stem.out.print(H+i,+j,%39t3);

    '

    2stem.out.println();

    '

    /* @hec ing whether the matri0 is s2mmetric or not */

    int flag ! ";

    for(int i!";i$m;i%%)

    {

    for(int j!";j$m;j%%)

  • 8/9/2019 Computer Proj

    67/74

    {

    if(H+i,+j, 8! H+j,+i,)

    { flag ! #; // etting flag ! # when elements o not match

    brea ;

    ' ' '

    if(flag !! #)

    2stem.out.println(3nThe given Batri0 is :ot 2mmetric3);

    else

    2stem.out.println(3nThe given Batri0 is 2mmetric3);

    /* >in ing sum of the iagonals */

    int l ! "= r ! ";

    for(int i!";i$m;i%%)

    { for(int j!";j$m;j%%)

    { if(i !! j) // @on ition for the left iagonal

    {

    l ! l % H+i,+j,;

    '

    if((i%j) !! (m #)) // @on ition for the right iagonal

    {

    r ! r % H+i,+j,;

    ' ' '

    2stem.out.println(3The sum of the left iagonal ! 3%l );

    2stem.out.println(3The sum of the right iagonal ! 3%r );

  • 8/9/2019 Computer Proj

    68/74

    '

    else

    2stem.out.println(3The Batri0 i?e is ut f Range3);

    ''

    Output:

  • 8/9/2019 Computer Proj

    69/74

    Program @": n+eritance

  • 8/9/2019 Computer Proj

    70/74

    A super class Record has b een dened to store the names a nd ranks of 5 students. Dene a su bclass Rank to nd the highest rank a long with the name. The details o f both classes a re given below:

    Class nam e : Record

    Data Members / instance variables:name[ ] : to store the names o f studentsrnk[ ] : to store the ranks of students

    Member functions:Record() : co nstructor to initialize data membersvoid readvalues() : to st ore n ames a nd ranksvoid display() : displays t he names a nd the corresponding ranks

    Class name : Rank

    Data Members / instance variables:index : integer to store the index of the topmost rank

    Member functionsRank() : c onstructor to invoke the base class constructor and to initialize index to 0.void highest() : n ds t he index location of the topmost rank a nd stores it in index without sorting thearray 6void display() : d isplays t he name and ranks a long with the name having the topmost rank.

    Specify t he class Record g iving details of the constructor() , void readvalues() , void display() .Using the concept of inheritance, specify the class Rank g iving details of constructor() , voidhighest() and void display() .

    import java.io.*;

    class Recor //superclass

    {

    1uffere Rea er br!new 1uffere Rea er(new -nput treamRea er( 2stem.in));

    tring name+,;

    int rn +,;

    Recor ()

  • 8/9/2019 Computer Proj

    71/74

    {

    name ! new tring+ ,;

    rn ! new int+ ,;

    '

    voi rea values()throws - 0ception

    {

    2stem.out.println(3*** -nputting The :ames Hn Ran s ***3);

    for(int i!";i$ ";i%%)

    {

    2stem.out.print(3 nter name of stu ent 3%(i%#)%3 4 3); name+i,!br.rea 5ine();

    2stem.out.print(3 nter his ran 4 3);

    rn +i,!-nteger.parse-nt(br.rea 5ine());

    ' '

    voi ispla2()

    {

    2stem.out.println(3:amettRan 3);

    2stem.out.println(3 tt 3); //this is just for st2ling the output. Vou can s ip it 8

    for(int i!";i$ ";i%%)

    {

    2stem.out.println(name+i,%3tt3%rn +i,);

    ' ' ' //en of superclass Recor

    class Ran e0ten s Recor //subclass

    {

    int in e0;

  • 8/9/2019 Computer Proj

    72/74

    Ran ()

    {

    super(); //invo ing the constructor of superclass

    in e0 ! ";

    '

    voi highest()

    {

    int min ! rn +",;

    for(int i!";i$ ";i%%)

    {

    if(rn +i,$min)

    {

    in e0 ! i;

    ' ' '

    voi ispla2()

    {

    super. ispla2(); //calling the superclass function ispla2()

    2stem.out.println(3nTop most ran ! 3%rn +in e0,);

    2stem.out.println(3 tu ent with topmost ran ! 3%name+in e0,);

    '

    ' //en of subclass Ran

    /* -n 2our e0ams 2ou onKt nee to write the below given co e

    Le are writing it so as to familiari?e the stu ents on how to run programs

    base on the concept of inheritance.*/

    public class Nuestion##C- @ "## //@lass which will contain the main() metho an e0ecute the program

    {

  • 8/9/2019 Computer Proj

    73/74

    public static voi main( tring args+,)throws - 0ception

    {

    Ran ob!new Ran (); //creating object of subclass

    ob.rea values(); //calling ra values() function of superclass to input the names an ran s

    ob.highest(); //calling the function highest() for fin ing in e0 of topmost ran

    2stem.out.println(3*** utput ***3);

    ob. ispla2(); //calling ispla2() function of subclass

    '

    '

    Output:

    *** Inputting The Names A nd Ranks ***Enter name o f student 1 : AamirEnter his ran k : 5Enter name of student 2 : ZakirEnter his ran k : 2Enter name o f student 3 : SaalimEnter his ran k : 7Enter name o f student 4 : SamirEnter his ran k : 3Enter name o f student 5 : SaifEnter his ran k : 6*** O utput ***Name Rank Aamir 5Zakir 2Saalim 7Samir 3Saif 6

    Top most rank = 2Student with topmost rank = Zakir

  • 8/9/2019 Computer Proj

    74/74