100% found this document useful (2 votes)
2K views12 pages

Tcs Ipa Java

It's questions that comes in tcs ipa Java

Uploaded by

amulyasg18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views12 pages

Tcs Ipa Java

It's questions that comes in tcs ipa Java

Uploaded by

amulyasg18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Logic BuUdlng with String manipulation - Java 09Feb 2-3PM

1. Strine ()Oncatenatlo n

publlc clus Main{


pub Ile static void main(String argsD)
{
String str1• "Welcome";
String str'Z - • Java session";
String f =strl +" to "+str2;
System.outprintln(f);
}}

2. tol.owerCase(J ■nd toUpperCae(J

String str1• "Welco=•;


System.outprlntln(strl.tolowercase());
SystEm.outprlnt:ln(s:tr1.toUpperCase{)J;

3. substrinc()

String str'Z •"Java session";


String newstr • str2.substring(S,12);
Sys:tem.outprl ntl n( newstrJ;

4. trim()

String str2 • "Java session ";


System.outprl ntl n(s:tr2J;
System.outprf ntln(str2.trlm I));

S. Reverse strinc usinc ch■ rAt(J

String str2 • "Welcome";


String reY = •• ;
for(int I • str2.length(J-1; l>•O; 1-)
{
rev - rev+ str2.charAt(f);
J
System.out.pr! ntln( rev);

6. lndexOf(I

String str2 • "Welcome";


System.out.pr!ntln(str2.lndexOf{"e" ,2)1;
Find count of vowelJ In a Strine

Strlns str • "Good Afmmoon•;


lrrt: c=O;
for (Int I• O; I< str.length(); l++J
{
lf(str.t:olowerCase().charAt(l)-'a' 11 5tr.to lowerCase().charAt(ll-'e ' 11
str.tolowerCase().charAt(iJ- "r 11 str.tolowerCase() .charAt[ij='o' 11
str. tol.owerCase( ).charAt(i)- ' u')
t
c=c+l;
}}
System.outprfntln(c);

Cou.rrt of words In a senten.,.,(Strlne)

String str • "This Is a l ava code complier ";


lrrt: C•l ;
String news• 5tr.trim(J;
for (Int I• O; I< news.lengthQ; i++J
t
if(news.charAt(i)= ' ')

c=c+l;
}}
System.out.prfntln(c);

Odd/Even positioned substrl ni:

String strl • "Olgttal";

for(lnt l=O;i<5tr2.length();l=f+2)
{
Systi!m.ouLprlntln(str2.substrlng(l,l+ll);
)

Readlnc dm as ddmmyyyy and dlsplaylnc day, month and year

String str = "12122013";

String day• str.substrlng(0,2);


String month • str.substring(2,4);
String year • str.substrlng(4,B);

Sysb!m.out.prfntln(day};
System.oo t.prlntln(m onth);
System.ou t.prlntln(y earj;

String str • "Happy Nl!w Vear";

// Crellting array of string ll!nsth


charO ch= nl!w charfstr.ll! n&th(J];

for (lnt I = O; I < str.length(); I++) {


ch[l] • str.charA!ti);

System .out.pr1nt ln(ch[l)I;


}

I}

Revenc • ott1,. word by word


String str - "Happy New Vear";

Strl"lf) ns • sir.spilt(" 0 );
String nm,.••;

forjlnt f-O;f<ns.le ngth;f+t)I

for (int I • nsU).ll!ngth()-1; 1>-0; \-)


{

nstr - nstr + nsu].charAt(I);

}
nstr-nstr+" " ;

}
Sysb!m.out.prlntln(nstr);
Probleml-Solution
• Pri nt all odd numbers in a range (read the starting number and ending
number)
public static void main(String[J args ) {
Scanner s= new Scanner(System. i n);
// read the range
int start• s . ne~tlnt() ;
int end = s .nextlnt ();
System.au~ . print In( "Printing odd numbers from "+start+ " to "+end);
//use a loop to pr1nt odd numbers
II n,2- 0 mean~ n ~seven
for ( int i =start; i <=end ; i ++){
if(~2 I• 0)
System.oirr.println(i };
}

}
Problem 2 -Solution
• Read two number and find the value of one number raised to the
power of another.
public static void main(String(] args) {
Scanner scan = new Scanner (System . in);
//read x and y
int x • scan .nextlnt();
int y • 5Can .nextlnt();
int result•l;
II x"y
for (int i~0; i <y; i ++){
result = result•x;
}

System . out .println(x+"""+ y+" • "+result);


}
Problem3 - Solution
• Read a number and print all digits in that number.

public static void &'.lin(String[ ] ~rg~} (

int n = 1234567 ;

lxtract ,he Ja,t digit of the nulllber n by n~lO
~pdnte the value of n by n/10 an1 repeat the above ,tep
t i l l n i• not equdl> too.

""'1le( 11 > O){


lnt Jlt:ll • r1Xl0;
Sy~Le11. oat.prinlln( digi L) ;
n / • 10;
)

}
Problem 4 - Solution
• Read n number using for loop and find the biggest number, smallest number
and average of all numbers.
public at■tic void 11• l11(Sl l'l11d ] •• 11•) {
Sc.inner u~n -=- nb.l Sc,1nner(Sy,te••· l r1);
I I hnw ltlil ny nrnnhP r "
int count• scan .nextinl () J
int bi3-e~ s•~ll•8> sum =0, JVg ◄;

f'or( Jnl i=-8; icc:ount; i ++H


i nt n - ._ran . nPrtln-t(}; // r,-,irJ fl"i'lrh m1,nhpr
if{i=U) Slll,lll• n; II ln1t.u1lly set the first nurr,b~•• a, !>!iallest

if(11 > bl~)H~ -n:


lf(n<•••ll),""'ll=n;
~•"" +-n; // -.w, '\HIii • n
}

Sy~ t en, . out. swl.n Lln( "'llla,;g~ l riuatJur : "+blu-t.. ~nwll e.!:. L nllll..00.,·: ..+11,1 .tl l+"' illverilge: "'.+st.1a1/,ou11l) :
)
Problem 5 - Solution
public static void ,nain(String[] args ) {
int aaount - 25561;
int denaain•tion_coun t •5; // l, 10, ~ , SOO, 29UO -· can ndd r -~
/ declJ~e vJriMbles to , · oru denoon1nat1or L<>unl
int _2088Count•0, _500Count• 0 , _100Count-e,_10Count =0,_l(ount •0;
l"OII -,unt
/d~vide ~ t"J ~ te d~natton end r~ctuce cNr-"d' -in.antM'
for ( lnl 1 a. e, l < \i.-..,...ii.Uu11_'-uw.nLj 1-+'"' ) (
if ( -.r,t >- 2908) {
_200'IC.,.,..t • ucunt: / 2800;
aou11r- ... a110Unt - _2eeecOU"lt .. ieoo; )
els• lf ( - >• 590) {
S80CC1#1t a MOl.inl / 580;
MDiJ"'lt .. Pol,Jlt .. _SOICOi.ln't • see; )
w• if c-, ,. tee) {
_100CCU"t -"' ~n~ / 180j
MDUTft - oP"""-"'t" .. _1eeco.mt • 100; }
WO if (-.,,t >• 18) [ .. !..r<T ru lt
_11(0<,nt • ...,..,.t / 10; Syn,.. ou< .prlntJnL280i'Caunt• ·-2088 ") ;
~ t • lf1t - 10Cou "lt • Le; } Syn•.ou< .prlntln(_~OCount♦•·- " ) ;

Sy >Leta•"'•' .p-ri11Un(_l80Cuur1l• '"•1'8 "') ;


w• Sy•tea. cH1t' .prlntln(_lOCour1tt" .... JO .. ) _;
Syn111, oi11T ,prlnt"ln( .lC01.1nt ..• 1•);
}
Test Your Knowledge
What will be output of the below code snippet?

int X = 1234567 ;
int C = 0 ;
while (x > 0){
Prints the number of digits I n the
X /=10; given number.
c++; Output : 7

}
System. out .println(c);
_j
Test Your Knowledge
• What will be the output of below program?

int number c 12345;


int result = 0;
7
do {
result *= 10; Reverse the given number.
result += number% 10;
number / = 10; Out put : Result 54321
} while (number > 0) ;
System .out.println( "Result "+result) ; --~~~::.----------
Test Your Knowledge

• What will be the output of the below program?

for ( int i•l; i <• l0; i ++){


for (int j=l; j <=l0; j ++){
System. out . printf( "%2d ", i *j );
}
System. out .println("" );
} ~
,
----~-----'
Prints the multiplication table.
Output:
l 2 3 4 5 6 7 8 9 10
2 4 6 8 10 1 2 14 16 18 20
3 6 9 12 15 18 21 24 27 30
Try It Yourself -1
• Read a number and print sum of all digits in that number.

• Example
• Input : 2030
• Output: 5

You might also like