PPS Notes Unit 2
PPS Notes Unit 2
Branch : IT,ME,CE
Year/Section : First Year - N1&N2
Name of Subject: Programming for Problem
Solving (BCS-101)
UNIT-II
Notes
~
~~ i. L~ e_ 0,~ uJL_J__ . ~ ~ ~ _
M
(3 ~
r~ - ~ ~ ~~
~
°'-
JI-,_ ~~ ~ 1 Uv l~
(0,.) i_ f - ~~~
(b) i, 1- ~ "
( C.) AwiJct. - ~ "
;+ C{~LLirvv )
,w- 1 ~~t
r 3
~\...(. , ~
M t~ ~
3
D C n Co bol Smuduu
-salaet
int a, b, C, big
pi ("ELa a thre uwweans\n)
Aam ("dAd1.d, La,Lh,sc)
b big)
- b
3
CC bq)
big = C
("Graals o
bi)
The l slalemel
LAP to d th u a u t o hue
ww.burs ww -osesale ant
/x Usinq Q-Mud stalamt find t lngust
ho nwn bons k
inclda sldio h)
iude Ceeh)
yeLd ma
nt ab
AS N L ordd.
To Delermuml Even 0x 0dd number a
inude {sldish
include kaian)
Ucud mam ().
Yum
bi (" En yun Numben i \ )
ACam ("d &nn
m num 2
m==0)
wm
~e, ~ 1- ~
Lk,
~~2- ~
)
J.u-
{
{ ( ~polh¼~ 3)
/2-h,..t, ~ ~ ' /
iX~---
1-, k - t - ~ 1- ~
~
5 : .J ~ £U -tL fhr.e..e_
L oq1C AL oPE R.A To R
f F F
T F
F
F F
1
T T T
~ 4 ~ ~ M h.
Jk Y- xP Y ~ ()v-., A,,~
b J-
f F f
i= 1 T
T f T
T T T
~
O ~ m _ _ -£)_~ ~~ N D T : ~
(0 ~~ N~ L9-N'- k ~
~~ f,~u- fo
orJt99L- °'- ~ ccJ___ L xp Y. £VY1- ~
mAL e, r ~ ~ tB ~ - P-v.u-tt
EJ< pY ~~
~
) (~ ')
) ~ -
) (~
1~~,J__
-j/ J~ cu v_
(')pe__-rcJM-i,
c ~ J, o ~
o , ~~ 0~
6~ Pl\OL,,,,. J. kcD.-W\JL th ,
:
~ 1k v ~ y ~ J_ ~ k ~~ 2-_ 1 , ~~
e,_,-Jt be.- ~ ·-r ,(h¼ fJY'\ 3 _·
~ v~ Y- ~w vv cl,,
~ :x., )r ;
~~
;
;.,x 7t' } ;
~ ~ ~
~ p~~ :JhJ.!(.
M .M ~
Pnfh- ~ - ·'.ek.. D~~ -
~ ov v,, to ,_ _~
( ])(AtlA- j T/ P::= LOhi V ~ CN\., i V) A~~ ~ 0 ~~
~ ~~ ~ ~ ~p-'2- °t ~
Tt ~~ ~ Y-.P.M1-~ ~
1U t::i ~ 4
-Ir"- v ~ ~ n-,_ -4t -~ -k4.- ~ 4
o.J,/y,.t' ~ 0 ~ , ~ b..,__ ~ . 1 ,¼ c2-. Cl._ ~ V,
1¼. V~ 4 tJ--,..a.. h py.v.¼ °"'- ,u, 4 ~ pve,,,, ..t vl- oy- d,.,w,.,,tJ..
~1- °"' ~ ~p---"- 1- n---,,_, v ~ ry~ IM- t"'- ~
~ M,l,.. 1 1"-"- a,\¼~~ O~ -
, ~i°'1-- V<>v~~
A-n \-h 'M e..~ C G.,.. P"' ~ O'-,'\., Re,~~ A--il rh 'M e.l"\c, ( ;,< f"f~ e--A
R = 2/ 9 o C\. = 2/~
R - 2,0/9 o o.. = 2• 0/ ~
2./9,0 0 C\ = 2/q ,o 0' 2.21-
R --
D · 21.'L
R = 2•o/q, 0 0
4- c~ - S--/1 2- 4 •o
R -- 9j-;._
R -- q, 0/2- 4- °' - fj,- 0/2- -4· 5
R -= 9 /2, D 4- C\ = 4.5
R -- q,o/2.1 0 4- ~ .5
''1~CMh~
-B-
:- 1t ~ ~ ~ e-w V\... o..-6.
L r_
f' "'"uu;l, (.&v\ V ~~ ,
f
, ,__.._ ._.,~""'- -h--~
. •
::;:-----------
, \ L ,. .- _o -
- ~-
~ "IL~ - - 14-h--~ -~ --:,--- J~
- ~ ~
- , -t ~L , -
__l-__ c;JL_ ~ ~ - tN d:ovtJ-"'v a V)ot~-~ ~
_it=--~ ~;.J, ,,L_ ~ -~ ~ ~ ~ 6-,_
___kv_~ _O)lL_h _Lnf-_\AU__ he: . ~~--L;l~ \f. _~-t t9; :
- ~ • _ W~ lb-L· cl:o_v~---,~ ~ 1 \0.bW\.LJl .
1
- - - - - - ~\ ~ ~ ~-•-~h_ ) __ _
_ _ _ _ _ _ _c.Va_
_ :l -[i _~----+-
( _..,.
) - - - -- - -
p:P:.Jf ( h \'.0 })xi Vt,'.:! M virt iY\,!;Wv..L_ '~) ____,,
1
.____ _
~ )I---+-';----
- - - --
r
I
..
I
- ·c
--r-
I'
w ~ I.e. a It>Yl'f' h'Y'- C.,J l✓ Ck Ye VI 'C.cA the
Q Q
1
,U
I I '--
0 '0 C )
1 /J 7 ,-
11
I, pnJL ('• \V\ - C:xn..d~ A-s
\ \ ---
-Uu.. i t ( hd._: > s ~ 1, L (_ .(_ D 1 !~ t & ~ 5 b u c)
O C
/ - - _ -- - - - - -
_ ___.__
,1 _ J- - -- ---- -
~ \A.,!, ➔ .J ¼_, b gJii "'tr f½ YQ, ~ M,i ±-o,.b.,.,,. C!,b j10 ~ ..± ,
I (,.:) X? k °'- e,_ vrrr . __,
rn ti·vJ. -- CV N..ll,,.,,___ Cl. 1:-,; ~
T
- - - - -- - --- - - --- --
- -- - --- - - -
- --
- - - ------ - --
- - __ --- --- - - - -- -
---- ----
r
- _. - -
-- --- .
r
(9 Wb ➔ I: ~ A i V) C /-& G'"'-"\- wkfu-?k Q. Ve.,.,-,_ ck~Vt,_
11 M °'- v~.J._ 0 b LJE:vL&er~ . Vk · i hpv-C
s~-=;, I:
11
!l
I
d
.I c1w.. ct i
ik a uam Te dalemime whaMer
he Chanadun. Amluud a Cabia
Ltten a mal
Cask ttan a dligut
shaual Apmhel
hanacAs ASC Valus
A 6S 90o
97-122
O
Specio 127
T o chuk yhe ochoaco Lmtd
Rayhoand "
indudastdio a
nclnda lamB h
UoLd man _
char ch i
pnnl "Enlo a haacleu on bylocmd )
bam &ch)_
pat(" n The ckanacln is_in phah lak
Lisz ('th >=94 _hk=122)
ninl"\n Thedhanadn s in hawalas
2lse 'Ch> =48 2 ch&:52)_
int(" n 3he haracer as dit)D
hn "n J thanaclen as Ahualsuowbe)
/ Anohen o nhn4 pXem0usroham *
inudsdho h
inudetne. a>
veu moin)
chan ch,
e t ab, cd T1,Y2,
pid"Eta he valus a,hand c \n)
Atanf ("_Zf Z£ £" á2b a )
d= bxb ax C
if a==0_L b =b==o)
i l " In No Sel
i l i f a == o)
=
- c/b
)
3
else if(d <o
i ( "l n Koots ae magnna )
1 =-b+ t (d))/(2*a
Y2=(=b-sart (A))/(2a)
E ,Y112,
n l "\n Rrot 1=Zfand Reot 2=
trh(
OPERATOR PRECEDENCE TABLE
,Jh.,u__ o ~ o.> t. ~ ~~
8i 1TW1 SE o Pf? RA T oR.S : -
~ ~,;_~c.,-,J-
0~. J~ l\,11,L ~ /JJ
w .~ r ~ .r~ ~ .
~
v~~ ~ 1- w .:~ rm-J~ o (YU _ -
1) rv ( hJ --~ ~ o~)
~) M.. ( bi ~- AN D o ~ )
3) ( '""'~vt..-oR o ~ )
b·, r~
4) A ( b;\-·v-.{ ~ ,R,, i<:~ vt - oR Dp,t>L~)
S) <( (
0
C) >> ( b',\-L0M., ~( :t Yi d~ o~ J
~ .~ ~ h \ ~ w- 1 ~ o~ ro f}-.L ~f~ w1
® i , ~ ~1,~~
~ h~ kh
Y ~-
/JJV-
Ah ut1M4.- o ~ . <1
W-- ~ ;A "t .9 1 ,~ Mt -~ o .
b; d . ~~ _,, .± - Oooo ci11L
~ - ti ~ = 1 :,_1
x, ~~ = Oo oo 00 11
© I\ ~ ~ W AA () 4 t1-'L ~ W A,\ 1 ~
0
I ~ r&Y-½
~~ :J- ~ o.
o ~ W- AJ ,,u,t,- W 1 I
~- -x.,1'~ = 00 01 01 00
~ h k_ ~ft .ul _ hw ~~ - ~t
0~ ~~h'V, fk. ~
~ ·~ ~ r¼_ V I ~ { ~ ts k i ~ L . 6V\ Yl i -1M~
~- : - ~ << ?, = 00 11 1 0 oo
®_ >> _}l.L'\;_
o tv~~+.ct,
1,J ~ \h 1-
r(..vt-
v, t:-L"t✓"'-
Y L V-C\.y_
""\. ~ ,v, l},
6} W W½-( ;),-.. t•t-
Y) J·j
/-1t i,,.
),. '.lr,- _h ~~ J~.
1.:ik;;_
~ t--
- - - - - -
~ 1- tw.- CD»._ ~~ ~
~ G~~ ~ be] (fV\J L r9 /4u.., {
~ 'Ji
1~ \J ~ 1 .~
~ ~ t:Xp¥ ~ L~ . lf
~
0.,
d_a.,!~ t
LI,(.,~
~ A},
I
{
~ ~~ i :
~CL 1- ~~G, ;
by ~ / •
(_£).),,t_ ~ ~2 -:
~ u. 1 ~~ :
by 1--0.,k ;
~ d.,,;..v ~
~c k ~ ~ .·~ v ~ ~ ~ h,
w Y\ h'
No TE : Jk 6
p~ ·
~~~ / Li~ ~ ~~~ ~~ ~
~ ~e -k
1 - ~~ or ~C 9v l ~ ~~ k ~
~ v-,-..9.,,.l:,,,. . s4 OJ..trh 'M etc, 0 rJ#v, CUt.L- ~~ •
J- f 1:,-J_, V~
0
~ ~- &...,J~ ( q_)
f u~ 3:
1
,+ ~ ~ W w ~ W t- k ~ if - .JM- , & ~ ~ ➔
Cow
¼ \/\ )~ ~ ~ ~~
~v~ I"' °'- . p ~4
~ i ~ f¼, p~--"- I V 1 C Y ~
1
~ - ~~ t .~ W L ~ ) fN__
J.& . J ~ ~
/.N'> v~ 1- °"f~ OY ~<
w... ~ ~
\.,u ~ t'rl_ IY1 (Y,< JLV .-
~~ ~ ~ ~~ -
°'- ~c .,k µ~~
~/l}L, -{~
A.},
* M~ ~~wok~ ~O-NL~~
,ch - Ju _ ~~ .
t A, \~ ~ ~ ~ t,(_~ ~r ~ ' } whtrtP_AA
~ 1
O ~ DY ~~at h j, Yu½<OV4 .
~ °'1 ~
1
switch case default Statements: The control statement that allows us to make a decision
from the number of choices is called a switch, or more correctly a switch-case-default, since these three
keywords go together to make up the control statement.
Syntax of switch:
switch (expression)
{
case constant1:
statement1;
break;
case constant2:
statement2;
break;
:
:
case constantN:
statementN;
break;
default:
default statement;
}
Where constant1……….constantN are results of expression tested. Only one statement (or set of statements)
out of statement1……….statementN will execute because expression returns only one result at a time. Hence it
requires break after every case statement. If result will not match any case then control will be transferred to
default body and then back in main body.
IMPORTANT NOTE:
1. The limitation of switch statement is that, in switch, we can use only integer, character or any
expression which output either character or integer value.
2. The character case must be given into single quotation mark (i.e. ‘A’).
3. Each constant in each case must be different from all the others.
4. We can execute more than one case into single case as given below:
case ‘A’:
case ‘a’:
A few useful tips about the usage of switch and a few pitfalls to be avoided:
a) The earlier program that used switch may give you the wrong impression that you can use only
cases arranged in ascending order, 1, 2, 3 and default. You can in fact put the cases in any order
you please. Here is an example of scrambled case order:
void main( )
{
int i = 22 ;
switch ( i )
{
case 121 :
printf ( "I am in case 121 \n" ) ;
break ;
case 7 :
printf ( "I am in case 7 \n" ) ;
break ;
case 22 :
printf ( "I am in case 22 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
3
}
getch();
}
The output of this program would be:
I am in case 22
b) You are also allowed to use char values in case and switch as shown in the following program:
void main( )
{
char c = 'x' ;
clrscr();
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 'a' :
printf ( "I am in case a \n" ) ;
break ;
case 'x' :
printf ( "I am in case x \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
getch();
}
The output of this program would be: I am in case x
In fact here when we used ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of these
character constants.
c) At times we may want to execute a common set of statements for multiple cases. How this can be
done is shown in the program example of “Mathematical Calculator”.
d) Even if there are multiple statements to be executed in each case there is no need to enclose them
within a pair of braces (unlike if, and else).
e) Every statement in a switch must belong to some case or the other. If a statement doesn’t belong to
any case the compiler won’t report an error. However, the statement would never get executed.
For example, in the following program the printf( ) never goes to work.
void main( )
{
int i, j ;
clrscr();
printf ( "Enter value of i" ) ;
scanf ( "%d”, &i ) ;
switch ( i )
{
printf ( "Hello" ) ;
case 1 :
j = 10 ;
break ;
case 2 :
4
j = 20 ;
break ;
}
getch();
}
f) If we have no default case, then the program simply falls through the entire switch and continues
with the next instruction (if any,) that follows the closing brace of switch.
g) Is switch a replacement for if? Yes and no. Yes, because it offers a better way of writing programs
as compared to if, and no because in certain situations we are left with no choice but to use if. The
disadvantage of switch is that one cannot have a case in a switch which looks like: case i <= 20 :
All that we can have after the case is an int constant or a char constant or an expression that
evaluates to one of these constants. Even a float is not allowed. The advantage of switch over if is
that it leads to a more structured program and the level of indentation is manageable, more so if
there are multiple statements within each case of a switch.
h) We can check the value of any expression in a switch. Thus the following switch statements are
legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
Expressions can also be used in cases provided they are constant expressions (Cases can never
have variable expressions). Thus case 3+7: is correct, however, case a+b: or case a+3: is incorrect.
i) A float expression cannot be tested using a switch.
j) Multiple cases cannot use same expressions. Thus the following switch is illegal:
switch ( a )
{
case 3 :
...
case 1 + 2 :
...
}
Example: WAP in C for mathematical calculator for addition & subtraction operations.
Answer:
/*Program for Mathematical Calculator*/
void main()
{
int x=50,y=20,z;
char ch;
clrscr();
printf ("\n ENTER A FOR ADDITION");
printf ("\n ENTER S FOR SUBTRACTION");
printf ("\n ENTER YOUR CHOICE\t");
scanf ("%c",&ch);
switch (ch) //switch character value given through keyboard
{
case 'A':
case ‘a’:
case ‘+’:
z=x+y;
printf("\nAddition of x and y is = %d",z);
break;
5
case 'S':
case ‘s’:
case ‘-’:
z=x-y;
printf("\nSubtraction of x and y is =%d",z);
break;
default:
printf("\n ENTER CORRECT CHOICE");
break;
}
getch();
}
The disadvantage of switch is that one cannot have a case in a switch which looks like:
case i <= 20 :
All that we can have after the case is an int constant or a char constant or an expression that evaluates
to one of these constants. Even a float is not allowed.
The advantage of switch over if is that it leads to a more structured programming and the level of
indentation is manageable, more so if there are multiple statements within each case of a switch.
, c."' t,~ i-, D.-u__ Y ~ J? p o\p I~ _,._ C-1- -t ~a. ___) --:,
1."' c< ~ : Lo t Y "-°">'- 1-k v~ 1 """- ,.,i,1'_,.,, vMv1.P1Uc 6;j l
DL ( ~ ~ : ~4(.Q.., ., ,, 'I ,. ,,
h..<1 J
CA-Sf-'.:l.: -4 ')L::: : j 2- .
~ d ;:_ bl.++ ;
~ p~ u__ ~ ¥ ,,U,,(..Jk ~ ~ v ~ d' ~ 12- ~ ')(_ ~ 1 '5 L t_ , t i x_
~ J- cz__ ~ ~ v"-k
J- p ~~ ~ v~ ~ Y) ~
+e- ~ .
i
1vl 'n_.:: 4 I
ll
(J I
"\ . '
() J
~ = - -')C ~
a=- x. --;
r~v1f- c,\~ 1 J. ·;. cl '/, i,,, n , ~ , d ) 1
/
)
oj p :