[go: up one dir, main page]

0% found this document useful (0 votes)
24 views15 pages

CP Assignment 1..

Uploaded by

mujeeransari96
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
0% found this document useful (0 votes)
24 views15 pages

CP Assignment 1..

Uploaded by

mujeeransari96
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/ 15

FEL204 (C prograrmming)

Rizvi College of Engineering

Assinment Na, 1
Flowchart to
Qa Define a Flowchart and Draw numer
d Sum ot dgits of
ind an, accepted of
’ ) Fow Chart is gdepietor
torial
ial a epreo entati on
conist of various
the code 3aro Lt Dvals connacted
and
, idmonds flow. of control Theoe
line ect angleshowing
by aroUs he
tthe
by
symbols epresent
mating
as imputtig
input tig,
diffeent steps suhdecisioha and
dato peforning
Oteeling openations ,
()
atpttlhy
resultS.

START

TNfUT

clig n 210
No sum= Sumtdog
n0 n=n1

Yes

PRINT SUM

END

Page 1O0
C2j wite a Program to find Largest ot Bnumbere
using Nested rf else.
Irog ram8
#inclu e stio. h>
include cconi o.h
int mainl)
fchscr (
int asy ,zj
D;
Priatf C Entex Three numberS ; ;

if Cz79)
fCzz 2z)
Prntf (u\n Xd is the brgest numer, )j
else
PrintfCwn xdis te larqcst number.z);
ele

printf(nn
else
dis the lanaeit
number.9;
Printf ("nds te Janget
Jangest number," z);

retun 0;
Output :
Enter Tkee u mbess i +
the larqest numbey.
Rizvi College of
Engineering FEL204 (Cprogrammning)

in C Explan ditfenent data tye modAherg availtle


SData type Language
modifex S ae used to mod) fy
the behaniou and of th data ypc
So ma ot stor
(0siqnedthedata : typ used in C latguagei
T+ sigredto int 3
dec lare the datattyp
uted to
s used
Sizing the siqhed zey word
signed iteg
tpe isis siqnèd:
shan
naiables ". Bu default nt data

rUnigned
Uage Unstmgned i t y
Thas
Can
moofiets is usel to de clase vabiable s that
Dnly hold nonnegatie Values Czens anol
Poste
G) Shot
Usage :: short nt z?
t to declane a shot Tutegesn, whil
typically uges less
ess memoTy than egula int.
jnt u9
declane a long ulasntegn , ich bypially
than reg inte
() Long dong:
Ttroduced in caastandad ,Jong long greate
Kange
(vi)flot
than a egulas log?
Usage loat f'
Tt g used to declan
whi sastoge poiT
floating pont
humbet numbes.
(v) Douse
tsage :double d;
T+î9 used to decae double- pre cision loat ingpoit vaniable
dou bled
Tt N ued to decat
polnt Vaniable
Usageo doble piecuson flooting
Page | 101
Q-j Explain Condrol Breaking statemanti availabla in
CAangu age
C
ak the ge ) Contvol breakhna statemets
to ateon nor mal low of codo
Owteh cade statemmeito: They allow you tohinexit
-
rom A oop ship ceotaln 1teratios o jump, toeoI
olitfot Pat of te poogtam The main contol a
brealag statemet in c
[DBYea tatenent:
The byeat stotement n C teonin ates te clogast loop
o swltch statement. When
enCewnteed
-the Lo op o swtch amd contiues break exits
Commoy wed witt cedienal to exexitecuttng
Loops
aften it
(7) Conttye stotene eanly
Tle cotinue statement inC
aud mones to t e next. Whensies te cumet oof Hensto
exe cuted
to e next doop itenation ,by pa3s) g ,cortine jump)
It's handy for stpping cetain Condtons 2ubs e uet co de
ewitot
ex itbg the deop.
(iT) Retun stooteet:
Tle tcdtutn statmet inC exi in
avalue back to the calleswea hit, atuncon sendng
leae <tu fun Gtion vetun prompty
t's ds0 useful for , by pas3ing 'aye tining code
fun cion. brealsg out of loops oltn
iv) Goto stotem
The qoto stateunt in
Ciunfd to lobeled stodeme
tunctio. Tt's advised againt tos
poteialto
Can be
Complicate code readibi lity st11, it
handiy
erroY.
tor niting nes
nested
ted loops or kandiling
Rizvi College of
Engineering FEL204 (C programming)
Qs) Write a Program to print the toactorial of
te gnen nuimban.
’Programs
tinclude <st dio. h
tnclude Conioh2
int main
int f
Psintf ( Enten numbe:;

Psintf( n factorial is ! d" ,f);


retun 0;

Output
Fact)a
En ten a umbes i
)8 !120

Page | 102
Q6 Wite a progran to add two nu mbers ustng
Usen Defined Fun cti on.
S#nc lut
int add (nt stdio. h>
int addl inta,a,lnt b);
int b)
return atbi
fut main()

int a,9,3 u =0;


Prontf ( Ente two
Stanf C" xd xd numbers :"):
Sumcadd (a,y);e
Pritf(" Sum xd", sum),
Se tun O

Output:
Enter tuo num beY8
ber : 4
Sum
FEL204 (C prograrmming)

Rizvi College of Engineering

fled
gample
Qa) Enplain Conditional opeoator with,al[o
&)
’nThe conditional operato
Ohe of
Inthe operatos,
Ternay operatos , is 'Ohe of
Proayaymanig Cohdtonal openat ginen expresAon
st temert depends pdon the
upon
etu lt
a Teonouy opoatos n C
(1)7he baic syntax ot
Progpraning stateet 2:
Fspyesion stateneta
the gren test
(1m) Fsom the obone syta
conditon 2
will r e t n
and if it is falte gtoteer
(r) Fox example ,

int maln Ussildtd


it
3:4) ; /Tspraqrarn fprite 3, because
9= (a5 ? hene.
ptintflNd',)

Output
3
:

Page | 103
Q8) Dffenen ce between ohi le and do while
While Loop do while loop
Ocond4on it Checked Ostotement s executed
Statont s eaecuted fiyst then atleast once, theneat ten
Condion is ccked.
(3t mig htlt ocCurt
occur statement At
At,least
least onca the stote
ls executed z0 timed, it bet 8 execwted.
Condi tion is tale.
3 No semicolon at the 3) Semi olon at Hhe end
of while. ot wile
while CCondtion wlile Ccodi ior);
4Tf thene a single stalenad, Backets ase
brackets ane not
not reauired.
equired. equred
5)Vaniab le in condi tion i! Vaniable, may be
initialized before the executon nitialized before o
of lrop wiin the oop.
6) Odo
Contrel
)synta: led oop. eatt Cortroled 'loop
synl:
wlile Ccondio) Sdo statemen;?
FEL204 (C programming)
Rizvi Callege of
Engineering
progran
to cosplay all prima
tu 50.
tinclude estlio. h>
int m a à n l ) o t
int f ;
Prùntf ("\n Prme numbeh frorm 1to 5o;"');
toy Cint i=2; ies 50; 1t++)
if=l;
for Cint j= 2; j*j<sij jtt)
i ifCi%ji==0)
{f=0;
beak

printf ("t hd , i
3
retusn 0;

Output :
3 5
Hime umAc too 4 to 50: 2
29 3| 43
(3 23
44

Page | 104
Q-1o] Explain the follo wing fun ctions with Prpen caample
(a) st len l
Stlen() is st andavd ibrary function in c that
Tesued to find the Length of a null- teminated
string the functioh Calealates the length of te
in ity
Wg by counting the numbey ot characte end
exclu ding tthe nul- teminator \o' at the
ìnclude cstdioh>
include <string. h>
int main C)
ichay st s[ ]=" He lio;
Size_t length zstr ln latr);
psintt Cstring 17 s\n,st);
pintf (" Length : 7. zu \n,lengthdy
retun 0;
Out put:
String: Hello
Lengthi5
()strcnp()
’The strc mpll. is a bult-in lrbsany fun cton in C. thas
functioh takea two sting a agumet an compaes
these two tsings lexicog sopically
Syntas: int strcmp (Const chan tstr4 conzt chan
tr2);
Panametes: st4: Thts is the tirst string -to be cDmpared
stx: Tis is the second staing to be Comped
Return value :
It str1 19 less than sto2, the return value is less than O.
Tf str g greaten han str2, the retun valueis greates
than O.
str equal to stre ,the retn Value ig 0.
Rizvi College of
Engineering FE204 (Cprogramining)

#iitnclumain)
include daest<string-
dioh? h
char str[1"Heltlo"
cha str2 7 Hi"
cha sto 3T "hello"
int ne sult1 =str cm
int esu lt2= str cmp(str1,str2,
ìnt result 3 strcmp lstr2, str 3)3
(str4 ,str 1);
print-f ("Compavsbn of sty1 and sx 2: %d \n',result ;
Print f(" Com parison t
ot S
str2 t
and r 3;%ol
str3:/o n'
\n,e9ut3 2),
'pòntf (" Ompaison of resu lt3);
lsetn 0;
Output
Compason of str1. and str 2,3
Compau ton of str 2 and str3:
Conpans0h of st and str 4 : 0
(c) streat()
’The strcat() funcon in Cig uged for string
concatenatton I t cojl copy, of he
Sous ce strng to the of athe destination
fpend
Stying Syntoai cht strcatCchant Jodest ot. chansc)
Paabneles : dest Desti naton Sting
STC : Sour Ca str
Reten value : The strcat ) funtoo retns polntes
to t e dest string.
Example:
nclude <otdio.h>
int main ()
{ chan dest Lso]=This is
chan src B0J example.s Vn',est):
pintt (" dest Befove \n',dlest):
strcatt (dest ,rc),
psintf" dest After: Ysnd dest):
se turn 03 Page | 105
Out puti
dest Be fore & Ts is an
dest Aften !Ts is an exornple
The powt) functon in C ls us ed a ginen numbe to
spe citted powen Tt calculates tthe value sf base
aised to ethe poweh sfot exponet
Examplei estdioh>
*ncude
*nclude<math
int main () h
ba le 20
doube exponet 30'
double resu lt;
Tesult =pogs (bae,
Printt (i \n ponent;
t£ saised totha powes . t
sesult', bale,aporr

Output:
2-0 raigcd to the powe 3-0 8 80
C)cetl( k
) The ceil l)fntion in Cis used
to tnd e nallest
Integor qreaten than Aual to a gives floating
Poit numbeh.14 Younds e given ven numben e upto the
nearest inteqes qreate than oc s
Example
Rizvi Coliege of
Engineering FELZ04(C progratnnial

#include <stdio.
inclde cmathh
he
int man
{doule ne 5 3,
dou ble
PrintfC"
veturn eiling
0; t
3
Output:
celing of 53 is C-o
f srt ()
)The sert () -fun cHion n C is used to calculake
the quasne Yoot of a a gven num be-Tt ra
the Cquahe oot of a
negaine floahng point
fxapple
+nclude <dio-h
#nclude <math ho
int majn()
doub le n 25:
double syi,
printf lnhn sauane root of l t ig . JE " sn,s);
3
Out putvn
Cuane Toot ot 25 s 5 ,
(9) size of CJ
The sze of () opeonodos ia G is used to detenine
ze ot vaiabl in bytes Tt retun
Hhe mben of bytes a iredto store the
perand
Page | 106
Eromplet
inclwde estdio.h>
incude <mathh>
fint ns
doubl
Chan str l20)3
SiSi2et
zet ze-nt ì ze ot (n
Si ze t si2e-pisize ot Cpi)
Stze-tr ze of (st)
s
Printf('
Psintf("
sze of int : %zu
Sze of doudezubytes \n' ,sie-n);
printf (se of chan bytes r' siz0 f 20

Chan oray :%zu bytes


',Sie-34r);,
3
Output!
Size of int : 2 buteo
si2e of double e bytes
Size of chas
chan aYYay 20 buytes.
(b) stlws:
) The strlur funcion 5 C is uged to Convet al
uppen case lettes in a trina to lo
modfies he orgnad strlny in plae. wencase It
Eramplhe
#inchde stdio.h
#enclude <tringh>
int main()
chas str []"Hello,woRLD)";
Printt (" Driginal
Chas lowen Case
string: 7s n str);
str wr (str);
RizviCollege of
Engineering FEL204 (Cprogramning)

printt( strina Sn loweh case . %9 n ,louencase);


etutn 0;
Dutput;
Orjana strmgs Hello, woR LD)
Strha in lowecase i hello , world
The fHooyl) funtion in Cig uSed to fnd ta
ongest
to
integethat
that g
giren Abatna poirt
less tthan or eayal
numben. Tt
te oun to t e reasest

inple# include
less than or equa
#)nclyde, <stdio h
int man ()
Sdouble n58
mah h>
to æ

d= flo oy ln;
'yetnD;

Output
fioo of S& .0

Page | 107

You might also like