You are on page 1of 30

Coding

import java.util.Scanner; class base { int x; int a; int sum=0; int y, sum1; int n, max; String p="", q="", x1, y1; int j=0; int pp=0, k; int r=0; voi input!" { Scanner sc=ne# Scanner!System.in"; System.out.print!"$ = ""; x1=sc.next%ine!"; c&eck!x1, x"; x=r; p'=x; System.out.print!"( = ""; r=0; y1=sc.next%ine!"; c&eck!y1, y"; y=r; System.out.println!"x = "'x'" y = "'y"; q'=y; ) voi c&eck!String c&ek, int ce&" { i*!!c&ek".equals!"a""++!c&ek".equals!",""" { r=10; ) else i*!!c&ek".equals!"*""++!c&ek".equals!"-""" { r=1.; ) else i*!!c&ek".equals!"b""++!c&ek".equals!"/""" { r=11; ) else i*!!c&ek".equals!"c""++!c&ek".equals!"0""" { r=11; ) else i*!!c&ek".equals!" ""++!c&ek".equals!"2""" { r=13; ) else i*!!c&ek".equals!"e""++!c&ek".equals!"4""" { r=15; ) else S#apnil 6oyal 9S:S $778S

r=7nteger.parse7nt!c&ek"; ) voi base1!" { a=x'1; int k=p.lengt&!"81; pp=0; int aa=p.lengt&!"81; *or!j=1; j;=10; j''" { *or!int i=0; i;p.lengt&!"; i''" { c&ar c=p.c&ar,t!i"; pp=0&aracter.get<umeric:alue!c"; sum'=!pp=>at&.po#!j, k""; pp=0; k88; ; i*!sum==n" { System.out.println!x1'"!base "'j'"" = "'y1'"!base "'max'""""; System.exit!0"; ) ) sum=0; k=p.lengt&!"81; ) ) voi base1!" { k=q.lengt&!"81; int sum1=0; int ar?@=ne# int?q.lengt&!"@; *or!int i=0; i;q.lengt&!"; i''" { c&ar c=q.c&ar,t!i"; ar?i@=0&aracter.get<umeric:alue!c"; ) max=ar?0@; *or!int i=0; i;q.lengt&!"; i''" { i*!ar?i@Amax" max=ar?i@; ) max'=1; *or!int i=0; i;q.lengt&!"; i''" { c&ar c1=q.c&ar,t!i"; pp=0&aracter.get<umeric:alue!c1"; sum1'=!pp=>at&.po#!max, k""; pp=0; k88; ) S#apnil 6oyal 9S:S $778S

n=sum1; ) public static voi main!" { base ob=ne# base!"; ob.input!"; ob.base1!"; ob.base1!"; ) )

S#apnil 6oyal 9S:S

$778S

Algorithm
1. >ake a public class BbaseB. 1. >ake global variables. 3. >ake a met&o to input t&e *irst number *rom t&e user an store it in variable Bx1B o* ata type string, an o t&e same *or t&e secon number an store it in variable By1B o* ata type string. 5. >ake met&o an pass t&e value o* string as parameter to convert t&e string number into t&e integer by c&ecking is t&e number is entere in &eax ecimal t&en it is to be converte into ecimal. .. >ake anot&e met&o to *in t&e base o* t&e secon number, *or oing t&is *irstly an array is ma e o* t&e siCe equal to t&e lengt& o* t&e string an t&en eac& c&aracter is extarcte an is store into an array. D. <o# *in t&e maximum number in t&at array an store t&at in variable BmaxB o* ata type integer. E. <o# run a loop till t&e lengt& o* t&e string enetere an in t&at multiply t&e c&aracters o* t&e igit by t&e BmaxB to t&e po#er o* a ress number an store t&em in a variable Bsum1B by a ing t&em, t&is *in s t&e base o* t&e number. F. >ake anot&er met&o to *in t&e base o* t&e *irst number, *or oing t&is run a loop till t#enty an insi e run anot&er loop till t&e lengt& o* t&e *irst number!string". <o# in t&is extract eac& c&aracter an multiply #it& eac& number o* 1 to 10 to t&e po#er a ress number o* t&e string an store t&ies sum in a variable BsumB, an c&eck *or #&ic& value *rom 1810 t&e sum comes out equal to Bsum1B, t&at number is require base o* t&e *iirst number.

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
x sum y

Data Type
7nteger 7nteger 7ntger

Scope
6lobal 6lobal 6lobal

Purpose
9o store t&e number in integer ata type. 9o store t&e base o* t&e secon number. 9o store t&e secon number in integer ata type. 9o store t&e base o* t&e *irst number. 9o store t&e base o* secon in or er to compare it #it& *irst. 9o *in t&e maximum number *rom t&e secon number. 9o store t&e *irst number. 9o store t&e secon number. 9o input t&e *irst nunber. 9o input t&e secon number. 9o c&eck t&e base o* number *rom 1 to 10.

sum1 n

7nteger 7ntger

6lobal 6lobal

max

7nteger

6lobal

p q x1 y1 j pp k r c&ek c&e k aa j i c ar?@

7nteger 7nteger String String 7nteger

6lobal 6lobal 6lobal 6lobal 6lobal

S#apnil 6oyal 9S:S

$778S

Coding
import java.util.Scanner; class revstr { static String Str; static int n; static String Gevstr=""; voi getstr!" { Scanner sc=ne# Scanner!System.in"; System.out.println!"4nter t&e String""; Str=sc.next%ine!"; n=Str.lengt&!"81; ) voi recGeverse!int a" { Gevstr'=Str.c&ar,t!n"; i*!nH=0" { n88; recGeverse!n"; ) ) public static voi main!" { revstr ob=ne# revstr!"; ob.getstr!"; ob.recGeverse!n"; System.out.println!Gevstr"; i*!!Gevstr".equals!Str"" { System.out.println!"9&is is palin rome""; ) else System.out.println!"9&is is not a palin rome""; ) )

S#apnil 6oyal 9S:S

$778S

Input and Output

S#apnil 6oyal 9S:S

$778S

Algorithm
1. >ake a publc class BrevstrB. 1. >ake t#o global variables BStrB an BGevstrB o* ata type String to input t&e string an to store t&e reverse o* t&e string. 3. >ake a met&o BgetstrB to input t&e string *rom t&e user. 5. ,n store t&e lengt& o* t&e string an subtract it by 1 an store it in variable BnB o* ata type integer. .. >ake anot&er met&o BrecreverseB to *in t&e reverse o* t&e string an pass t&e lengt& o* t&e string in t&at parameter. D. <o# extract eac& c&aacter o* t&e string *rom backsi e an store it in recreverse!n" an t&en ecrease t&e lengt& o* t&e string t&at is variable BnB by 1 an t&en recall t&e met&o upto #&en n is not 0. E. make main met&o call t&e *unctions an c&eck #&et&er t&e t&e revrese string is equal to t&e original an i* it is so t&an print t&e string.

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
Str Gevstr n

Data Type
String String 7nteger

Scope
6lobal 6lobal 6lobal

Purpose
9o store t&e entere string 9o store t&e reverse string. 9o store t&e lengt& o* t&e string.

S#apnil 6oyal 9S:S

$778S

Coding
import java.util.Scanner; class reverseI#or { static voi main!" { Scanner sc=ne# Scanner!System.in"; System.out.println!"4nter t&e Sentence""; String s=sc.next%ine!"; String t=""; System.out.println!"Geverse sentence is""; *or!int i= s.lengt&!"81; iA=0; i88" { t'=s.c&ar,t!i"; i*!s.c&ar,t!i"==B B++i==0" { i*!i==0" System.out.print!" ""; *or!int j=t.lengt&!"81; jA=0; j88" { System.out.print!t.c&ar,t!j""; ) t=""; ) ) ) ) Input and Output

S#apnil 6oyal 9S:S

$778S

Algorithm
1. 1. 3. 5. >ake a public class BreverseI#or B. 7nput t&e sentence *rom t&e user. Gun a loop *rom t&e lengt& o* t&e string till 0 extract eac& c&aracter o* t&e string an store t&e c&aracter in ne# string an i* t&e c&aracter is spae!B B" t&e run anot&er reverse loop in t&at an *rom t&e lengt& o* t&e ne# string till 0 .. extract t&e c&aracter an print it an t&en set ne# string to null .

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
s t i j

Data Type
String String 7nteger 7nteger

Scope
%ocal to main!" %ocal to main!" %ocal to main!" %ocal to main!"

Purpose
9o store t&e entere string 9o store t&e #or 9o extract eac& c&aracter o* t&e sentence 9o extract eac& c&aracter o* t&e #or .

S#apnil 6oyal 9S:S

$778S

Coding
import java.util.Scanner; class minImax { static voi main!" { Scanner sc=ne# Scanner!System.in"; System.out.println!"4nter t&e number o* elements to be entere ""; int l=sc.next7nt!"; System.out.println!"4nter t&e elements""; System.out.println!"; int ar?@=ne# int?l@; *or!int i=0; i;l; i''" { System.out.println!"4nter t&e "'!i'1"'" element""; ar?i@=sc.next7nt!"; ) int max=ar?0@; int min=ar?0@; *or!int i=0; i;l; i''" { i*!ar?i@Amax" max=ar?i@; i*!ar?i@;min" min=ar?i@; ) System.out.println!"4lement #it& maximum value = "'max"; System.out.println!"4lement #it& minimum value = "'min"; S#apnil 6oyal $778S 9S:S

) )

Input and Output

Agorithm
1.7nput t&e limit o* t&e elements *rom t&e user 1.store t&e limit in t&e variable BlB o* ata type integer. 3.>ake an integer array o* lengt& l. 5.Gun a loop an input all t&e elements *rom t&e user. ..>ake an integer max an store t&e value o* *irst element o* t&e array an o same #it& anot&er variable min o* ata type integer. D.Gun a loop an c&eck i* any element o* t&e array is greater t&an t&e BmaxB t&e store t&e value o* t&at element in max. E.0&eck i* any element o* t&e array is smaller t&an t&e BminB t&an store t&at value o* array in BminB.

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
l ar?@ min max i

Data Type
7nteger 7nteger 7nteger 7nteger 7nteger

Scope
%ocal to main!" %ocal to main!" %ocal to main!" %ocal to main!" %ocal to main!"

Purpose
9o store t&e limit o* t&e elements. 9o store t&e elements. 9o *in t&e value o* minimum element 9o *in t&e maximum element 9o c&eck t&e elements

S#apnil 6oyal 9S:S

$778S

Algorithm
1. >ake a public class BconsecutiveB. 1. 7nput t&e number *rom t&e user an store it in variable BnB o* ata type integer. 3. Gun a loop till t&e !nJ1"'1 an set sum to 0, run anot&er loop *rom t&e number o* *irst loop till !nJ1"'1 an a t&e a t&e igit o* number an store t&em in sum. 5. 0&eck i* t&e sum a s up to t&e number entere . .. Gun anot&er loop *rom t&e number o* *irst loop till t&e number o* secon loop an print t&e value o* t&ir loop.

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
i s l a j k

Data Type
7ntger 7ntger 7ntger 7ntger 7ntger 7ntger

Scope
%ocal to input!" %ocal to input!" %ocal to input!" %ocal to input!" %ocal to input!" %ocal to input!"

Purpose
9o count t&e number *rom 1 to nJ1'1 9o store t&e sum o* t&e igits. 9o count t&e igits upto nJ1'1 9o input t&e number. 9o a t&e number *rom i to t&e l

9o print t&e consecutive number

S#apnil 6oyal 9S:S

$778S

Algorithm
1. 1. 3. 5. .. D. E. >ake a public class Bcc&angeB. >ake global variables BnB an B&exB. >ake a constructor to initialiCe t&e variable. >ake a met&o to input t&e number *rom t&e user by passing a variable in t&e parameter. >ake anot&er met&o to calculate t&e &exa ecimal equivalent number. 7n t&is met&o run a loop till t&e entere number is greater t&an 0. extract t&e number by taking taking its remain er by ivi ing by 10, an store it in variable num an c&eck i* t&is number is graeyer t&an K an i* yes t&en c&eck t&e con ition i* t&is is equal to 10 t&en &exa ecimal is a an i* it is 11 t&en &ex number is b an so on. Store t&e numbers in variable s o* ata type string. F. Store t&e lengt& o* t&e variable s in a variable BlenB o* ata type integer. K. Gun a loop till t&e lengt& o* t&e string BsB. extract eac& c&aracter o* t&e string an print it. 10. >ake anot&er met&o in or er to convert t&e &exa ecimal number to ecimal number. 11. 7n t&is met&o run a loop till t&e lengt& o* t&e converte &exa ecimal number. 4xtract it c&arcater an convert t&e alp&abets o* t&e &exa ecimal number to ecimal. 11. Lt&er #ise multiply t&e extracte igit to t&e 1D raise to t&e po#er o* string a ress. 13. ,n print t&e converte number..

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
n &ex a num s len

Data Type
7nteger String 7nteger 7nteger String 7nteger

Scope
6lobal 6lobal %ocal to input!int a" %ocal to &exa eci!" %ocal to &exa eci!" %ocal to eci&exa!"

Purpose
9o store t&e inputte number. 9o store t&e converte &exa ecimal number. 9o input t&e number *rom t&e user. 9o extract eac& igit o* t&e number. 9o convert t&e igit to &exa ecimal alp&abet. 9o store t&e lengt& o* t&e convert &exa ecimal number. 9o extract eac& c&aracter o* t&e &exa ecimal number. 9o store t&e converte ecimal number. 9o count t&e number

c&

0&aracter

%ocal to eci&exa!"

ec i S#apnil 6oyal 9S:S

2ouble 7nteger

%ocal to eci&exa!" %ocal to eci&exa!" $778S

*rom to lengt& o* t&e &exa ecimal number.

Coding
import java.util.Scanner; class Mi*act { int n1,n1,&c*,lcm; public static voi main!" { Mi*act ob = ne# Mi*act!"; ob.get2ata!"; ob.c&ange!"; ob.result!"; ) Mi*act!" { n1=0; n1=0; ) voi c&ange!" { i*!n1An1" { int temp=n1; n1=n1; n1=temp; ) S#apnil 6oyal 9S:S

$778S

) public voi get2ata!" { Scanner sc=ne# Scanner!System.in"; System.out.print!"4nter 1st number N ""; n1 = sc.next7nt!"; System.out.print!"4nter 1n number N ""; n1 = sc.next7nt!"; ) public int rec&e*!int a, int b" { i*!a==0" return b; else return rec&e*!bOa,a"; ) public int *nIlcm!int a, int b, int &c*" { return !a=b"J&c*; ) public voi result!" { &c* = rec&e*!n1,n1"; lcm = *nIlcm!n1,n1,&c*"; System.out.println!"PnM0- = " '&c*"; System.out.println!"%0> = " 'lcm"; ) )

Input and Output

S#apnil 6oyal 9S:S

$778S

Algorithm
1. 1. 3. 5. .. >ake a public class BMi*actB. 2eclare t#o global variables Bn1B an Bn1B o* ata type integer. >ake a constructor to initialiCe t&e variables to 0. make a met&o to input t&e number *rom t&e user. >ake anot&er met&o to c&eck i* t&e *irst number is greater t&an t&e secon one an i* yes t&en s#ap t&eir values. D. >ake anot&er met&o in or er to calculate t&e &c* o* t&e t#o numbers. E. 7n t&is c&eck i* t&e value o* a is 0 t&en return b ot&er #ise return t&e met&o by mobulus a by b an b by passing t&ese t#o in t&ier parameter. F. >ake anot&er met&o to calculate t&e lcm K. in t&is return t&e pro uct o* t#o numbers ivi e by t&ier &c*. 10. >ake main met&o an in t&at store t&e value o* &c* an lcm in t&ies respective variables.

S#apnil 6oyal 9S:S

$778S

Variable Discription Table


Variable
n1 n1 temp

Data Type
7nteger 7nteger 7nteger

Scope
6lobal 6lobal %ocal to c&ange!"

Purpose
9o store t&e inputte *irst number. 9o store t&e inputte secon number. 9o s#ap t&e value o* a an b i* a is smaller t&an b. 9o pass t&e numbers as parameter 9o pass t&e numbers as parameter 9o store t&e &c* o* t#o numbers. 9o store t&e lcm o* t#o numbers. 9o pass t&e value as parameter

a b &c* lcm a

7nteger 7nteger 7nteger 7nteger 7nteger

%ocal to rec&e*!" %ocal to rec&e*!" 6lobal 6lobal %ocal to *nIlcm!"

S#apnil 6oyal 9S:S

$778S

7nteger

%ocal to *nIlcm!"

9o pass t&e value as parameter

Coding
import java.util.Scanner; class 0ollection { int ar?@; int l; int *lag; 0ollection!" JJ e*ault constructor { l=0; *lag=0; ) 0ollection!int a" { l=a; *lag=0; ar=ne# int?l@; JJ intialiCe t&e array #it& t&e inputte siCe ) voi inparr!" { Scanner in=ne# Scanner!System.in"; System.out.println!"4nter t&e array elementsN ""; *or!int i=0;i;l;i''" S#apnil 6oyal 9S:S

$778S

{ ar?i@=in.next7nt!"; JJ inputs t&e numbers ans store it in t&e array ) ) 0ollection common!0ollection 0" JJtakes in an object o* t&e class as a parameter { 0ollection Gesult=ne# 0ollection!ar.lengt&"; int j=0, in ex=0; *or!int i=0;i;ar.lengt&;i''" { *or!j=0;j;0.ar.lengt&;j''" { i*!0.ar?j@==0" *lag=1; i*!0.ar?j@==ar?i@" JJ true i* a common elemnt is *oun break; ) i*!jH=0.ar.lengt&" { Gesult.ar?in ex@=0.ar?j@; JJassigns t&e common element its place in ex''; ) ) return Gesult; JJ returns an object o* t&e class t&at contains t&e array o* common elements ) public voi arrange!" { int temp=0; *or!int j=0;j;ar.lengt&;j''" { *or!int k=0;k;ar.lengt&8j81;k''" { i*!ar?k@Aar?k'1@" JJc&ecks i* t&e present elemnt is bigger t&an t&e next elemnt { temp=ar?k@; ar?k@=ar?k'1@; JJs#aping ar?k'1@=temp; ) ) ) ) public voi isplay!" { System.out.println!"9&e sorte list o* common elements is as *ollo#sN ""; *or!int i=0;i;ar.lengt&;i''" { i*!ar?i@==0 QQ *lag==0" S#apnil 6oyal 9S:S $778S

continue; System.out.print!ar?i@'" ""; ) ) public static voi main!" { Scanner sc=ne# Scanner!System.in"; System.out.println!"4nter t&e limit o* array""; int r = sc.next7nt!"; 0ollection ob =ne# 0ollection!r"; ob.inparr!"; 0ollection ob1=ne# 0ollection!";import java.util.Scanner; class 0ollection { int ar?@; int l; int *lag; 0ollection!" JJ e*ault constructor { l=0; *lag=0; ) 0ollection!int a" { l=a; *lag=0; ar=ne# int?l@; JJ intialiCe t&e array #it& t&e inputte siCe ) voi inparr!" { Scanner in=ne# Scanner!System.in"; System.out.println!"4nter t&e array elementsN ""; *or!int i=0;i;l;i''" { ar?i@=in.next7nt!"; JJ inputs t&e numbers ans store it in t&e array ) ) 0ollection common!0ollection 0" JJtakes in an object o* t&e class as a parameter { 0ollection Gesult=ne# 0ollection!ar.lengt&"; int j=0, in ex=0; *or!int i=0;i;ar.lengt&;i''" { *or!j=0;j;0.ar.lengt&;j''" { i*!0.ar?j@==0" *lag=1; i*!0.ar?j@==ar?i@" JJ true i* a common elemnt is *oun break; ) i*!jH=0.ar.lengt&" S#apnil 6oyal 9S:S $778S

{ Gesult.ar?in ex@=0.ar?j@; JJassigns t&e common element its place in ex''; ) ) return Gesult; JJ returns an object o* t&e class t&at contains t&e array o* common elements ) public voi arrange!" { int temp=0; *or!int j=0;j;ar.lengt&;j''" { *or!int k=0;k;ar.lengt&8j81;k''" { i*!ar?k@Aar?k'1@" JJc&ecks i* t&e present elemnt is bigger t&an t&e next elemnt { temp=ar?k@; ar?k@=ar?k'1@; JJs#aping ar?k'1@=temp; ) ) ) ) public voi isplay!" { System.out.println!"9&e sorte list o* common elements is as *ollo#sN ""; *or!int i=0;i;ar.lengt&;i''" { i*!ar?i@==0 QQ *lag==0" continue; System.out.print!ar?i@'" ""; ) ) public static voi main!" { Scanner sc=ne# Scanner!System.in"; System.out.println!"4nter t&e limit o* array""; int r = sc.next7nt!"; 0ollection ob =ne# 0ollection!r"; ob.inparr!"; 0ollection ob1=ne# 0ollection!"; ob.arrange!"; ob. isplay!"; ) )

S#apnil 6oyal 9S:S

$778S

ob.arrange!"; ob. isplay!"; ) )

Input and Output

Algorithm
1. Start. 1. Make three instance variables: two integer type, say len and flag, and one integer array. 3. Make the default constructor which assigns len and flag the value zero. 5. Also ake a para eterized constructor that takes in an integer and assigns ethod assign the size of the array as len.

len that value. !n this very .. Make a

ethod to input array ele ents:

D. Ask the user to provide the ele ents of the array. E. "ill the F. Make a S#apnil 6oyal 9S:S in the array. ethod to detect the co on ele ents: $778S

K. As a para eter take in an ob#ect of this class. 10. $reate another ob#ect of the class. 11. $heck whether the ele ent of the presently running ob#ect is present in the ob#ect passed as a para eter. 11. !f it is found then put that ele ent in the newly created ob#ect%s array. 13. &epeat the above two steps for all the ele ents of the present ob#ect%s array. 15. !f any of the co 1.. Make a on ele ents is ' set flag to (.

ethod to sort the array ele ents in the ascending order:

1D. "or each ele ent of the array check if it is greater than any of the ele ents on the array. 1E. !f true then swap the present ele ent with the one that is s aller than it. 1F. Make a ethod to display the ele ents of the array:

1K. $heck if flag is zero. 10. !f yes, then proceed to print the ele ent of the array. 11. )nd.

Variable Description Table


Name of Variable ar Data Type int*+ Scope ,lobal variable to the class ,lobal variable to the class ,lobal variable to $778S Responsibility Array to store the nu bers. -o store the size of the array. $hecking for zero.

int

flag S#apnil 6oyal 9S:S

int

the class size int .ara eterized constructor $ollection/int size0 inparr/0 ethod Assigning the value of len.

int

-o get the input fro the user and assign it to the array -o fill in the co on ele ents in the array at their inde1es. $ontrolling the for loop and checking if there is a co on ele ent present. Swapping the ele ents .

inde1

int

2ocal to $ollection co on/$ollection $0 ethod

int

2ocal to $ollection co on/$ollection $0 ethod

te p

int

2ocal to void arrange/0 ethod

S#apnil 6oyal 9S:S

$778S

You might also like