0% found this document useful (0 votes)
4 views25 pages

CPR Assignment Part 1

Uploaded by

divya.tekade
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)
4 views25 pages

CPR Assignment Part 1

Uploaded by

divya.tekade
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/ 25

Pace No.

Assignment NO:1. Date

1Tist the different types of operators used inC


longuage.
1)Arthemetic operators.
2) Relational operatoYS.
3)L0gical operaBors.
4) Assignment operators.
5) Increment and
decrement operators.
6Conditional operators.
1 Bitoise operators.
8)Special operaBorS.
.Stote diferent datotypes Supported by C
language.
Datotype Ronge Sjze
Uhsiqned Char 0 to 255 8 bits
Signed char -128 to 27 8 bits
Unsigned INI 0to 65535 16 bits
Signed INT -32768 to 32767 J6 bits
Floot
Fractional No. 32 bits
Double
Fractional No. 64 bits.
(Page No.
Date

.State Qny four Relotional operators in C (coHh


their uses)
i== (Equal to): Checks if the values of tio
operands are eguol 0r not. if
yes then Cond)ion becornes true.
= [Not equal to) : Checks if Hhe yalues of 4uo
OperandsOre equal oY not, if
Values are not equal then
Condition becomes tYue.
iil>GreaBer thanl: Checks if the value of left
Opexand js qreaBer than the
Value of right operand, if yes
then Condition becomes_ true.
ivl<Less than:Checks if the Value of left
operand is less than the yalye
Of Yight operand, if yes then
Cond i4ion becomes true.

Define the term.


TdentifierS:Identifiers are hames given to program
elements Such as Variables, Constant and
functions.
ToKens :The smallest individual units in a cC
program are called tokens.
Page No.
Date

.Oescribe Scanf oHh Syntax and example.


Sconf:
IH iS uSed to Yead dota from sBandayd input device
iLe. Keyboard
Syntax :
sconF ("ConHol String". yar1, vor2....2Von);:
Exomple :
int a.bi
floot C:
char d:
SCanf (" %d %d", a, b):

Wite an algorithm and flouwchart t0 Swap the


Contents of two Varigble.

Algorithm
Step 1 : START.
Step 2: PRINT Enter the two num bers"
Step 3 : INPUT a,b.
(Page No.
Date

Step 4 X= a.
Step 5 a=b.
Step 6 : b = ,

Step 7 PRINT a,b.


Step 8 : STOP

Elowchart .

START

PRINT "Enter the two numbers/


INPUT a.h/

b=x
PRINT Q,b
STOP
(Page No.
Date

Write on algoriBhm ond drau) flow chaft to find


whether entered number js even 0Y 0dd.
Algorithm :
Step 1:START
Step 2 PRINT Enter a number'"
Step 3 : INPUT n

5tep 4If mod 9 =0 THEN


PRINT "The number is even"
ElSE humber is 0dd
Step 5 5TOP
Flowchart:
(START)
/pRTNT "Entey a number"/
INPUT N/

<nmod 2

/PRINT "EVen'"/ PRINT "Odd"


5TOP
Page No.
Date

Nrite a C prog1am to ocept Thyee numbers ond


Find their average
# include < Stdio.h
# include < conio.h)
int main (

floot number 1.number 2number 3, average;


Dint f C" Entey three numbers \n" :
Scanf C"% FZE 1E" numbey 1.A number 2,! number3);
avexage =(number 1 + number 2 + numbeY 3)/3 ;
print f( Averoge of /{ 2£2£ \n", number 1, number2,
numbex 3, average);
qetch():

Output:
three numbers : 15 |5 15
Enter
15,15,15 is l5.000
AVeroge of
Page No.
Dete

Wite aC program to ocept Three numbers and


Find theiY avefage
#jnclude <stdio.h)
# include < conio.h)
int main ()
floot number 1.number 2.numbeY 3, average ;
printf C Enter three numbers\n");
SCanf C%f 2f 7.E", Anumber1. number 2,9 number3);
averag e =(number 1 + number 9 + number 3) /3;
Drint f( Average of 4 74 2.f\n number 1, number 2,
number 3, Qverage ;
qetch (:

1.Expgin oith suitoble example preincrement and


postincrement operatofs.
PYeincrernent (ttx)
(t+x) is Called 05 0 preincrement operator.

Example :
If X =4;
Then y =ttX ;
Fage No
Date

After the execution 4he yolue of y =5 and y: 5


As the nome (preincrement ) IH fitst incrernents and
4hen qives he inCremenied yalue.
Post increment (X++)
X) iS called as a postincrerment operaBor.
Example:
If X=4:
Then 4= X4+ ;

After the execution of above sBaBement the yalue of.


yzt and xX=5.
As the nome Cp0stincremet), It fitst gives the
previous Value aod then increments.
Page No.
Date

Oistnguish beBaeen ompiler and interpfeter.

Compiler InterpreBer
A Compiler translates code An interpreter ranslates
from a higher -level prog-Code coritten in a high
Tamming language into Jevel progromming language
machine Code before theinto machine code Iine-by
pioqiam Yuns. -line as the code Yuns

Compiler takes enHire prog- Interpreter toa Kes single


1om as input instucion a5_ output.
Interrmediote obiect Code NO intefmediate objeCt
Is generoted Code is geneiated.
Conditiongl Control statemeConditional Control Statemen
nts ate execute5 fasterts executes slower

Memory yequirement is Memofy fequired is less


moYe

Erfors_ afe displayed afte Erfofs are displayed fof


entire progiam is checked.every instíucHion interpreted
Example: Ccompiler Example BASIC
Page No.
Date

Assignnent 2
Stote use of Continue stotement.
I is used 4o bring the program (ontrol t0.
Hhe beginning of the loop.
ilLhe coninue statement Skips Some lines ot
Code inside the lo0p ond onHinues coith the
next iterGtion

iT ends the CUYYent iHeration of a loop


ivJ 05 moinly used for a Condition s0 that we can
Skip some code fora particulaY Condltlon.

Give Syntax cf sutch (ase sBatemeut.


Syrkax
5ultch (expression or Variable)
COse Value-1 Block-1
break:
COse yolue-9:Block-2
break;

defoult: default - block ;


Page No.
Date

Compare whie ond do ohile Statement

while do- while


I js entry ccntrolled I is exit CCoHclled loop.
loop

he loop PXecutes the state! The loop AXeCutes the


ment only offer testing Stoterment at Jeast once.
Condition.

The loop terminates if the The condtcn is tested


Condition becomes folse. before execution.

There is no 5emicclon at Thene is Semicolon at the


Hhe end 0f while staBernen end of stoterment.

IH executeS the stotment


I Checks the conditionI
before executing statement|at Ieast once befone
|Checking the condition
Page No.
Date

Write a C progrom to
numbers enBered by userfind sum of natural
#include <Stdio.h)
HInclude<
int main (0
Conio.h)
int n, L,5um =0 ;
DIintFEnter a
number :\n";
SCanf"d",&n);
for (i= 1; i(=n; +ti)
5um t =i;

pintf (Sum= d", Sum):


qetchC):
Output:
Ente a number
Sum =210
Page No.
Date

Stote ony tour contro sto-ements


iiH is js a selecion sBotemen+ thot al|ows
more
than ône possible floc) off Control
il- else : I4 is Used 4o decide whether the part
0f the cOde wil bÇ
i break: It js used to execu ted.
biing the program
piogiom (orHio
control
OUt OF the loop
iNContinue IH is used to bring the piogiam (onio
to the Start of the loop
Exploin while Ioop Oith syntox and eXaMple.
The ohile loop 1S an entry Contro| led 0op 5taBement.
Hhe while oop iS used to repeat a specifiC
O code an unknown number of times untilbock
a condiion i5 net.

Syntox
While (test condition )

body of the loop;

I check5 the condition before exeCuting the


Stateme nt.
Page No.
Date

Example
find the sum and produc
Write_aC pIogiam_to
digit 0f a yse entered num ber
o all the
# include <stdio.h)
#include<conio-h)
Void moin()

int n,sum =0, pr0duct =1:


DrintfCEntex a number \n ):
Sconf C"Zd" n):
while (n: 10)

pioduct = píoducB *n,;

getchC1
Page No.
Date

breOk and (ontinue Sfaterment


Stofe the function of
break.
enclosing.
ferminates the execution Of the nearest
which 14
IH o while 5taternent in
do for. S0itch,
appears.

out of a Sing le loop at 0 Hime


T! Can only bTea k

It stops_ the enfire prOCeSs of Joop.

ConHinue:

to Skip_ the (UTIent iHeration


I! is used in logps
gn to the next Heration without
ond moVe below the continue jn
executing the StoBements
Hhe loop body.
Contnue staferoent Skips some lines 0f Code
The next iteration
inside he loop and continues cwith
Page No.
Date

. Stote diHference beBueen while ond do wbile loop

while do cohile

TH is entry contolled oop T /s exit contolled loop

I checks the (ondition I executes the statement


befofe Pxcuting statement.at Ieast once bfoye
checking he condition

There is no 5enicolon at There is Semicolon at the


Hhe end of ohile staBe-end of statemen+
ment

9. Describe the use of for Joop with syntax Ond


expression.
1The foT l00p is used to iHerate he staBerment
0Y a port of the program Several HiS.
2I provides a funcHionality /feature o repeat a
set of statements a defined number 0F times.
I is o 4oim Of an entsy contolled loop
Page No
Date

Syntax:
for (inHiallzaion est Condilon, increrment o
decrement)
Body 0f the loop ;

Expiession:
progiom to print EATC five times
Write a(_
# jnclude <Stdia.h>
#jnclude <Conio.h)
Void main)

clIscrC):
for Ci=1;i<= 5; itt)
prindfC"E TC\n")

Qutput:

EATC
ETC
ETC
E TC
ESTC
Page No.
Date

olin how Switch Stoterment is used in the


Explain
00 (9m instead 0f e if -else Statements with a
Suitoble Pxomple

A4witch Stotement is Used in a piogiam as an


IOHernative to mulHiple if-else stateme nts when
theye 01e muHiple CondiHions to be Pvaluoted against
o Single Voriable.

povides a
T! D)OVides moe concise ond some Himes more
RICient ay of hond ling SUch scenaios
I! elseiS used to test he descisions while a
Suitch Statement 0s used to test the yo lue of
the qiven Vaviable ggainst list of Case Valve

Exomple
Using if - else staBement
HCday of week.equals ("saBurday") I| day of week
fquals ('sunday' ))
dakatype= "Weekend;
3
else

dota Type ="Neekday


Page No
Date

Exomple:
numbers
Nite a progiom to disploy n noBural
enBered by useY

by using i-else Staterment


#includeStdio.b)
#include<Conio.h
int moin()

int n j=1;
Drintf(" Enter a number :"):
5C0nf"d" Sn):
ore :\n", n;
Orin+F C First Zd natural numbers

while Ci<=n)
print f C" Z.d"i):
itti
2

else

Print f (" Involid number");


qetch C):
Page No.
Datn

lBy using Switch stoBement


#jnclude stdio.h
# include <Conio.h)
iDt moin
int n. i= 1;
pintf "Enter Qnumber :"):
Zconf ("Zd"_ An):
Drint f ("Fist Zd ngtuíal numbezs ae : \n" n:
SauiHch Cn>o)

Case 1:
while Cn)o)
prinB f C"Zd" D:

bregk
default : printf (" Jnvolid number");

getchC):
Page No
Assignment 3. 2

Stote the adontoge5 of funcion,


AdvonBages:
1. Modular O Structured progiamming Canbe done.
21 Debugging becomes Easler.
I is easier to understond the proqram cgic.

4 By follocaing top- doun ogprooch. the main funcion


Con be Kept Very smoll_and oll the taskKs Con be
designed to Vaious functions.

Explain TeCursion with example.

Recussion :
Recursion is_ a procPS5 where Qfunction calls itself

Too impoftant (onditionsshould be satisfied by any


ECUISiVe function
1. Eoch ime the function is_colled recursively It must
be closer to the Solution.
2. There Inust be Some terminoting condition,twhich.
Qill stop Tecursion.
Vedak
Page No.
Date

Exoyple:
#include Kstdio.b)
int facBorial (int no):
yojd ain)

int no,ans;
pintf C"|n Enter the number"):
Scont("%d" &no):
On5 =foctcial Cno):
p1intt C"In koctoial af Yd is Zd" n0,0n5)
qet chC);
int factovial Cint no)
int fact :
i (o= 1)
Tetuyn(1):
else
foct : no facouial Cn0 -1):
1etuin(fact),

Enter the numbei


5
Foctoial of 5 i5 |20
Page lic

z3. Write a Cptogiom to sop Contents of t00


Noriables using col by Yeference.
# include stdio.h)
# include <Conio.h
Void main (int a. int b)
int ternp= a:
b= termp;
int main ()

int x.yi
printf(Enter value for x ");
SCon f(Zd " &x):
printf("Ente volue fox y:):
Scanf "A",8y):
Suap( x, y
pint f ("Affei 5apping: x2d, y: 2.d\n". , y);
getch():
Pape

tNTit a
4 Etampleprogram to find factoio) of given numbey
NriBe the program to find the foctorialL OE he given
n. using the ecursion
#inctude (stdio.h
int factoriol Cint no ):
Void main )
int no. ans:
printf ("\n Enter the number
SCanf C"Zd" no):
ans = factorial (no):
printF (\n Foctorial of %d is Xd". no ans):
gekchl).
int factoiial Cint no)
Int foct :
If (no==1)
YeBurn (1);
else
fact = no factoriol (Cno-1):
Yeturn (Fact):

Output.
Enter a number 5
Factorial 0f 5 is 120
Page No.
Date

with yntax.
6 list any two librory functions
1. pintf):

This funclion is used to print forrmotted output to


the stondord output.

Syniax
int printf (Const chayformot,...

2. Scanf(;
This funcHion is used to 1ead formatted input from
the standord output.

Syntox
int sconf Cconst chay *format, ...):

You might also like