Reference Material from
“Computer Fundamentals and Programming in C” by Reema Thareja
IBerative Statemets
te neheat te exectien o< a ist
These an wsed au itegen
en he value o
statemeutz,debeudirg
expressien.
) while Jop
Do-atile oop
cerelitien is tested
the shila Jopp, the
Tn
0 Whila Joop- tatemeuts is executed.
befo auy the
[Pre test p
Statemeuts X
statemeut y
Corditien
Truu
Fale
Statemeut z
Flwchant o whie icop
Syntar hile op -
statemets x ;
chile (enditien)
Statemeut Y
Cerdiien ubdate )
Statemet Zj
(
(2) Do white loop
cendiien is tested ot
Ln dodile loop, he test
Aoop)
the end o the oop. [Post-test
statewdut X
Statemeuta X)
do
staBeret Y;
ydition update;
Iadile (covditien); bdatecodiin
ex penon
statete Y
True. yditiern)
|Fala
Stateutt Z
hon thaue busic ingredicuts
() fen locp Thie. for-loop aud
ation Aoep vaiahle,Ceondition,
J-e, iniializ
updation,[
uþdatiasncenaut (decrehkut)
(initializaien;cONditien;
fon
Statomeutej
deteAminate ar de<iite loop becan
ay
* for loop is knoôn
hao mat tlaces te loop col Aepeat
the pregam
tnitializatien
vaxiable
[Gendiinckdate
COntitien
teestateunot Y
Lant
Ate
heck
for (izo; i<= o; it+)
For loop Flashat
Ex: Bint ato 1o.
# include <stdio.hs
sid. main)
iniializatien loop vaçable
int i= o; //
/test the conditien
tile Ci<so)
%d, i); statemauts
printf (" lubdate conditien experien
O|2 3
itua
cd mainl)
Iinlhialiatien
int i O;
do /sbkuuds
pint ( d, i);
i=i+i; Cenditie:
hile ic=(o): l
netn j
#idude <stdioh>
i t maiu )
statenut;}
ut i;
(i=o; i<=o; i++)
for
actum o Aai O I 2 3 4 s
6 t % 9
bosttet lopp%
aetest aud
Combaison
Pre test loop Post test loop
featue
# tests
Statencuts
exeeuted
N
hopCentl
Vaiahle fdate
iteatia
# ncluce <stdio>
iut main C)
fonl;i<=lo;)
Nested Loops
#incude <stdio.h>
nt maiu ()
for (ind;i<=S;j++)
printf ('ln Pass od - ");
printt("hd,);
3
Pass 1 - 2 3 4 S
Pass 2- | 2 3 4 s
| 2 2
s S
Pan y- | 2 3
3 4 S
Pam S- |2
Centiue Statemauts
Bredk aud
biak
Vsid man () Tf oe
2. 3
iut i=1;
(i=1; i<10;t+) 'Cortu'
for
6 9
break ; / Continuej | 2 3 4
printt (" yod",i);
3
statemeut is wed to terminate tha
The boak
nearant Joop iu cohich it abbec.
execuien a< the
Comailer encowter a contwue statemat,
’ When a
the ret o the statemeuts u th loop ana.
Hhen cenditianaly
cetrel is cend
skipped aud the
loop-comtiuatien pien
transtead to the
the nearet loop.
ot
iven no.
)WAP to find the no. o digits agiven
k>
4jnclute <stdio
ut maln ()
numbe: "))
printf ("Enden aug
Scanf ("d"n);
cshilo (n>o)
n=n/0;
ais
digits %d, c);
printf("TAenooe
a giveu no.
to tind es u digts
() WAP
#incudo <stdioh
{it s -0,dj
fjt n, S=o,dj nube:");
pintf (° Enter a
Scaur (" tod,kn);
cohile (n>o)
fd=n%lo;
S= S+d;
n=m/O)
pintf("The suu digits is Td", s);
frotrtor
noon
(toheu a no. is eqal to its 2eveNX)
3) PelindomeNmbon
# inctdo <stio.A
nt main()
fivt n, d,s-o,p;
pint{ ("6te adno:");
p=nj
chilo (n>)
fd=n%o;
S=S* (o +dj
n= n o,
if (s=- p) re.is Palindweno");
pintf ("he given
ele faindromeno"J;
pintf ( Not a
4) factoia
#Jnclde (stdio.h>
196id nain ()
{int fact=1,n;
pitf ("bte auuy ne.:");
Soaf ("%d,kn);
cshile (n>o)
fact - factx n,
n--;
pintf("zd ', fact);
L is not Bime no]
S) WAP to tnd cshethen a gven
ven no. is pim o not.
tt tnclaude <stdlo.h
(iut i, n;
pintt ("Ete aug no!");
scauf(d",n );
tor (i= 2; i<=nj it+)
boak
if n ==i)
puidf (" Prie");
els
puntf("Net Pie"J;
fuuctien
a set l statemects that takes
fuucien is baically a
cempuatien aud producen output
inputs, þeferm seme
fuctennane (set o intute);
Retusnyke
mpostaut ease
* Therc ano toe
) Reuncbility cau be reued osa aud
One thefuuction n definad, it
aqoin
(2) Abstaction
uucten inyn pogam theu gou
the
u ane jutwingabout wide
I4 hos it oyko
dont have to coor
fuctien prototyhe)
Fuuctien declanatien (alho caledfuctien tethe compilen.
* fnaaenties o a
moaua declaing the
fu (iut, chan);
int
Name tho fuctien: fuu
Projeartio: )
fuuchien: t
e) Retunntybe ol
Nuumbn of pamete: 2
3)
Tyheot uametel: int
9
Tyhe pauamete 2! chan
6)
Examhls:
A)WAP to add toe nos.
# incude <stdio.h> IFuuction Dalaration
ut add (int, i t
);
f t a, b, c ; nos:");
paintt("Een toe
scauf (" d hd",a,Lb); /Fuctien Call
C= add(a, b); Argmerdks or Arqumets
pintf ("/%d', c); > Achual Panametev
finitien
/ Fuuctien De
i t add (iuta, iud
b)
Argmect or Paameten
>Formal Parameten
C= a bj
petun (c);
otputs:
(B) Check the t#ncludo <stdioh
#include<stcioh> char fuu (J
char fun(); (2ehnn 'a';}
j t main() lct maiu()
fchar c =funl);
fchar c-fuo;
paintt (chanacter is : hc', c):? 2cc
paidf("chanacto is
chan tn )
(hetn 'a';} a
Am:chauacte is :
Aug chanacten is:a
au cinplicit
Cay acto as
fuuctien delinitien itse euctien is dofined
x Fhe So, in cane the
fucten deelartierm, paoganmat skip fuuct
be<ore beig uncd;the
declanoten.
3
# include<sttioh>
int main ()
chan cfuc);
puintf("chaater is %c,c);
chan u( )
etn 'a';
cenfliching tyb fo fuu
Asiever:
setnn tye afuction is int
* By default, the
cexsiste block code ehich is caable
x fuuctien definiten
penfo ming Seme
se sbecific tak
b)
int add (it a, j t
etun (atb);
declana tien aud de<inihen
Paramete is a vaniable cu the paameteENECLved by afuuctie
* Parameler:The
the tuctien formal that gets
o th puameten
is he actual valee pnameter prned teafuchen]
Arqsste fucten
Rarametey
Aetuat Panamete: The
Pared to te
CALL By VALUE
Hens vauto of actual parametery cel be copied te tormal prametey
valuba
Oud thONe too panamete an Cufteeut parametes store
u diffeeut docatens. b b
#include<stdio.h> 2 2
sed fuu Cint, int);
eid main ( )
In mainC) In fut)
iut a=1,b=2 )
fn (a, b);
puntt("a-%d, b=%d"a ,b); q=1, b-2
u (iut a,int
b)
jait
a =2 j
b= 1
CALL By REFERENCE
pnameter refento semt memor
Hene both actual aud
formal
the formal paametex tcol
!
chaugemade to
locotien. Therefore,any panameten. Heu instead assing valuen,
get refleted to actual
Coe pa addrese.
#include(stlio.h> t2,
int x);
eid fn (int *, ka=looO Rb=2oOo
19id main ()
P
fiut a=1, b=2;
fun (la, kb);
pintt ("a=%d, b=hd',a,b);
o0o200o
Uoid fun (iut xp, it a) Oulput:
Q=2, b
*p =2;
Address a
meaus "Valne atp'
contain addre sses a
anigbles cohich
etey ano
a csho lo number. contaius addren
cothich
Poiter is a veviable
Quethn waiable
se cuosien. an a loop.
callingitse in a act
Fuctien but it au
- looping
staBement, tatenut,
is neta neit- ebe
- It definitien,most coe
fucten
- In reavie fuc tion.
umberunig
WAPtfactial
Exaupla()
# nclude <stdio.
int fect (iut);
Led moin ()
ut n, ki
Scaunt("%d,An);
k= fact (n);
prindt("%d, k);
n)
Jnt fact (int
retun ();
tunn (m *fact (n-));
}
Sun ol umbeu upto n
Ex(2)
clude<stdio.h>
# in
jut am (int};
oid main ()
fint n, k;
Scauf("d", Rn);
k= Sum (n))
plint}("%d', k);
int sm (int a)
{if (a==o)
etn(o);
ele ;
tun (a+ su (a-))
series using secuoien.
WAP to print the Fibonacci
Ex() include <steioh>
int fib(int);
int ma'n ()
there. tems:");
pintt(rn Ente
Scaut ("%d", n);
fon (i=o; j<n; j++) (z4)= %d", i, fb a)):?
(printt ("nFibonacci
dnt fib (int mum)
{t (num (=2)
else fib(num-2));
Iotuon (fib (nwm-i) +
Sterage Classes
classes allos wy te chauge some attibutes t
Stoage
variablee aud fuctiens.
-’ Cau chaye exteut ol a vauiable
proga
’Cau Nestict avadablo o a fuuctien toa single
negister
staic
extern
data tye vaniable-namintialvalu]
Sgrdx: stvaragclan
DAuto Steag Clas
autematically created coheu ucten is
-Ate niabla is semeved coheu fuctien is teminated.
Called aud automatically local aia bles.
-De<ault storage clan for int a;
aute nt aj
Registen Storage lass a CPU egister fo
Tequest to cenpilo te ue
By using t, e
(RAM) allbcatien.
Uaniable dtead stack are heavilg (frequaty)
auce -or vaiable that
- Improse prform
ed.
ddress egister iable uy Negiste de
- Cannet take ce cat
net hawe addresses. mee
egicter int a1;
(
3) Stote storage Ctam vanablo to static.
exteut ol a local
Osed o 0naae vaniable fona pantculaa
-Used to etain value d a
fuuctie call.
include <stdio h> Vaolo
Examale> tt
a=lo, /Global
h
tt inctudo <stdio t
1761d hikeC);
vad hike();
Ded mainC)
1beld mun C ) ILocalVaiable lpintt(a=%din a);
iut q= 10; hikeC);
printt("a= lhd n", a); prindf('a=%dn,a);
ike l);
hikeC;
printf("a= 7d n,a); bod hike()
qhike); puntt ('a=%d 1na):
Ueid hike () a+t)
iut a= 1o; l/Static Vaiabl
static
pintf ('a=%d n", a)
att)
Outhut- a= 1o
a-lo
Outt a= 10 a=l1
a= 11
a- l1
a= 11
4) Exterm Sterage lasses declaned
fom te comilor thata vaniable i's
- Used te in
another c-fle)
du Som external file
-Prog1am Example
Ale2.c
# include <stio.h>
t price = loo ;
ijt
13eid hikeC)
extem int pice ;
exten ued hike O) puce t t)
iut main ()
Lpindf(°Pice =d n' price);
Aike();
potf ("ice=hd \n' pice);
eton o;
Output bice =loo
Pice= lo l