hssiqment- 02
Ephim er bop at ts syntax.Wte a C-geq3om te checke ahebhea
enteed mumba is pime or mdt Usinq fx lop.
Synax:
fos (itialisatox;
Conditin; ncremeatldeevenent)
{ statments;
Initialsetion
Vaxiable
ofVavable % done at the begining ot he
used n iop.
s
- far' sa initalsatiern paxt
entry contl lbop
ca led a5 loep Corntasl Vasfable.
nitialisatien:
ahich Cansi st 3 psts:
-This past all be
exected anly ance
at
at the begiming of lonp
-checked each fime beoe entene
-I Candit ian is
the loop.
TNe Then & ouly then statmets
be exeeuted wll
eke it exts foom
Saem laop.
idTncse/ecsement
-When bedy f foy loop is
back to begig of
fx
executed
to
cot s trancted
.3
loep
(ovtsl e;thex tncxe /decvemexf
Vasiable.
All
setions of fos loop
aze
• Sepauated by semicolon.
.Al
Au
sections fox loop Qae sptional
be alJays (n such cose
aill
Tue. Žond
We can have nee than
tiatsaton statmemt sepalted
by Comma.
-WeCon have mese thn { ce mert / deckement statments
seported by cormma.
Poime ox Not
Checking Togam
#inelude <stdio.h
Void maim);
tnt n,i,p-0,
pintí ("Enten a umbex).
break;
pritCA ts a Rine Numbe',n).
>gce
else
poitf (d s Not o Bine Numbes', ),Lb
poime.c
>.laout
J Entex a numbe8: 7
7 5 a Pime Numbes
fme ueton Explkn dcaent carteeis cf usex-delined funcdhens.
Funton & set of tnstuetns ta pekom a pertkuay tosk
• Thexe e 2 types of functions:
standaxd/ Bult-tn Fancttons
U- eftned Faetons.
• These axe 4 defesent categosies of uses-defined functns:
Aed pagametey wtlout tun Value
inchde<stdth>
Void add
);
Void maim ()
addc),
printfBye).
void addl)
a=\0;
b=20;
catb;
pintt (a'",).
30
Bye
w:hod Rasamcteas & Wih Retuton Volues
#inelude <sth tahs
nt addc);
Vd main()
ac= addc;
pn ("4, ),
nt add C)ot shxmeog s tohevdh
C-a+b;
tusn c;
30
3vbh aVametes G w:theut eutm value
Vaid add(nt aint b
fn
yoid main ()
-l0,
add (a,5);
b-20:
pointí ("Bye)
wid addbn a,sit b)
C-atb;
printt (,);
30
Bye
with Posameteas E
wth Retun Values
#ineude<steih
nt add (imta,int b)
void moinC)
{ nt
$
a,b,c;
b=20;
C= add (&,b).
rt add (Gnt a,rt )
C-atb;
Yetusn C;
320
3Ete Wte a C-Presam to mplement brasy 5eeNch on ntegen
Jtinclude Kstdiohs
Void man ()
i tnt
printf
B,m, k,high, lous, mid
("Entes No of
,afound-0,a [,
Rrsay Elements:");
Scanf
(/d, &m);
qrintk("Etex
aray elements");
fox
{ (t-0,<n;f+)
skant
(ta"ga]);
pst ("Etex Seanch
lement-")
Scanf 1,&),
high-n-y
whtle (towd-
high)
I print (Seavdh Foumd ),
Yebuam;
low = mid+l:
else
printf ("'Seaach
Unsucesshr),
> gec binarys <
>.la.out
Entex No Aqy 5
Elements :
Enter asray elements:
Entes Seaseh Element:
3
Seaxch Feund
wte 5yntar of nested f.le stntewiert
-est of 3 numbess
wotte acpaom to ind
uing nested
)
ef.else statment
Syata
{1f ean d
statmentl,
else
tohnent
;
else
statment
.When seies o decison axe to be made we have teo Use mose than me
ebe skatmet atek can be Testing
In above syntax,if cond" ! ts false will 4o its else post
execute 5tatment 3 4
. con dl s tve,ae entes nto t bleck ef nestedl
Condn pe cond^ 2 be checked f itis toye stabmu
tl be exeeuted x else stotmert 2ll be ereuted
3//Larest of 3 Nmbes:
ttinchdesstda h
iSt a,b,e;
prntf'Etes 3 dfexont No.
else
printf (d s Lasqest", o);
el
gcc
las
>fa.out
Ente 3 4:feert numbeg
23
3s lavqest
5What S ecuston? Wtte o C-Pegsam
achoqsam to tndaxaestofumtbea
Compute factosial of umber using secusien.
tchce he dhky of fuetian t0 Call lselt to caled ecasive
tunetien. The pro cess s called oecasen.
Reassive fundion
sheud
keep on Callug itsel t|| cestaim condition fs met e ornce
Conditiom becomes falso,secuNSive tuneton existsele thefdion
all ca tsef ne o
sncte ve ch
tms.
1/ Factoal Given Numer
#finehde<stdto.hs
nt fact nt
Jold
Voicd mafn)
pinH ("Entes a unbes)
{(nsll
etun
un
n
n-)
fadn-);
n
>.la out
3 EnteN a no:3
6
Explain Syntax of saitch stotment.wste a Gpso qram asing swtth to fnd
qfade Beced by studert.
.'-Oslinction
'A'-Fst Class
.'8-Second Class
'c'- Thisd (loss
.-Pass Class
Sateh stabent s
Syntax:
suliteh expessfon)
{ case label stobnnt;
bseak;
Case label2: statment?;
byeak;
Case label : statmet n;
bxeak,
delauti slalbment;
multinasy bsomhng statmevd TE takes the valhe o qieh
Vaxiable against the list of s case labels ahen match i e
Tound lsk statnerks asssited uh that case aill be Keruted.
ne expsessten to the ateh9s of t ox
type choactes
Label1,label2.- axe
temstter comdant expsessons.
-Case
- The
label ends th coln.
bseak statment at nd ot ach cose bleck ?ndcates end of thst
block fF Causes ev
ext Som
detauH an
epaessin doesnt
opthnal case
match
ahen
süitch
th
ese
statments
any
l
the
be erecubl qf Value
cae values.
- No
Each
2
Case
cases
label
Can
must be unque oeble to xane
have Same label.
- The default Can be placed anyahee
fn switch statmert
Used at end of suiteh statment. but usually
- We can have nestinq of sai toh statme.
1 Gade Poegsam
4indude <stdie.h
Vid main()
{ achar C;
Grade
points 'Entex Ehasacs")
Scanf (4e'",Q);
Sait ch c)
Case 'Sa': points ("
Drskimtian").
byeak;
Case ' :pbint{ ("Fiast clas").
byeak
case B': prints ("Second Class).
case 'e' :
break.
pimt
break;
('Tiad Class),
case o':paitf ("Pass (lass
bseak;
case 'E':pain ("Fa');
boeak,
delaull: pointf ( Jnvakd nput..
gs qade.c
>aout
Fntes fcacte Gsade: G
Distinetton
Wste a CPogborn to Suap 2 nunbess usima pass by Values è Pabs
pass by zed
-ne pobamete passinq tehvig ues fn funetien.
Ras y ahe
#inelude <stdioh
void swap(int a, int b),
Vd main()
poimtf ("Befoe Suep: a=td, b: /d", b),
Suap(.b),
azb;
perintf ("Pftex Suap:a-td,bzd",a,b);
>gee sap
>.laout
3Reoxe Suap:a\0,b=20
Aexe Suap a-20,b-10 :
•lIPass b Refesence
#inehude
stdtok>
Vaid slap(nt
Void nam ()
&at&5);
psint ("a Betove sapi az
Swap (a,b); td,baa,b),
psint
(Atey Saap:a=td,btda,b)
Siop<a:t a,tnt &b)
t= *a;
at*a-
+a *b;
*b4;
>.la.out
Befsae swap: a=\0,b=20
Ater Soap:
a-20, b-l0
8] D:fezentiote betaen awhtle & do atie loop.ate a C-posqam
too find Sist 5 terms Sn fbonac Sesies using do ag le loon
do.akele rle
- Exit Contol -Grtsy cortl
-Ascond
sp
ts
Loop
checked at end f -L loop
cene, ts true.then £ y
then
€cond s olse m:kally bedy of loop fs exeded he
st:ti
atleast
body
Once.
of locp il be executed
se ll hat be execueted.
Symtax?
-Smtax
witaltzat;:
ddof
statments
stutnerts; /desenent,
Tneremet
Sraement /eckent;
Joshile Ccondition),
-#include <stdahs -#ineludestdto.h)
veid main() Void main()
Smt n=l;
whte (n>)
do
print ("Bye);
JBye
Bye
Rbenacci Sebies
#inelude<stdio.h
Votd matn)
{nt n,fl,s-\sres;
point! ("Ente a no:");
Scarf ("1d",n),
points ("d,1a',$,9;
do {
C+t;
}ahie (ca(n))
3Ente a no:5
I|2 3 5
I wte aa c-Psoqtam to sart grem Pn ascendtng ovdo sing
numbexs tn
inude<cttoh>
vod mafnC)
pointf("Ert numbes of a say elemerts:");
Scamf
pointf
(',&): l
("Erterarsay elerneuts:");
{temp-nulg;
Tum (]:num;;
point("8,al);
Ertes ubes of axtay elements: 5
Entes agTay elements:
3 6
D 1 3 6 9
Ial wstte a C- Poqpam t mutply 2 matbices check ath multplcaon
Condihons E display appropsiate messaqe.
nelude<stdto.h)
votd main()
{ nt alJM, 6IJI-1,:, t,cla;
psint(" Enter avvay A elements:"
for (t-0,t<å,t+)
Scav4(&atn
patntf("Entey anayB8emerks:");
Seanf("d", 2al11,1);
('Poduct fhey Denerts)
for(-0;t<2;:++)
fo (ked, k«2;k+)
fos
Guo,t«2,i++)
fo 0t42:+)
Entey a3ay Aelements:
2
Enter anay B elements:
|2 3 4
Pyoduct axsay Elements:
7
lo
15
22
qwrte a C Poqtarn to dtsp lay the Ralloutng potte using mesy
2
I 2 3 2|
tinchde <stdto.h>
Void manC)
it'spaceg,80$;
printfEntes numbe of Tous:"),
Seanf
("",&Tous);
fo (:?4-
Xous;?4+)
1 fo(space=1;Space<- XOWs,-i; spasg++)
point (").
fov
(g-l;f4=;j+)
pintf ("w)
Entex numbes ofyo ws:3
| 2
2
3
I
2 I
1]Wrte a Cpogtam to stmulate Simpk caleulato sufth statment
usins
inlude<stdto.h>
void main)
1float
a,b,es;
char opt;
pointf("Entes a b Values:")
Scanf 7.f 7",&a,&b,
pointf
("Chosse Optan Chasactes
fox Respective Gpexaton");
pointfCa.Add \n b.subtact
inc. Multigleatem n d. Division\n");
1 case a': sesatb;
pains ('sum: e;
beak.
Case 'b:ses -a-b;
paiatD:fesence: %4xe)
bseak;
Case c:ses =ab;
bxeak;
case d':f (6 =o)
{ ves :alb;
pimts ("Guotient ,e)
else
points ("zeso Diksien Exv).
break;
defaule?pointf("Jmatd Chesaetes Chice..")