Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

DI GI Talassi Gnment SRI Harshi Thadeepala 19BCE7406 Slot-G1+TG1

1. The program calculates the cost of a long-distance call based on rates that vary by day of week and time of day. It takes input for the call details and outputs the cost. It allows the user to repeat the calculation. 2. The program accepts a 4-digit year as input and outputs the corresponding year in Roman numerals. It allows the user to repeat the calculation for different years between 1000 and 3000. 3. The program calculates monthly loan payments and balances over the life of a loan given the loan amount and interest rate as input. It also outputs the total interest paid and annualized interest percentage. It allows the user to repeat the calculation for different loans.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

DI GI Talassi Gnment SRI Harshi Thadeepala 19BCE7406 Slot-G1+TG1

1. The program calculates the cost of a long-distance call based on rates that vary by day of week and time of day. It takes input for the call details and outputs the cost. It allows the user to repeat the calculation. 2. The program accepts a 4-digit year as input and outputs the corresponding year in Roman numerals. It allows the user to repeat the calculation for different years between 1000 and 3000. 3. The program calculates monthly loan payments and balances over the life of a loan given the loan amount and interest rate as input. It also outputs the total interest paid and annualized interest percentage. It allows the user to repeat the calculation for different loans.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DI

GITALASSIGNMENT
SRI
HARSHITHADEEPALA
19BCE7406
SLOT-G1+TG1
1.Wr i
teapr ogram t hatcomput esthecostofal ong-distancecal l
.Thecostoft hecal li
sdet ermined
accor dingtot hef ollowingr ateschedul e:
a.Anycal lstartedbet ween8: 00am and6: 00pm, Monday
throughFr i
day ,i
sbi ll
edatar ateof$0. 40permi nute.b.Anycal lstartingbef or e8:
00am oraf t
er6:00
pm, Mondayt hroughFr i
day ,i
schar gedatar ateof$0. 25permi nut e.c.Anycal lstartedonaSat urday
orSundayi schar gedatar ateof$0. 15permi nute.Thei nputwi llconsi stoft hedayoft heweek, t
he
ti
met hecal lstarted, andthel engthoft hecalli
nmi nut es.Theout putwi llbet hecostoft hecall
.The
ti
mei st obei nputi n24-hournot at
ion,sothet i
me1: 30pm i sinputas13: 30Thedayoft heweekwi ll
ber eadasoneoft hef oll
owi ngpairsofchar actervalues,whi char est oredint wov ari
ablesoft ype
char :
MoTuWeThFrSaSuBesur et oall
owt heusert ouseei therupper caseorl ower casel et
tersora
combi nati
onoft het wo.Thenumberofmi nuteswi l
lbei nputasav alueoft ypei nt.(Youcanassume
thattheuserr oundst heinputt oawhol enumberofmi nutes.)Yourpr ogram shoul di ncl
udeal oop
thatlet stheuserr epeatthiscal cul
ationunti
l t
heusersay ssheorhei sdone.

2.Wr i
teaprogr am thatacceptsay earwri
ttenasafour-
digitAr
abic(or
dinar
y)numeral andout puts
theyearwr i
tteninRomannumer als.ImportantRomannumer al
sareVf or5,Xfor10,Lf or50,Cf or
100,Df or500, andM for1,000.Recallt
hatsomenumber sarefor
medbyusi ngakindofsubt raction
ofoneRoman“ digit
”;f
orexample,IVis4pr oducedasVmi nusI,XLis40,CM i
s900,andsoon.Af ew
sampley ears:MCM i s1900, MCMLi s1950, MCMLXi s1960,MCMXLi s1940,MCMLXXXI Xis1989.
Assumet hey eari sbetween1000and3000.Yourpr ogr
am shouldincl
udealoopthatletstheuser
repeatthi
scal culati
onuntil
theusersay ssheorheisdone.

3.Interestonal oani spai donadecl ini


ngbal ance, andhenceal oanwi thani nterestr at
eof ,say,14
percentcancostsi gnificant l
ylesst han14per centoft hebal ance.Wr iteapr ogr am thatt akesal oan
amountandi nterestr ateasi nputandt henout put sthemont hlypay ment sandbal anceoft hel oan
untiltheloanispaidof f.Assumet hatthemont hlypay ment sar eonet went iethoft heor i
ginal l
oan
amount ,
andt hatanyamounti nexcessoft hei nterestiscreditedt owar ddecr easingt hebal ancedue.
Thus, onal oanof$20, 000, thepay ment swoul dbe$1, 000amont h.I fthei nter estrateis10per cent,
theneachmont hthei nter estisone- t
wel fthof10per centoft her emai ningbal ance.Thef ir
stmont h,
(10per centof$20, 000) /12, or$166. 67,woul dbepai di ninterest,andt her emai ni
ng$833. 33woul d
decreaset hebalancet o$19, 166.67.Thef oll
owi ngmont hthei nterestwoul dbe( 10per centof
$19,166.67)/12,andsof or t
h.Al sohav ethepr ogr am outputthet otal interestpai dovert hel i
feoft he
l
oan.Fi nall
y,determinewhatsi mpleannual i
zedper centageoft heor i
gi nalloan
balancewaspai dini nter est.Forexampl e,if$1, 000waspai di ninterestona$10, 000l oanandi ttook
2y earstopayof f
,thent heannual i
zedint eresti s$500, whichi s5per centoft he$10, 000l oanamount .
Yourpr ogram shoul dal l
owt heusert orepeatt hiscalculati
onasof tenasdesi red.

4.Alit
eris0.
264179gallons.Wri
teapr ogram t
hatwi l
lreadinthenumberofli
tersofgasoline
consumedbyt heuser’
scarandt henumberofmi lestravel
ledbythecarandwillthenoutputthe
numberofmi l
espergall
ont hecardeliv
ered.Yourprogram shouldal
lowtheusertorepeatt hi
s
cal
culati
onasoftenastheuserwi shes.Defi
neaf uncti
on/methodtocomputet henumberofmi les
pergall
on.Yourprogr
am shoulduseagl oball
ydefinedconstantfort
henumberofl it
erspergallon.

5.Wr i
teapr ogram togauget herateofi nfl
ationforthepasty ear.Theprogram asksforthepriceof
anitem (suchasahotdogora1- caratdiamond)bot honey earagoandt oday.Itest
imatesthe
i
nflati
onr ateast hedi
ff
er enceinpricedividedbyt hey ear-
agopr i
ce.Yourprogram shoul
dal l
owt he
usertor epeatt hiscal
culationasof t
enast heuserwi shes.Defineafuncti
on/met hodtocomput ethe
rat
eofi nflation.Theinf
lat i
onrateshouldbeav al
ueoft ypedoubl egi
vingtherateasaper cent,f
or
exampl e5. 3f or5.3per
cent .

6.Wr i
teaprogr am tostoreapolynomialexpressioninanar rayanddi spl
ayi t
.Taket henumberof
ter
msandcoef fi
cient&exponentofeacht er
m( star
tingf r
om highestexponentt olowest )fr
om user.
Storecoeffi
cientandexponentinconsecut i
vel ocati
onsi narray
.Eg.Expr ession5x3-2x2+x-5should
bestoredasi narraywher eval
uesatev enpositions(0,2,4.
..
.)ar
ecoef fi
cientsandv aluesinlocati
ons
tothei
rright(1,3,
.
..)ar
eexponents.SampleInput :Enterthenumberoft er
ms: 3Entercoef fi
cient1:5
Enterexponent1: 3Ent ercoef
fi
cient2: -
2Ent erexponent2: 2Entercoef f
ici
ent3: 5Ent erexponent
3: 0Hencet heExpr essi
onis5x3-2x2+5 (as5x0=5)
7.Writ
eapr ogram tocal
culatetheaveragesalar
yf ordi
ff
erentagegroups25- 29,30-34andmor e
thanorequalto35anddi splayit
.Ini
ti
ali
zethearraywithfol
lowingdata,whereindexr eferst
othei
dof
theperson.Hint:Lett
heagegr oupsbe25- 29,30-34and>=35ageSal ary
2515000
3015850
2616750
3517850
3114200
3316150
3517650
3414100
2714500
4517800
Output
: Avgforagegroup(25-29)is:15416.666666667Av gforagegr oup(30-34)is: 15075Avgfor
agegroup(=35)i s:17766.666666667

8.Findoutt heAmi cabl eNumber swi t


hinar ange.Innumbert heory ,
apai rofami cablenumber s
consist
soft wodi fferentnumber ssucht hatthesum oft hefactorsofeachi sequal tot heot her
number .Forexampl e,220and284i sapai rofamicablenumber s: Thefact or
sof220ar e1, 2,4, 5,10,
11,20,22, 44, 55and110, ofwhi chthesum i s284.Andt hefactorsof284ar e1, 2,4, 71and142, of
whichthesum i s220.( Note:Asshowni ntheexampl e,weconsideronl ythepr operf actors,whi chdo
notincl
udet henumberi t
self.
)Givenar ange[lower,upper]
,wecountapai rofami cabl enumber sasa
ful
lpairifbot hnumber sint hepairarewithintherange.Incontrast,wecountapai rofami cabl e
number sasahal fpairifonlyoneoft henumber sint hepairi
swi thinther ange.Forexampl e, i
fthe
rangegiveni s[ 100,300] ,
220and284i scount edasaf ullpai
rbecausebot hnumber sar ewi thint he
range.Incont rast,iftherangegiv enis[250,300],t
hesamepai riscount edasahal fpai r,
sinceonl y
284i swithinther ange.

9.Wr i
teaprogr
am t oarrangetheelementsofanar rayi
nsuchawayt hattheevenelement sar
e
placedatevenindexandoddel ementsareplacedatoddi ndex.Ifoverf
lowhappens( i
.e.numberof
even/oddelement saremor et
hant heavai
lablerespect
iveindices)
,ignoretheel
ement sanddispl
ay
theupdatedarr
ay .Incontr
ast,i
ftheindi
cesar eempt yt
henst orezerointhoselocat
ions.Itmaybe
notedthatzer
othi ndexisconsider
edasev en.

10.Wr
it
eapr
ogr
am t
odi
spl
ayt
hef
oll
owi
ngpy
rami
dst
ruct
ureusi
ngmet
hod.UseNoRet
urnar
gument

11.Aper fectnumberi saposi t


iveintegerthatisthesum ofi t
spr operposit
ivedivisor
s.Apr oper
posit
ivedivisorofanumberi saposit i
veintegersmal lerthanthenumberanddi vi
desthenumber .For
example,theposi ti
vedi vi
sorsof20ar e1, 2,4,5,10and20; all
except20ar epr operposi t
ivediv i
sor s
of20.Forexampl e,6i saper f
ectnumberbecause6=1+2+3, but
8and100ar enot,because8≠1+2+4, and100≠1+2+4+5+10+20+25+50.Wr it
eapr ogram
thatasksuserr epeat edl
yforanonnegat iveinteger,
andst opswhent henumberi szero.Foreach
posit
iveintegerentered,yourpr ogram ist ocheckwhet heriti
saper fectnumberornot .Yourpr ogr am
shouldhav eaf uncti
on/ methodnamei sPerfect(
)thatt akethenumberandr eturns1ift hear gument
i
saper fectnumber ,or0ot herwise.Usepassbyaddr essoraddr esswhi cheverissuit
abl e

12.Thef
ormul
aforcomput
ingt
henumberofway
sofchoosi
ng‘
r’
dif
fer
entt
hingsf
rom asetof‘
n’
thi
ngsisthedef i
neasC( n,r
)=n!/
(r!*(n–r )
!).Writ
eamet hod,st
ati
cdoublecom(intn,i
ntr)tofi
nd
theval
ueoft heC(n, r
).Thi
smethodfurtheri
nv okesarecursi
vemethod,
stati
cintfac(
intn).Her
e,
methodstatici
ntf ac(
intn)r
ecur
sivel
yfindsfactori
alofanumberpassedasanar gument .Wri
tethe
completeprogram.

13.Fi
ndTr
ansposeofanar
rayusi
ngmet
hod.

14.Jav
aPr
ogr
am t
oSor
ttheAr
rayi
nanAscendi
ngOr
derandDescendi
ngOr
derusi
ngmet
hod.

15.Sear
chi
ngofanel
ementi
nar
rayusi
ngmet
hod.

1.
i
mpor tjav a.
ut i
l.
* ;
publi
ccl assMai n{
 
 publi
cst aticv oidmai n( Stri
ngar gs[]
){
 
  
 Scannersc=newScanner (Sy
stem. in)
;
 
 
 
 System. out.println("
Ent erthemi nute")
;
 
  
 i
ntmi n=sc. next Int(
);
 
  
 System. out.println("
Ent erthelengthoft hecall
")
;
 
  
 i
ntlen=sc. next Int()
;
 
  
 System. out.println("
Ent ertheDay ");
 
  
 Stri
ngday =sc. next()
;
 
  
 doubler ate=0;
 
  
 i
f(min>=800&&mi n<=1800){
 
  
 i
f(day.equal s("Mn" )|
|day .
equals("Tu")
||
day.equal
s("
We"
)|
|day
.equal
s("
Th"
)|
|day
.equal
s("
Fr"
)){
 
  
  
 
 
  
  
 System. out .
pr i
ntln("Rateis"+len*0.4);
 
  
 }
 
  
 
 
  
 
 
  
 
 
  
 }
 
  
 el
se{
 
  
  
 i
f(day.equals("
Mn")||
day.equal s(
"Tu")|
|day.
equal
s("
We"
)|
|day
.equal
s("
Th"
)|
|day
.equal
s("
Fr"
)){
 
  
  
  
 
 
  
  
  
 System. out.
pri
ntl
n("Rateis"+len*0.25);
 
  
  
 }
 
  
  
 }
 
  
 
 
  
 i
f(day=="Sa"||
day=="Su"){
 
  
  
 
 
  
  
 System. out.
pri
ntl
n("Rateis"+len*0.15);
 
  
  
 
 
  
 }
 
  
 
 
  
 
 
 }
 
 
 
  
 
 
  
 
 
  
 
 
 }

OUTPUT:

2.
i
mpor
tjav
a.ut
il
.Scanner
;

publi
cclassMain
{
publi
cstati
cvoi
dmai
n(St
ri
ng[]ar
gs)
{
Scanneri
n=newScanner
(Syst
em.i
n);

charchoice=' y'
;
whil
e( choice=='y'
)
{
i
ntyr=i n.next
Int
();
i
ntlen=( i
nt)Math.
log10(yr
)+1;
i
ntyAr r
ay [
]=newi nt[
len]
;
f
or(
inti
=len-
1;i
>=0;
i-
-){y
Arr
ay[
i]=y
r%10;
yr/
=10;
}

i
ntmul t=1000;
Str
ingr es=" ";
for
(inti :yArr
ay)
{
i
f(mul t==1000)f or(i
ntk=0;k<i;k++)res+="M";
el
sei f(mult==100)
{
if
(i ==4)res+=" CD";
elseif(i==9)res+=" CM";
elseif(i<5){for(i
ntk=0; k<i;
k++)res+="C";}
else{r es+="D";for(i
ntk=0;k<i-5;
k++)res+="C";}
}
el
sei f(mult==10)
{
if
(i ==4)res+=" XL";
elseif(i==9)res+=" XC";
elseif(i<5){for(i
ntk=0; k<i;
k++)res+="X";}
else{r es+="L";for(
intk=0;k<i-
5;k++)res+="X";}
}
el
se
{
if
(i ==4)res+=" I
V" ;
elseif(i==9)res+=" IX"
;
elseif(i<5){for(i
ntk=0; k<i;
k++)res+="I"
;}
else{r es+="V";for(i
ntk=0;k<i-5;
k++)res+="I"
;}
}

mul
t/=10;
}

Sy
stem.
out
.pr
int
ln(
res)
;

Syst
em.out
.pr
int
("Doy
ouwantt
otr
yagai
n?(
y/n):
");
choi
ce=in.
next(
).
char
At(
0);
}
}
}

OUTPUT:

3.
i
mpor
tjav
a.ut
il
.Scanner
;
publi
cclassMai n
{
publi
cst at
icv oidmai n(Str
ing[]args)
{doubl ebal ance=0;
doublemont hlypayasinter
est=0;
Scannerob=newScanner (System.in)
;
i
ntpr i
ncipal ,i
ntrat,hmuwi ll
pay permonth,
noof
ye;
//t
aki
ngi
nputofpr
inci
pal
amountofl
oan,
i
nterestrate&numberofy ears
pri
ncipal=ob. next I
nt()
;
i
ntrat=ob. nextInt()
;
hmuwi ll
pay permont h=ob. nextInt(
);
noofye=ob. nextInt
();

f
or(i
nti=1; i
<=( noofye*12);
i++)
{
monthl
ypayasinter
est=(principal*(intr
at/100)
)/12;
doubl
emont hl
ypay=hmuwi l
lpayper month-mont hl
ypayasi
nterest;
bal
ance=pr i
ncipal-monthlypay ;
monthl
ypayasi
nterest=monthlypay asi
nterest+monthly
pay asi
nt er
est
;
}
Syst
em.out.
pri
ntln(
"BALANCEI S:"+balance);
Syst
em.out.
pri
ntln(
"MONTHLYPAYMENTASI NTEREST:"+mont hly
payasi
nter
est
);
}
}

OUTPUT:

4.
i
mpor tjava.util
.Scanner;
cl
assMai n{
staticdoubl eCal culat
ion(
){
Scanneroc=newScanner (
Sy st
em.in)
;
Sy stem. out.pri
ntl
n("Ent
ertheno.ofli
tr
esofGasoli
neconsumed:
")
;
intL=oc. next I
nt()
;
Sy stem. out.pri
ntl
n("Ent
ertheno.ofmilest
rav
ell
ed:"
);
intM=oc. next I
nt(
);
doubl eg=0. 264179;
doubl eMPG=M/ (
L*g);
returnMPG;
}
publ i
cst aticvoidmai n(St
ri
ng[]
args){
Scanneroc=newScanner (Sy
stem.in)
;
doubleans=Cal culati
on();
System.out.
pri
nt l
n( "
No.ofmilespergall
onis"
+ans);
System.out.
pri
nt l
n( "
Doy ouwantt ocont
inue?
" )
;
charch=oc.next().
char At(
0);
do{
doublet=Calculation()
;
System.out.
pr i
ntln("
No.ofmilespergall
onis"+t
);
}
whil
e(ch=='y'
&&ch==' Y');
}
}

OUTPUT:

5.
i
mpor tjav a.util
.Scanner ;
publi
ccl assMai n{
publicst aticdoubl ei nfl
ationRat e(doublepr eyrcost,
doublecurcost)
{
return((curcost -prey r
cost )
/ curcost);
}
publicst aticvoi dmai n(String[ ]
args){
doubl epr ey rcost,curcost;
Scannerscan=newScanner (
Sy st
em. i
n);
Sy stem. out.pr i
ntln("Thepr ev i
ousy earcostofpr oductis:
")
;
preyrcost =scan. next Doubl e();
System. out.printl
n( "
Thecur rectcostofpr oductis:
")
;
curcost =scan. next Doubl e();
System. out.printl
n( i
nfl
at i
onRat e(preyrcost,curcost
)*100+"per
cent
");

}
}
OUTPUT:
6.
i
mpor tjav a.
io.* ;
i
mpor tjav a.
ut il
.*;
publ
iccl assMai n{
publicst aticv oi
dmai n(Str
ingargs[]
){
Scannerob=newScanner (
System.in)
;
System. out .
print
("Enterthenumberoft erms:
");
i
ntn=ob. next Int(
);
i
nta[ ]=newi nt[n]
;
i
ntb[ ]
=newi nt[n]
;
for(i
nti =0;i<n;i
++) {
Sy stem. out.
print(
"Entercoeffi
ci
ent1:");
a[i]=ob.next Int
();
Sy stem. out.
print(
"Enterexponent1: "
);
b[i]=ob. nextInt
();
}
for(i
nti =0;i<n;i
++) {
Sy stem. out.
print(
a[i]
+"");
Sy stem. out.
print(
b[i]
+"");
}
}
}
OUTPUT:

7.
i
mportj
ava.ut
il
.*
;
publ
i
cclassMain
{
publ
icstaticv oidmai n(Stri
ng[
]args)
{
Scannerin=newScanner (Syst
em.in)
;
i
ntsal;
doubleavg;
i
ntx=0,y=0,z=0, p=0,q=0,r
=0;
Syst
em. out.println(
"enterthenumberofper
son"
);
i
ntn=in.nextInt();
i
nt[]
[]a=newi nt[n][
2];

for(
inti =0;i<n;i
++)
{
Sy stem. out .
print
ln(
"ent
ertheageofperson"+i+1);
a[i][
0]=in.next I
nt(
);
}
for(inti=0; i
<n;i++)
{
Sy stem. out .
print
ln(
"ent
erthesal
aryofperson"+i+1);
a[i][
1]=in.next I
nt(
);
}
for(inti=0; i
<n;i++)
{
if
(a[ i
][0]<=29)
{
x=x+a[ i
][
1];
p=p+1;
}
elsei f(a[i]
[0]
<=34)
{
y=y +a[i]
[1]
;
q=q+1;
}
else
{
z=z+a[ i]
[1];
r=r+1;
}
}
avg=x/ p;
System. out .
pri
ntln(
"Avgforagegroup(25-29)i
s:"
+avg);
avg=0;
avg=y /q;
System. out .
pri
ntln(
"Avgforagegroup(30-34)i
s:"+avg)
;
avg=0;
avg=z/ r;
System. out .
pri
ntln(
"Avgforagegroup(=35)is:"
+avg);
}
}
OUTPUT:
8.

i
mpor tjav a.i
o.*;
i
mpor tjav a.uti
l.
*;
publ
iccl assMai n{
publicst aticvoidmai n(Str
ingargs[]
){
Scannerob=newScanner (
System.in)
;
System. out.pr
int
("Enterthestart
ingpoint:
");
i
nts=ob. nextI
nt(
);
System. out.pr
int
("Entertheendingpoint:"
);
i
nte=ob. nextI
nt(
);
whi l
e( e>s){
intt emp=1, sum1=0, cou=1,
sum=0;
whi le(s>cou){
if(s%cou==0) {
sum=sum+cou;
}
cou++;
}
whi le(sum>t emp) {
if(sum%t emp==0) {
sum1=sum1+t emp;
}
temp++;
}
if(s==sum1) {
if(sum<=e&&sum>=s) {
System.out.pr
intl
n(s+"&"+sum+"arefull
pai
r.
")
;

}
else{
System.
out
.pr
int
ln(
s+"i
shal
fpai
r.
")
;
}
}
s++;
}
}
}

OUTPUT:

9.
i
mpor
tjav
a.ut
il
.*
;

cl
assTest{
publ
i
cst
aticvoidmai n(Stri
ng[]args){
Scannersc=newScanner (Syst
em.
in);
System.out .pri
nt("Ent
ernumberofel ementi
nar
ray:
");
i
ntn=sc. nextI
nt();
i
ntar r
[]=newi nt[n];
i
ntodd[ ]=newi nt[n];
i
ntev en[]=newi nt[n]
;
i
nti =0, j=0,k=0;
for(i=0; i <n;i
++){
i
ntnum =sc. nextInt(
);
i
f( num%2==1){
odd[j
++]=num;
}else{
even[k++]=num;
}
}

k=0;
j=0;
f
or(i
=1;i
<=n;
i++){
i
f(i
%2==1){
ar
r[
i-
1]=odd[k++]
;
}el
se{
ar
r[
i-
1]=even[j
++]
;
}
}

Sy
stem.
out
.pr
int
ln(
"Out
put
")
;
f
or(
i=0;
i<n;
i++)Sy
stem.
out
.pr
int
ln(
arr
[i
])
;
}
}

OUTPUT:

10.
i
mpor tjav a.util.
Scanner ;
publi
ccl assMai n{
publi
cst aticv oidpat tern(){
Scannerscan=newScanner (
Syst
em.i
n);
System. out .
pr i
nt l
n("Enterthenumberofrows")
;
i
ntr ows=scan. nextInt()
;
System. out .
pr i
nt l
n("**Pri
ntingthepat
ter
n**
");
for
(inti =1; i
<=r ows;i++) {
for(intj =1;j<=i;
j++) {
i
f(j%2==0) {
Sy stem. out.pri
nt("1"
);
}
else{
Sy stem. out.pri
nt("0"
);
}
}
for(intj =i
* 2;j
<r ows* 2;
j++){
Sy stem. out .
print("");
}
for(intm=1; m<=i ;
m++) {
i
f(m%i ==0) {
Sy stem. out.pri
nt("1"
);
}

el
se{
Sy
stem.
out
.pr
int
("
0")
;
}
}
Syst
em.
out
.pr
int
ln(
);
}

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{
pat
ter
n()
;
}
}

OUTPUT:

11.
i
mpor tj av a.util.
Scanner;
publi
ccl assMai n
{
publicst aticv oi
dmai n(Stri
ng[
]args)
{
i
ntn, p;
Scannerscan=newScanner (Syst
em.in)
;
Sy stem. out .
pri
ntl
n("Enteranumber ")
;
n=scan. nextInt
();
p=per fect orNot(
n);
i
f(p==n)
Sy stem. out .
pri
ntl
n(n+"isaper f
ectnumber"
);
else
Sy stem. out .
pri
ntl
n(n+"isnotaper f
ectNumber"
);
}
publicst aticintperfectorNot(
intnum)
{
i
ntsum=0;
for(inti =1;i<=num/ 2;
i++)
{
if(num%i ==0)
{
sum+=i ;
}
}
retur nsum;
}
}
OUTPUT:

12.
i
mpor tj
ava.ut
il
.Scanner
;
publ
icclassMai n{

st
ati
cdoublecom(i
ntn,intr
)
{
r
etur
nfact(
n)/(
fact
(r)*
fact(
n-r
));
}

st
ati
cintfact
(i
ntn)
{
i
ntm =1;
f
or(inti
=2; i
<=n;i
++)
m =m *i
;
r
eturnm;
}

publicstati
cvoidmain(Str
ing[
]args){
i
ntn, r;
Scannerscan=newScanner (System.
in)
;
n=scan.nextI
nt(
);
r=scan.next
Int
();
Sy st
em. out
.pri
ntl
n(com(n,r
));
}
}

OUTPUT:

13.
i
mpor tjava.uti
l.
Scanner;
publi
ccl assMai n{
publi
cst ati
cv oi
dmai n(
Str
ing[
]ar
gs)
{
Scanneri n=newScanner(Sy
stem.
in)
;
i
ntm=i n.
nextInt
();
i
ntn=in.nextI
nt()
;
i
nta[][
]=newi nt
[m][
n];
i
ntb[][
]=newi nt
[m][
n];
f
or(i
nti=0;i
<m; i
++){
f
or(i
ntj=0;j
<n;j
++)
{
a[
i][
j]
=in.
nextI
nt()
;
}
}
t
ranspose(a,b);
}
publ
icst aticv oidtr
anspose(intc[
][
],
i
ntd[
][
])
{
f
or(inti=0; i
<c.l
ength;i
++){
for(
intj =0;j
<d.l
ength;j
++){
d[i][
j]
=c[ j
][
i]
;
Sy stem. out.
pri
ntln(
d[i
][
j]
+"")
;
}
System. out.
pri
ntln(
);
}
}
}

OUTPUT:

14.
i
mpor tjav a.ut i
l.
Scanner ;
publi
ccl assMai n
{
 
 publi
cst aticv oi
dmai n( St
ri
ng[]args){
 
  
 Scannerscan=newScanner (System.i
n);
 
  
 i
ntn=scan. nextInt(
);
 
  
 System. out .
pri
ntln("Ar r
aysize:"
+""+n)
;
 
  
 i
ntar ray []
=newi nt[ n];
 
  
 System. out .
pri
ntln("theelement sofarrayar
e")
;
 
  
 for
(i
nti =0;i<n;i
++) {
 
  
  ar ray [i
]=scan.next Int()
;
 
  
  Sy st em. out .
printl
n( arr
ay[
i]+"")
;
 
  
 }
 
  
 System. out.pr intln("");
 
  
 descendi ng( array ,
n);
 
  
 System. out.pr intln("");
 
  
 ascending( ar ray ,n);
 
 }
 
 publi
cst aticv oi ddescendi ng(i
ntx[
],
intp){
 
 i ntt ;
 
 f or
(inti=0;i<p; i++) {
 
  f or(
intj=i +1; j<p;j
++){
 
  if
(x[i]
<x[ j]){
 
  t=x[ i]
;
 
  x[i]=x[ j
];
 
  x[j]=t;
 
  }
 
  }
 
 }
 
  Sy stem. out .print l
n("DescendingOrder:
");
 
 f or
(inti=0;i<p; i++) {
 
  Sy stem. out .pr i
ntl
n(x[i
]+"")
;
 
 }
 
 }
 
 publi
cst aticv oi dascendi ng(i
nty[
],
i
ntq) {
 
 i ntm;
 
 f or
(inti=0;i<q; i++) {
 
  f or(
intj=i +1; j<q;j
++){
 
  if
(y [
i]
>y [j]){
 
  m=y [
i];
 
  y [
i]
=y [
j];
 
  y [
j]
=m;
 
  }
 
  }
 
 }
 
  Sy stem. out .print l
n("Ascendingorder:
")
;
 
 f or
(inti=0;i<q; i++) {
 
  Sy stem. out .pr i
ntl
n(y[i
]+""
);
 
 }
 
 }
}

OUTPUT:
15.
i
mpor tjav a.uti
l
.* ;
publ
iccl assMai n{
publicst aticvoi dmai n(Stri
ng[
]args){
Scannerscan=newScanner (
Sy stem.i
n);
Syst em. out.print("
Enterno.ofelement sinthearray:")
;
i
ntn=scan. next I
nt()
;
i
nta[ ]
=newi nt[n];
Syst em. out.printl
n("Enteral
ltheel ementinanAr r
ay: "
);
i
ntcount ;
for(inti =0;
i<n;i++)
{
a[i]=scan. nextInt()
;
}
Syst em. out.print("
Entertheelementt ofi
ndi nanAr r
ay: "
);
i
ntm=scan. nextInt(
);
Syst em. out.printl
n("")
;
i
ntj =0;
for(inti =0;
i<n;i++)
{
if(a[i]
==m) {
j=1;
Sy stem. out.pri
ntl
n("Your
sel ementiffoundinindex: "
+i)
;
}
}
i
f(j!
=1) {
Sy stem. out .
printl
n("Yourselementisnotf ound."
);
}
}
}
OUTPUT:

You might also like