You are on page 1of 14

and eploin

ct PTo9am
. w r i t
a Sample

ubing System

claw Porogram
8toHc void Main (SHingI aas)
Corvso Le Tite inu" Hello, LooTlolJ3

ds
Stautement at the toP OL the l e
Th&using 8ystem to
direcHve,
Tt etils the ct comptw
called
colled a using whfcb Contains w qu
wsel
namtspace,
wse
thtSystem
dhe corsole, înput
clouseS a n o
Hethods ke ooTking 9ith
and othut utings
oupuut claus calleol
a ne

Th clauss program ine declatus blocks


the undamental
building
PTOgramclaSus a data anl
and thy èncapsulatt
pogoom%
oy
nto rusabl Codle
behavioT
dingle methcd
Prooam claus,theu s a

Insidle th
th entty point el
ous
Hethod s
called 'Hafn' .this
sben o
pCpoum,
aund it i what 9 s Cecuuted
TUn O ul p o 9 a L m .
,aun QHLay
The Hoin. methool takus a ginale arQumert
iopHonal,bud it
called 'aqs. This avgamend
Shins
SHings
mandline -agtuents
cOun be u s t te passing Co
to Ou po9arm
LEnt s wbee
I h C orsole. iorfte Line ("Hlo, uoold)
4 Tht

the 'uoríteline muthod


he happes.Thfs ne caUs
maoic
utbe &hir9 ttello
the Con eole' clab, cuhfch oritts
O nuouine
anol a d d s a
O0Ttd |" t o the console
choacdo a t the endl
shy namebpa.ces a u useol
o. what
In CH
ae NamspacL
OL namtSpace is a 00 uto 9Loup rulatd c Explain
othur typeb unto a named Containu.
untuyaces and
Namt8pace as a looi0ol Con t a i n u kei oTqani 3ing ard
A
qrouping cocle
Hert oue a why harntspacS Osu usqul
jeo
reasons

have multipl dlase


d. Parevent naming conlic you
Saume noumeyou Can p t dhêm úb dfLix
uoith a e the
namespaces to
avoid namng Conlic. This maks t
casfet to maun age and undustard yow Cod

ImpoDve Code adobiutyBy

nto hamts paces y o caun ipiovt dhe


gdnigingradobilt
Tgdni3ing Oue Cool

Code. you can ouickly gee hen classts asu


Oyow
rldttd to each othu and Lohat t h i r puLpose i .
By using namtspaces you con ancon3ul
3. EncapSulate Coole
ak yout code and mak it casiU
to TEUSe. you
ecutt o namuSpa.CeLeO paxttclot 8 t Q
Can
euse that paumpa.ce ün othe
uncH ornalityo Ound thep
Co dle
Oydue
pauso

namcspoce y Name6poLce o l
claysMyClass { bodia
odu

hame Space MyOthueNamts pace


class MyOthu Clas f

uttwo namespaces
dyintd
this eomple, we
ve
In

and My0thetlamuspaceThe s t namespa


Mylamt6pace namtbpact
the Second
Corntain% a cla cblled MyClass', and
Contoinb a class coLed Hyother Class
Lbl Uhus ClasUb in othut
To pass a CodL, uot Cou
yunce the ncumtspact dtke h i s

sing MyNamtspa.ce
clauss Progeum {
etaticvofd Hain (&irgt1 axgs) L
Mycla pnOict ntto Hy class t)

Jn this
Codle, uoe ustd tht 'using' direuive do dnpot Hu
MyNameSpac
nam &spa.ce, This allo ws ws to T to t
clas fthout hawing to i a t y J t rane uoitb
Hyclau
Ahe namespaLce p i

3. Explain Vaiabus E x p e sions ubed in c pcopaoming

In C H , a Vasuablu i a namtd Uo cotioD in mmey Hhdt


8terts a valut, Vadiabls a Ubol to &te 4 manipulate

olata
ain a Pooam

unt

cA
CH Buppeu5 a v a i c o l a t a t y p e vardabls, dnchuding

bool Booltan (uealse) valuu


unbiontod 8 bit inte qw.
byt
Cha'Sineu unicodlt chasact
decimad Decimal numbu e i t highu pecisfon than

oat c double
'douae' : Doublt prcision loating poînt numbu
'oat' eingle ptdsioo oatng-point numbu.
fnt stqned 30-bit ínttqu
long 8fned 64-bit irutte
tlo- bit intthu.
Short:signtd Chaack
shing' A 8enuen
un C to psom operaHors on vauabu
avu ubed
EXprusions
Values. An eepiusion con
caLn be aa ei
aimple
neuob
produuce
and
and vasiab, or a Combin autfon oopeHCLtes opurane
vaues, a

t3 x

@q fnt my Reblt

vasuoble calLd 'myRsul


codle we've declouteod
a
In his
i t t h t Uclut o
the erpsioD
OT typ
0T typ 'fnt and assigntd
UssTon Cvaluaks '
utoIH becaust
becaus
+3H Thfs etp
us pOuemcbuee
tht addiHoo
the multtp licoion
Ct 8 p p e a

in tastt uused to p pe
kpTUsions
8uch as athmHc,
OpeLauteis
8uppots vaious o peiates
toenuy opeatets.
logical,bituoist g
Compwúson
in C bu t, , * l ,7.,
-

ommonlycLsed
opeLatets
Some
5 A,9: ,chc
4,1,A
==, ==
>,<=<
au ndaumental Conceps
OveHau Vawuables and expHesions
usedl fn tOTitína
4 asu ettnsi vey
fn C pooammipg

Procpams
CH.
tht dtisíon statemuns usedin
Eist and explaîn
8tautemens that
N a i ng
Suppo Seveal decision -
bloch o Code
to Cecute a pauHoula
alloD thi poqHam
Outcoot a conolitfon.
bastd on th ot
8tottmenks ún
The decdsion -

Hakin9
Statemunt i ustd to ecicute a
statument The
i
poticulau oncltion ds tiue.Fhe
blos o cocle a

Sy tay!

Co ncdition)

l Code to ccLcue f Conl Hon is Hue


clst Statement T h u k -elst toknent us td o eceCLu

codle a CondIHon is tue and anothu


bLock oL
ont

tht corolftHoD us alse,


codlt
block Ok oct df

SyntaX

th CcondlrtHen)

Code to ececLtc Condi tion s u

elst
blodqold.oiso oc
tt Col toececuue f Concli tiop is alst,

s t a t m e n s : Tht nsted i&tatemens is an ik


Nes teol t CLbed eto
cubed to
&tatermunt, Lt s
Stattment i n s i d i anothut i
ttst mulHple condftfons.

Suntax
CCondttora)

Com dfon i/atd o an lovpnta a d

botth Cond tiors aL tHue


tlCode &o excecute Jol

Jest
else-i Statmunt: else-&atment is utd do
Th
t t nsttd
multfpl Condlftfon.I t u s f m i l o to
unt aereqa udo
Stotement b u t mee Conciseu

Stntax'-
Cco noliion
e o o aoiigautqlduay argaadubd
ll Cocle to ececute Cond?Aon1 fs e n

ebty (Londi Hon aL binplo id ce


tl code to execute Condi tion tue
elbt

CoclL t o eocecuu both condditfors o u s e

5. what o oant dctapes


olotatypts 2 Erplaf'n diseAn datape usd

ubtlto clcssTY vaiouws C


VOO p o dos
da
Data Type ousu

useol in a Pogaun
data tupe d a .
that cah b con botd, a
that a voiable
the typt o valu
hat caun be peyermed on thou valu. In .
Operfos
butlt -in- dcto pe OVai lobu..
thee ale Sveou

Tepesnt disnt kinos ok dLoor

ustd data C
the Common
nly ps t
pcgtan
ming
1.Value Types
Vaue t Oe utht basio olata tps n C# hat
hold a 8fnel vaue .

Thy ouu &teud i h


piooram
Stack, shfch makus then aster to access Sorne o the
Nalui typs î'n Ct
Boolan Kpesens hue or alse volutb6
+Byte: Repusenus a 8-bit unsianed
B y e Repeusens a 8-bib, aignol inta.
Cha :Repusehs a 8ngle tonicodu charaut.

Decimal: Repesents a decimal numbut wftth 98-99

3ignigicant dliglts
*Doublet Rep>esents a doublu - pecdaf o n Loaing-
loaing- opoin
numbe.
Poirt number
floot epesuns a si ngle - peciaion ocing po

i n t Represtns a 30- bit Stgned Inteq.e


UJnt Repustns
3D- bftuva19neol snlequ
Peptusents a 6H-bit Sfqned Irtegee
Loog
a 64Bit unsiqnid mttge
Rptbens
OLong + 8tontd I n t a
Riptsents
a lb -bit
xghoTt
usighd nttgu
Repus Chts 6-bft
she
9. Rence Typus UPs dtot hold
dlata
as-uL Complex
P uncL Types
memeyocatfon
coheu th
to a shich
uinc
the p09pam htop
ae &tettcl tfn
Steid.They aLccUs
Hhan vallue tpes
to
makus them &lousL
au
in CH
n tups
Som o
he T
any type o ojet in CH
objectRpesths
d chaactes

Repiesens
a
Cesuenc o unf Codle
S t i ng elemerts o thu 8ar
coUletion oL
ReptoS ents
a
Ava
data dup
a wse-deintd r ence Type
clays RLptueny

I n tsa ce: Rtptesens a -dui ne


dui fndtoce Type
unidm yd m
3. Poin Tpes
o Vaviab
Point Tpes
hold dhL memey adldes o
ous thy Con be
useol fn
ubedl iD C F P o g a u m ming
Thty ate mastely
dongeows and can Couse mimY
Voiabl o a Specic
to
RepUsens a point
poink
data P
Command asionmunt 8tattmens as Ubcod ein Ctt
6. Hoo
aie uhed to assign a value to
&tattmens
In C, asbignment
an asighmunt 8takment
OT a eld TheSuntoxe
a Vouable

Vari able = values


HesutVauiabu Uthnamt O hh vastiabu
ab o o r d
u the Valu hot you Loarnt tto aysin
o as gn
and 'valut
he voiable or l d

Fo eccmple 8uppese you haue declootd evas ab


coud
ype
fnt
qou can
asign
0
a valuento wing tt aysgpmunt
Stattment

NOW, the value o x eisto


can also ube assignment Gta.ttments to asion th
you
TUSuLto an epusion to aa vasuablu o7uod
int ba

u evauluaked and Uult


*
toee,the ecptesion
asigned o x
can olso be ombinid wi rithmuHc
8tattmens
AssiqnmentF
o7Loicad
Looica opuLaaos
Op op arum artt
rue int o= 10ien cnonpog abue lusune

a tb . Value o

teet, t h e valu
v aluL ok
O
sult
'a is

is
nctementt o

assined
b by Value
b a c k to
t

a
b and dh conkxte
t a u m u n t 8 i n V a s u i o u s

you Caun bt asign ment Statemins


Stakemns,
oDolitional
tsfdu doops
tinduding
fn
S Hehod casls
Cauls
Exploin hoo uuLevs and ecceplfos Hanagtsin ctt
oth an ccomple.

In Ctt buLoTs and eccCeptions OLL marag ed ubi rea

ty cotch 6LoCkb A ty- cautch blocH i s üstcd to Hand


ecpions cthat might o c c dwing t ececutfon o
Opoqamn

eg

cocl that
ntqht oaust an ccception

cotch (Exeep tion ec)

Codl to handle tthu ecceptton

Tht y blou contalns h codi dtthot might Caust an


eccepion an ecception occus duüng t h ececutisn
olock theececuution l us ammeliakl
hansevLdtotu Cotch blocs. e
Tht catch blocs Cortai rs Hu block code that handles
thecception Uht Ecceptfon paarnutteis used
acceb ne ation about thu esccLption 8uch os thu

n Lolg c

it OS

calch (Di videByZeno Ekcep ion a)

Console,toTi tel'ne C EroT te.Mowagd


CH ouse blots dhat
pouovid us onadi' can be u
estecute cod that 8hould auluoays be cxecuted,
ed, Tg0s
huho an cxcepHon ub throon not
not.
oT
osdla 1 foT

fni

N Codu uthat might cauust an


eccepfon

Catch (Excupton e)

Codl tho hondle thu The


eoccepHoo
be
bec
nally
9h
t Cod that ghould
oluocs be ccecued, O
3
providty-catch blotkS do mana 610s ond
erceptis
whtn an eccepion i
throuon, hecicuHHon s iasiy td
uto h cautch blods, ushfch contain the coluto
honddl
th eccepion inally blotk the con be usd to erLutt
Code tat Should &lsouysbe ececuol
8. hich ae thi ftuaion Statemens wtd in
tpopiouro ming?
Ezplaln 3. do-
In ctt útetaton &tatemens ase used to
Tepeautdly escecuk
a block oL Code. Thee ae 8 main ctpes O teHaton
Stotements in cH
1t0T 8tattment

3. 'while Statement
3. 'do- ohile' Btatement (oqda ovio)dauos
4 o T loop: H OT Loop Std to execuue a block o Coc
aiocecd
cecd numb o t m s .t
onbies,
consi&s o 33 pasus
po

fritialigatton Conclitton unuLmartolLcemert


8yntax i
nailor
to(fniialigaion Copdttons inotemerdt|deo ement)

t code to be qeniatd Juoo denuu o


l dm
Loleul
i ececutd ot ottu
The initializatiorn &tatement onc

at ttu
ht loop t h i Condition ci checkLd
beginning o
ecLch tHatton, Qnd uthe inotement dacrermen
beginnig o
thu end o each dtuakon.
Atalernertts ececuuttd at
ib ustd t o erecute a block
. while lpop; A 9hf le loop
condlitioD iS tue. e
as a cetafn
O Cocle ab ong
Suntax
(o0
while (ConditHon)
Coole ttD be ececuttd

The Condition is Chtckid at dttu beainoing o} each iteatton


is
ús thue, thL codle
in t e loop i s exècuked. t
Tit
kaUse the doop t e m i n a t t s

A do- uohile ddoop us Simila to a ehfle loop,


3. do-ushile doop:
is checkid at the dnd o each
ercept
eTcept that the Conoli tion that
beginning. Thi's
means
at the
iteratton inste.ad o least
l e a s t Once
Once,
is ececud at
the Code in tht loop
b unitaly tsue
Condlftton
ushtthe the
Yegatdles o
o oUt,
Sqntax 1 do t
t Code to be ececuted

while(conclition) a llrolsoll
The code in uth oop us ececuted ust then thu
Condlf tion is checked I
toittate
t
S t s aulst
is túe ,
thloop Coni
the loon
the loop duMiDat

8. which-a the iteiation BtaremensUbld in_CH


9.write a CH progam tto Ch eck whtthu t u umber r e
NO
eaal
tom st i 8hoh oT t
Numbe s
CHint cculedd etongnumbe 8um ot the
ackorriod o ts dlfafs &s eubl to numbe isew

sing Sustem

8tac nt fatettol (int'n) d

Goa
n o)

Tetuen 1

elt

Tuusen n Pactoial (n-1)3


Llou Jo obtolfer logton. gpa
qoodaio-ob :qooluade
1

gtaHc bool, Ie &hong bnatlona d tod


nt n)
is o
unt 8um =0ono
d
int emp =ns ychideuo
shile (tem p!-e)

unt olfost - temp .10


Gum Pacor iau ( aiai)S
temp = 1O)
Tetun Sum == D

Stottc votd Main (&Hngt T ass) og


Console. Oritel" Entoi a numbet to check s i t is 8hong

int n i n t , Patst (Conbole. Reod Line C));


Ts &itog Cn))

numbet", ); a
Gonbole.Oriteline("io} is SHong
a

else

numbet,n)
i not a 8Hong
Console.Oiteine
o

Conbole. Readkeyc);

Sum e column Suum in o

9Titt a c proram Compute Touo


1o.

Qiven aiau

wsing System
ClasProgram

Static void Main (Shing CI axqs)1


aset-111,0,3j.{5,6}
t.s.97
int[ J
rt num Ro uws = eut ungth (o)s
au

dnt num Cols aer.qetLength (1)

heuo
intnuumRous
nt CI Tou9 Sum
=

Colsum new nnum cols


dntt
i++)
o7 int i=o; nuroRows ;
ic

toT (int j-ei je numols jt*) {


TowSumli]+ = asui,J3
colSum Ci1+ asu[i, ]:
Conbole. WritlintRoo
8um ")
t0) fc numRows I++) o
ko7 Cint

SumCi]
Console. ori teline ($ Ro{1:1); {To

Conbole. Ositeline (" Column 8uum: "

oT Cint-o j mum Cola j*)


Console. OriteLine (4 "Column fi+ s: lolBunEj]f

You might also like