You are on page 1of 98

1

DATA STRUCTURE:The logical and physical representation of data is called data structure. Data Structure Linear Non Linear -Tree Array Stac" #ueue Lin"ed- List inary Tree A!L-Tree ST

De$ueue Enueue

Single Lin"ed-List Dou%ly Lin"ed-List Circular Lin"ed- List

Differentiate %et&een the data type and data structure. Stac":'t is a linear data structure( &hich &or"s on a principle or Algorith) called L'*+. A stack is also an ordered collection of element like array, but it has a special feature that deletion & insertion can be done only from one end, called the Top of Stack [Top]. Using stac" &e can do follo&ing operation. ,- .US/ 0- .+. 1- .EE.

1(a) Algorithm for PUSH operation in STACK

.US/ 2Stac" 3 4( int Si5e( Top6 -,( int !alueStep ,: if 2Top66 Si5e-,7 print8 Stac" +9erflo&8: return: ; Step 0: Top 6 Top<, Step 1: Stac" 3Top4 6 !alue Step =: ST+. Step >: END

,2%- Algorith) for .+. operation in STAC? .+. 2Stac" 3 4( int Si5e( int Top( int !alueStep ,: if 2Top66 -,7 print 8Stac" Underflo&8: return: ; Step 0: !alue 6 Stac" 3Top4 Step 1: Top 6 Top-, Step =: return 2!alue-: Step >: ST+. Step @: END ,2c- Algorith) for 'nfiA to .ostfiA Step ,: .US/ eApression( # to stac" array. Step 0: Scan # fro) left to right and repeat 1 to @. Step 1: 'f an operand is encountered( add it to RESULT Step =: 'f left parenthesis B 2 B is encountered( push it to STAC? Step >: 'f an operator 2<(-(C( D - is encountered then 2a- Add operator to STAC?. 2%-Repeatedly .+. fro) and add to RESULT each operator &hich has sa)e precedence or higher precedence than operator. Step @: 'f a right parenthesis is encountered then
2

2a-Repeatedly .+. fro) STAC? and add it to RESULT until a left parenthesis is encountered. 2%Re)o9e the left parenthesis. Step E: EF'T. ,2d- Algorith) for e9aluating a .ostfiA eApression Step ,: Add a right parenthesis to the end of the eApression. Step 0: Scan eApression fro) left to right and repeat step 1 and = for each ele)ent of eApression until senital 2 B-G -is encountered. Step 1: 'f an operand is encountered .US/ it in the STAC?. Step =: 'f an operator2A- is encountered then 2a- Re)o9e the t&o top ele)ents( top and top-,. 2%- E9aluate top-, 2A- top. 2c- .lace the result %ac" on STAC?. Step >: Set result e$ual to top. Step @: EF'T. RULES:.refiA notation:- R++T LE*T R'H/T 'nfiA notation :LE*T R++T R'H/T .ostfiA notation:- LE*T R'H/T R++T #ues:- Con9ert the infiA eApression into postfiA eApression. ,- AC <CDD Ans:- 2AC -<2CDD2A C-<2CDDA CCDD< 0- 2A< -D2C-DAns:- 2A <-D2CD-+ * A <CD-D 1- 22A< --2CC2DDE--Ans:- 2A <--2CC2DED-A <-CDEDC A B C A <CDEDC-

/ D E

+R #ues:- Con9ert infiA to postfiA eApression using stac" 2CC2DDE--Ans:'N.UT STAC? +UT.UT 2 2 2 22 A 22 A < 22< A 22< A 22<A < 2A < 2 2-2 A < C 2-2 A <C C 2-2C A <C 2 2-2C2 A <C D 2-2C2 A <CD D 2-2C2D A <CD E 2-2C2D A <CDE 2-2C2DA <CDED 2-2CA <CDEDC 2-A <CDEDC-

22A< --

0. #UEUE:A queue is a logically F F! type of list. t is linear data structure. t is an homogeneous collection of element "here ne" element are added fro) the rear end deleted fro) the front end.

02a- Algorith) for 'nsert operation in a #ueue #'NSERT 2)aAsi5e( ite)Step ,: 'f 2Rear I6 )aAsi5e-,7 .rint J#ueue +9erflo&8 Return. ; Step 0: 'f 2*ront66-,'nitiali5e *ront 6 K and Rear 6 -,.

Step 1: Set Rear 6 Rear < ,. Step =: #ueue 3Rear4 6 'te). Step >: Return. 02%- Algorith) for Delete operation in a #ueue Step ,: 'f 2*ront 6 Rear 6 -,7 .rint J#ueue Underflo&8 Return. ; Step 0: 'te) 6 #ueue 3*ront4 Step 1: *ront 6 *ront < ,. Step =: 'f 2*ront 66 Rear7 Set *ront 6 -,( Rear 6 -,. Return 2#ueue 3*ront4-. ;

Circular $ueue:- A circular $ueue is a one in &hich the

in certain of ne& ele)ent is done at the 9ery first location of the $ueue if the last location of the $ueue is full.

A circular $ueue )aintains the :,- *ront &ill al&ays point to the first ele)ent. 0- 'f front is e$ual to rear the $ueue is e)pty. 1- Each ti)e ne& ele)ent is inserted Rear 6 rear < , 3 Delete 6 *ront 6 *ront < , 4 02c - Algorith) for 'nsert operation in a Circular #ueue Step ,: 'f 2*ront 66 Rear < , L MaA si5e7 .rint J#ueue +9erflo&8 Return. ; Step 0: 'f 2*ront 6 -,7 Set *ront 6 K and Rear 6 -,. ; Step 1: Rear 6 22Rear < ,- L MaA si5eStep =: #ueue 3Rear4 6 'te). Step >: EF'T. 02d- Algorith) for Delete operation in a Circular Step ,: 'f 2*ront 66 -,7 .rint JUnderflo&8 Return. ;
6

#ueue

Step 0: 'te) 6 #ueue 2*rontStep 1: 'f 2*ront 66 Rear7 Set *ront 6 Rear 6 -,. ; Step =: *ront 6 2*ront < ,- L MaAsi5e. Step >: EF'T. D+U LE ENDED #UEUE:'n the $ueue insertion and deletion operation are perfor)ed at %oth the ends i.e. &e can insert the ele)ent fro) the rear end as &ell as fro) the front end. There are t&o types of D#UEUE:a. +utput restricted D#UEUE. %. 'nput restricted D#UEUE. There are four operation in D#UEUE ,- 'nsertion at REAR END 0- Deletion fro) *R+NT END 1- 'nsertion fro) *R+N END =- Deletion fro) REAR END N+TE:*or input restricted D#UEUE all the a%o9e operation are 9alid. *or output restricted D#UEUE operation a( %( c are 9alid %ut d is not 9alid. 02e- Algorith) for 'nsert operation at Rear end in a D$ueue #'NSERT 2MaA si5e( ite)Step ,: 'nitiali5e *ront 6 K and Rear 6 -,. Step 0: 'f 2Rear I6 )aA si5e-,7 .rint J#ueue +9erflo&8 Return. ; Step 1: Set Rear 6 Rear < ,.
7

Step =: #ueue 3Rear4 6 'te). Step >: Return.

02f- Algorith) for Delete operation fro) *ront end in a De$ue Step ,: 'f 2*ront 6 Rear 6 -,7 .rint J#ueue Underflo&8 Return. ; Step 0: 'te) 6 #ueue 3*ront4 Step 1: *ront 6 *ront < ,. Step =: 'f 2*ront 66 Rear7 Set *ront 6 -,( Rear 6 -,. Return 2#ueue 3*ront4-. ;

02g- Algorith) for 'nsert operation at *ront end in a De$ue Step ,: 'f 2*ront 6 K7 .rint J'nput not Allo&ed8 Return. ; Step 0: *ront 6 *ront N ,. Step 1: D-$ueue 2*ront- 6 9alue. Step =: Return.

02h- Algorith) for Delete operation fro) Rear end in a D$ueue Step ,: 'f 2*ront 6 Rear 6 -,8

7 .rint JUnderflo&8 Return. ; Step 0: 'f 2*ront 6 Rear7 Return 2#ueue 3Rear4Set *ront 6 Rear 6 -,. ; Step 1: 'te) 6 #ueue 3Rear4. Step =: Rear 6 Rear-,. Step >: Return.

1. L'N?ED-L'ST A lin" list can %e defined as a collection of 9aria%le nu)%er of data ite). Lists are )ost co))only used non-pri)iti9e data structure. An ele)ent of the list )ust %e containing t&o fields: one for the storing data or infor)ation and other for storing address of neAt ele)ent. As you "no& for storing address &e ha9e a special data structure in c called pointer.

12a- Algorith) for creation of a Lin"ed-list Step ,: Define structure:struct list 7 int ite): struct lin" list C neAt: ;: Step 0: Define pointer 9aria%le as start and ptr. Step 1: Allocate )e)ory for each N+DE. Step =: 'nsert 9alue. Step >: start neAt 6 ptr. Step @: ptr neAt 6 NULL.

10

# OA. for Lin"ed- List PincludeQstdio.hI PincludeQconio.hI PincludeQalloc.hI struct lin"list 7 int ite): struct lin"list C neAt: ;: typedef struct lin"list Node: 9oid )ain27 Node Cstart Cptr: clrscr2-: start 6 2NodeC- )alloc 2si5e of 2Node--: printf2RSn Enter the 'te):-------IR-: scanf2RLdR(T start 'te)-: start NeAt 6Null: ptr 6 2NodeC- )alloc2si5e of 2Node--: printf2RSn Enter the 'te):-------IR-: scanf2RLdR(Tptr 'te)-: start NeAt 6ptr: printf2RSn Lin"ed - List DisplayedR-: printf2RS Ld LdR(start 'te)( start neAt 'te)-: getch2-: ; # OA. for Lin"ed- List PincludeQstdio.hI PincludeQconio.hI PincludeQalloc.hI struct lin"list 7 int ite): struct lin"list C neAt: ;: 9oid )ain210

11

7 Node Cstart( Cptr(Cptr,: clrscr2-: start 6 2NodeC- )alloc 2si5e of 2Node--: printf2RSn Enter the 'te):-------IR-: scanf2RLdR(T start -I 'te)-: start -I NeAt 6Null: ptr 6 2NodeC- )alloc2si5e of 2Node--: printf2RSn Enter the 'te):-------IR-: scanf2RLdR(Tptr -I 'te)-: start -I NeAt 6ptr: printf2RSn Lin" - List DisplayedR-: printf2RS Ld -I LdR(start -I 'te)( start -I neAt -I 'te)-: printf2RSn Adding Node to %egining of the ListR-: ptr, -I NeAt 6start: start -I ptr ,: printf2RSn Ld -I Ld -ILdR( start -I ite) (start -I neAt -I ite)( start -I neAt-I neAt-I ite)-: getch2; getch2-: ; 3(b) Algorithm for Insertion of a node at the Beginning of in!"list Step ,: 'f 2ptr 6 Null7 .rint J+9erflo&8 EAit ; else ptr 6 2N+DEC-)alloc 2si5e of 2N+DE-Step 0: Set ptr ite) 6 9alue. Step 1: Set ptr neAt 6 start. Step =: start 6 ptr.

11

12

12c- Algorith) for Deletion operation in Circular $ueue# Step ,: 'f2 *ront 6 6 -,7 .rint2J +9erflo&8-: ; EAit: Step 0: 'te) 6 #ueue2 *ront-: Step 1: if 2*ront 6 6 Rear7 Set *ront 6 Rear 6 -,: ; Step =: *ront 6 2*ront < ,- L MaAsi5e. Step >: EAit. 12d- Algorith) for 'nsertion of a node at the End of Lin"-list Step ,: 'f ptr 6 Null 7 .rint J +9erflo&8 EAit ; else ptr 6 2N+DEC-)alloc 2si5e of2N+DE-Step 0: Set ptr ite) 6 9alue. Step 1: Set ptr neAt 6 Null. Step =: 'f start 6 Null then start 6 ptr. Step >: Set Te)p 6 start. Step @: Repeat step E until Te)p neAt U6 Null. Step E: Te)p 6 Te)p neAt. Step V: Te)p neAt 6 ptr.

12

13

12e- Algorith) for 'nsertion of a node at a gi9en location in Lin"-list

#st $ethod%&
Step ,: 'nsert location Step 0: Create node ptr 6 2 N+DE C - Malloc" 2 Si5e of 2N+DE-Step 1: Te)p 6 Start Step =: Repeat until iQ6location-, Te)p 6 Te)p NeAt Step->: ptr NeAt 6 Te)p NeAt Step @: Te)p NeAt 6 ptr. 0nd Method: Step ,: 'f 2ptr 6 Null- then 7 .rint8 +9erflo&8 EAit ; else ptr 6 2N+DEC- )alloc" 2si5e of2N+DE-Step 0: ptr ite) 6 9alue. Step 1: 'f start 6 Null then start 6 ptr. ptr neAt 6 Null. End if Step =: Set i6K( te)p 6 start. Step >: Repeat @ and E until iQloc. Step @: te)p 6 te)p neAt. Step E: i 6i<,. Step V: ptr neAt 6 te)p neAt. Step W: te)p neAt 6 ptr.

'eletion
12f- Algorith) for Deletion of a node fro) Start of a Lin"edlist Step ,: 'f 2start 6 Null- then

13

14

.rint JLin" list e)pty8 EAit End if Step 0: ptr 6 start. Step 1: start 6 start neAt. Step =: free 2ptr-.

12g- Algorith) for Deletion of a node fro) the End of a Lin"ed- list Step ,: 'f 2start 6 Null- then .rint JLin"ed list e)pty8 End End if Step 0: 'f 2start neAt 6 Null- then ptr 6 start. start 6 Null. free 2ptr-. Step 1: ptr 6 start. Step =: Repeat step > and @ until ptr neAt U6Null. Step >: Loc 6 ptr. Step @: ptr 6 ptr neAt. Step E: Loc neAt 6 Null. Step V: free 2ptr-.

12h- Algorith) for deletion of a node fro) a gi9en location in a Lin"ed-list Step ,: 'f 2start 6 Null.rint JUnderflo&8 EAit End if Step 0: N+DE Cptr( Cte)p( i6 K( ptr 6 start. Step 1: Repeat step = to E until iQ6 loc. Step =: te)p 6 ptr. Step >: ptr 6 ptr neAt.
14

15

Step @: i6 i<,. Step E: te)p neAt 6 ptr neAt. Step V: free 2ptr-.

Lin"ed N List i)ple)entation of stac":f ( Top ) ) *ull+ , Temp ) get node( + Temp nfo ) -alue Temp .ink ) *ull Top ) Temp } Else { Temp ) get node( + Temp nfo ) -alue Temp .ink ) Top Top ) Temp }

C Algorith) for deletion of a node fro) stac" using Lin"ed-list


Step ,:

'f 2 Top 6 Null.rint J Underflo&8 Else 7 Top 6 te)p Lin" *ree node 2te)p;

Algorith) for deletion of a node fro) Lin"ed-list fro) any certain point:-

15

16

Step ,:

Step 0: Step 1:
Step =:

if 2 start 6 Null.rint J Underflo&8 EAit End if Node Cptrt ( Cte)p i6K ptr 6 start Repeat step = to E Until Q6 loc
te)p 6 start ptr 6 ptr neAt

Step >: Step @:


Step E:

i 6 i<,
te)p neAt 6 ptr neAt free 2ptr-

Step V:

'oubly .inked list %


A dou%ly lin"- list is one in &hich all nodes are lin"ed together %y )ultiple of lin" &hich help in accessing %oth successor and predecessor node fro) the gi9en node position. 't pro9ides %idirectional professional. 12i- Algorith) for 'nsertion of node at start of dou%ly lin"-list. Step ,: ptr pre9 6 start neAt Step 0: start pre9 6 ptr Step 1: ptr neAt 6 start Step =: start 6 ptr

SEARC/'NH :

.inear search

/inary search

.inear search%& Linear searching search each record of the file, one at a
time until finding a given name and hence the corresponding. The time
16

17

required to execute the algorithm is proportional to the number of comparison. The arrange number of file is equal to the n/2; the complexity of linear search algorithm is given by C2A- 6 nD0. /inary search%& It comp !e t"e #$%e& & me '$t" t"e & me $& t"e m$((le o) t"e l$st* t"$s tells '"$c" " l) o) t"e l$st co&t $& & me+ ,"e comp !e '$t" t"e & me $& t"e m$((le o) t"e co!!ect " l) to (ete!m$&e '"$c" -. !te! o) t"e l$st co&t $& & me &( co&t$&.es t"e p!ocess .&t$l )$&($&# t"e & me $& t"e l$st+ the complexity of binary search algorithm is given by C2A- 6 log0 A

C Algorith) for linear search:Step ,:

Step 0: Step 1: Step =: Step >:

Step @:

Step E:

'eclare Array A [*], ) 0, 1os ) &# nsert element in Array nsert -alue -al to search 2epeat step until 3 n if ( A[i] ) ) -al+ , 1os ) 4 56it4 7 if(pos 8&#+ 1rint 9item in list:. 5lse 1rint 9item is not in list:. 5nd.

C Algorith) for Step ,: ;al, pos ) &# Step 0:

inary Search:-

'eclare Array A[*] min)0, $a6 ) *&#,$id, nsert element in Array in sorted manner.
17

18

Step 1: Step =: &# Step >: Step @:

nsert -al to search. 2epeat step <,=,> until $in 3 ) $a6 && pos ) ) $id) ($in ?$a6+@A f( A[ $id ]) ) -al+ , 1os ) $id4 /reak4 7 f ( A [ $id ] 8 -al+ $a6 ) $id &# 5lse $in ) $in ? # if pos 8 &# 1rint 9Found:. 5lse 1rint 9 *ot Found:. 5nd.

Step E:

Step V:

Step W:

TREE
A tree is a non-linear data structure. 't is a finite set of A one or )ore data ite).

B 18 D E

19

Root :- Root is the first in the /ierarchal arrange)ent of data ite). Node:- Each data ite) is in tree is called tree. Ter)inal Node:A root &ith degree 5ero is called leaf node or ter)inal node. Degree of a node:'t is the nu)%er of tree of the node. Si%ling:- The children node of a gi9en parent node is called Si%ling. Depth:- 't is the )ini)u) le9el of any node( it is also ter)ed as the height of tree. *orest:- 't is the set of disXoint tree. inary tree:- A %inary tree is a finite set of data ite) &hich is either e)pty or consist of a single ite)( is called the root &ith t&o disXoint %inary tree is called left-su%tree and right su%tree.
A

Array representation of
D E / A B D E / C 0 19

inary Tree.
0

20

D E

H inary Tree.

LinedNList Array representation of A C


NULL * NULL

NULL

NULL

NULL

NULL NULL H NULL

*unction for 'n-order tra9ersal in %inary tree:-

Recursi9e *unction:-

!oid in-order 2 struct node C root7 'f2 2 root U6 NULL7 'n-order 2 root left.rint root data 'norder 2 root right; ;

Linear Search:-

$ Algorithm for linear sear%h&"


Step ,: Step 0: Step 1: 'nput A pos6-,(i6K 'nput ele)ent to search.

20

21

Step =:

Step >:

Repeat until 2iQn'f 2A3i4 6 ele)ent.os 6 -, '<< EAit 'f2posI-,.rint J ' 8 Else Print !alue not found"

$ '(n%tion to %reation of Binar) Tree %reate B Tree ( info* +,-.) &


Step ,: 'f N+DE 6 NULL N+DE 6 Create a N+DE N+DE Left 6 NULL N+DE Right 6 NULL Step 0: 'f N+DE info I6 info N+DE left6Create Tree2info N+DE leftElse N+DE Right6Create Tree2info N+DE rightStep 1: Return N+DE. .reorder tra9ersal of inary Tree.

!oid preorder 2 N+DE C .7 'f 2rootU6NULL7 .rint . nu) .reorder 2. left.reorder 2 . Right.ost order tra9ersal of inary Tree. !oid post order 2 N+DE C .7 'f 2rootU6NULL7 .rint . nu)

21

22

.ost order 2. left.ost order 2 . Right. nu).

inary Search Tree:


A inary search tree &hich is either e)pty or satisfy the follo&ing:,- The 9alue of the left child should %e less then the 9alue of the root. 0- The 9alue of the left child is )ore then or e$ual to the 9alue of the root. EAa)ple: E 1 , = W
7 3 1 4 2 5 6 8 9 1 0

,K

>

LE S+RT:-

$ Algorithm for b(bble sort &"


Step ,: Step 0: Step 1: Step =: Step >: Step @: Step E: Step V:
'nput A for 2i6, to i6 n-,- do 1 to E for 2X6, to X6 n-,- do = to E if2 A3i<,4 Q A3i4T 6 A 3i4 A3i4 6 A3i<,4 A3i<,4 6T print A

22

23

B CA1 to /ubble Sort.


Dinclude3stdio.h8 Dinclude3conio.h8 -oid main(+ , int 6[#0],i,E,n,t4 clrscr(+4 printf(FGn 5nter the number of element%&&&8F+4 scanf(FHd F,&n+4 for(i)#4i3)n4i??+ , for(i)#4i3)n&#4i??+ , for(E)#4E3)n&#4E??+ , if(6[i?#]36[i]+ , t)6[i]4 6[i])6[i?#]4 6[i?#])t4 7 7 7 7 for(i)#4i3)n4i??+ , printf(FHdF,6[i]+4 7 getch(+4 7 'nsertion sort is a successi9e ele)ents are inserted at the proper position in the sorted list.

'nsertion sort :-

23

24

$ Algorithm for Insertion sort&"


Step ,: Step 0:

inp(t /

Step 1 : Step =: Step >: Step @: Step E: Step V: Step W: Step ,K:

for i 6 K to i 6 n do 1 to W T6 A3i4 X6 i- , &hile 2 t Q A3X4 TT X I K - do @ T E A3X<,4 6 A3X4 X6X-, X 6 X<, A3X4 6 T print J A8

Array
An array is defined as a set of finite homogeneous element and data item. it means array can contain one type of data only, either only integer, all floating Ipoint number, or all character . Chere int specifies the data type of element array store. Some common operation performed on array are4 #. creation on array A. tra-ersing an array J. insertion of ne" element K. deletion of required element <. modification of require element =. merging of array.

$ Algorithm for insertion of element in Arra)&"


Step ,: Step 0: Step 1:
Step =: 'nput the ele)ent into an array a3si5e4. 'nput the location nu)%er loc input the 9alue ne&si5e6si5e<, &hile ne&si5eIloc a3ne&si5e4 6 a3ne&si5e-,4 24

Step >:

25 ne&si5e-a3ne&si5e4 6 9alue si5e6si5e<,

Step @: Step E:

Algorithm for Deletion of element in Arra)&"


Step ,: Step 0: Step 1: Step =:
'nput the ele)ent into the array. 'nput the location nu)%er loc &hile loc Q si5e A3loc4 6 A3loc < ,4 loc 6 loc < , si5e--

0" 1AP for Tra2ersal for Arra)


Dinclude3stdio.h8 Dinclude3conio.h8 -oid main(+ , int a[<], i4 clrscr(+4 printf(FGn 5nter the element in the Array%&&&&&&&&8F+4 for( i)0 4 i3 <4 i??+ , scanf(FHd F,& a[i]+4 7 printf(FGn Array 'isplayedF+4 for(i)04 i3<4 i??+ , scanf(FHdF,&a[i]+4 7 getch(+4 7

B& CA1 to insertion of an element in the array


Dinclude3stdio.h8 Dinclude3conio.h8 -oid main(+ ,
25

26

int a[<0],i,n,-alue, loc4 clrscr(+4 printf(FGn Lo" many element%&&&&&&&8F+4 scanf(FHdF, &n+4 printf(FGn 5nter array element%&&&&&&8F+4 for( i)04 i3)n4 i??+4 , scanf(FHdF,&a[i]+4 7 printf(FGn 5nter the location no. %&&&&8F+4 scanf(FHdF,&loc+4 printf(FGn 5nter the -alueF+4 scanf(FHdF,&-alue+4 for(i)n4 i8) loc4 i&&+ , 6[i?#] ) 6[i]4 7 6[loc] ) -alue4 for(i)#4 i3)n?#4 i??+ , printf(FHdF,6[i]+4 7 getch(+4 7

Question 1:- Write a programme to convert temperature in centigrade? Answer:#include<stdio.h> #include<conio.h> main() { float f, C; clrscr(); printf( \n Enter the temperature in Fahrenheit: - );
26

27

scanf( %f , &f); C = (f-32)/1.8; printf ( \n The temperature in Celsius scale is = %f,C:); getch(); }

output:-

Question 2:- Write a program to swap two number without using third veriable?
27

28

Answer:#include<stdio.h> #include<conio.h> main() { int a, b; clrscr(); printf(\n Enter the two numbers: ); scanf( %d%d, &a,&b); printf( \n the value before swapping is :- %d%d, a,b); a =a+b; b =a-b; a =a-b; printf( \n after swapping the value is :-%d%d,a,b); getch(); } Output:-

28

29

QUESTION 3:- Write a program to calculate the volume and area of sphere using a symbolic constant . PI=3.142 test your program For redius 1,6,12,2,0,0.2,12.2. Answer:#include<stdio.h> #include<conio.h> main() { float Volume, area, r; clrscr()
29

30

printf(\n Enter the radius of sphere:-); scanf(%f,&r); Volume=4/3(3.142*r*r*r); printf(\n area:-%f \t volume:-%f,area,volume); getch(); }

Output:Case1. When enter the radius of sphere is 1 then

30

31

Case2. when enter the radius of sphere is 6 then

Case3. when enter the radius of sphere is12 then

31

32

Case4. when enter the radius of sphere is 2 then

32

33

Case5. when enter the radius of sphere is 0 then

Case6. when enter the radius of sphere is 0.2 then

33

34

Case7. when enter the radius of sphere is 12.2 then

34

35

Question 4:- Write a program to calculate the salary of medical representative based on the sales , bonus and incentive to be offered to him will be based on total sales if the seals exceed Rs-1 lakhs follow the particular of table 1 other wise table 2. Table 1 Basic = Rs 3000\HRA = 20% of basic DA = 110%of basic Conveyance =Rs 500\Incentive = 10% of sales Bonus = Rs 500\Table 2 Basic = Rs 3000\HRA = 20% of basic DA = 110%of basic Conveyance =Rs 500\Incentive = 5% of sales Bonus = Rs 500\-

Coding #include<stdio.h> #include<conio.h> main() { float s-amt , hra , da , ins ,sal; int bs =3000, bonus = 500 , con = 500; clrscr(); printf(\n enter the sales amount :-); scanf(\n %f , &s-amt); if { hra = 0.2 *bs ; da = 1.1 * s-amt;

35

36

sal = bs + ins + bonus + hra + da + con; printf(\n hra =%f \t da = %f \t ins = %f \t netsal=%f, hra, da, ins,sal); } else { hra = 0.2 *bs ; da = 1.1 * bs; ins = 0.05*s-amt; sal = bs + ins + bonus + hra + da + con; printf(\n hra =%f \t da = %f \t ins = %f \t netsal=%f, hra, da, ins ,sal); } getch(); }

Output

36

37

question5:- Write a program to calculate energy bill read the starting and ending meter. The charges as follows No of unit consumed 200-500 100-200 Less then 100 Rates in rupees Rs 3.50\Rs 2.50\Rs 1.50\-

#Include<stdio.h> #include<conio.h> main() { float uc , bill; clrscr(); printf(\n enter the electricity unit consumed by user:-);
37

38

scanf(%f ,&uc); if(uc <100) { bill = 1.50 *uc; } else if ( uc <200 || uc =100 ) { bill = 100 + (uc-100)*2.50; } else if (uc <500 || uc =200) { bill = 500 +(uc-300)*3.50; } printf( \n bill is %f ,bill); getch(); } Output:Enter the electricity unit consumed by user :- 100 Bill is 100.00

38

39

Question 6:- W rite a program to sort six number and find the largest one by using ladder of if-else. #include<stdio.h> #include<conio.h> main() { int a,b,c,d,e,f; clrscr(); printf("\nEnter the six number:- "); scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f); if(a>b && a>c && a>d && a>e && a>e) { printf("a is the largest=%d",a); } else if(b>a && b>c && b>d && b>e && b>f) { printf("b is the largest=%d",b); } else if(c>a && c>b && c>d && c>e && c>f) { printf(" c is the largest=%d",c); } else if(d>a && d>b && d>c && d>e && d>f)

39

40

{ printf(" d is the largest=%d",d); } else if(e>a && e>b && e>c && e>d && e>f) { printf("e is the largest =%d",e); } else { printf(" f is largest=%d",f); } getch(); }

Output:-

40

41

Question8:- Write a program to calculate n factorial where n is a positive integer number and its value is to be read from keyboard . Try and implement factorial 15. #include<stdio.h> # include<conio.h> main() { int no,fact=1;
41

42

clrscr(); printf("\n Enter the number;-"); scanf("%d",&no); while(no>1) { fact = fact*no; no--; } printf("\n Factorial of given number:-%d",fact); getch(); } Output:-

42

43

Question10:- Write a program to generate a table of prime number between 2 and 20. Coding:#include<stdio.h> #include<conio.h> main() { int i,j,num,n; clrscr(); for(i=2;i<=20;i++) { num=0; for(j=1;j<=i;j++) { if(i%j==0) { num=num+1;

} }

if(num==2)

43

44

{ for(n=1;n<=10;n++) { printf("%d ",n*i); } printf("\n");

} getch(); }

Output:-

44

45

Question12:- Write a program to find the root of the quadratic equation find all roots. Coding:#include<stdio.h> #include<conio.h> #include<math.h> main() { int a,b,c,d; float x1,x2; clrscr(); printf("\n enter the value of a:-"); scanf("%d",&a); printf("\n enter the value of b :-"); scanf("%d",&b); printf("\n enter the value of c :-");

45

46

scanf("%d",&c); d=b*b - 4*a*c; if(d<0) { printf("\n imaginary root"); } else if(d>=0) { x1 = -b+sqrt(d)/2*a; x2 = -b- sqrt(d)/2*a; printf("x1=%f\t x2=%f",x1,x2); } getch(); }

Output:-

46

47

Question 14:- Write a program to find the sum of series +2+3+ ..n. Coding:#include<stdio.h> #include<conio.h>
47

48

main() { int i,n,sum=1; printf("\n Enter the number of terms:- "); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+i; printf("%d+",i-1); } printf("%d=%d",i,sum); printf("\n Sum of the series is:- %d",sum); getch();

Output:-

48

49

Question 15:- Write a program to find 1-x/2! x/4! .upto n terms. #include<stdio.h> #include<conio.h> int fact(int); int fact(int i) { int fact; if(i==1) { return (1); else {
49

50

fact=i*fact(i-1); return (fact);

} main() { int n,x; float term,sum; clrscr(); printf("\n Enter the Vlaue of x:- \n"); scanf("%d",&x); printf("\n Enter the number of term:-\n"); scanf("%d",&term); sum=0; for(term=1;n<=term;n++) { sum=sum+fact(2*n); } printf("\n Sum=%d",sum);

getch(); }

0utput:-

50

51

Question 16:- Write a program to find ..+ n^2 Coding:-

1^2 + 2^2 + 3^2 +

#include<stdio.h> #include<conio.h>
51

52

#include<math.h> main() { int n,i,sum=0; printf("\n Enter the number of terms:- "); scanf("%d",&n); for(i=0;i<=n;i++) { sum=sum+(i*i); } printf("sum of the series is:- %d",sum); getch(); } Output:-

52

53

Question 17:- Write a program to find the +n^3 Coding:-

1^3 + 2^3 +

#include<stdio.h> #include<conio.h> #include<math.h> main() { int n,i,sum=0; printf("\n Enter the number of terms:- "); scanf("%d",&n); for(i=0;i<=n;i++) { sum=sum+(i*i*i); } printf("sum of the series is:- %d",sum); getch();

Output:-

53

54

Questiopn 18:- Write a program to find the 1^2 + 2^2 + +n^2 Coding:#include<stdio.h> #include<conio.h> #include<math.h> main() {
54

55

int i,n,a,T=0; clrscr(); printf("For Finding The Sum of Square, Enter The nth Term Value.\n"); printf("___________________________________________ ___________\n"); scanf("%d",&n); Printf("Given nth Term value =%d.",n); printf("\n\n************RESULT OF SQUARE IN nth TERM********************\n\n"); for(i=1;i<=n;i++) { a=pow(i,2); if(i==n) printf(".....+%d.",a); else printf("%d+",a); T=T+a; } printf("\n\nTotal=%d.",T); getch(); }

55

56

Output: -

Question 19:- Write a program to find .. Coding:#include<stdio.h> #include<conio.h> #include<math.h> main() {


56

1 + sqrt(2) +sqrt(3) +

57

int i,term; float sum=0; clrscr(); printf("\nEnter the number number of terms:- "); scanf("%d",&term); for(i=1;i<=term;i++) { sum=sum+sqrt(i); } printf("sum=%f",sum); getch(); } Output:-

57

58

Question 20:- Write a program to reverse the string and check wether It paliandrawn or not. Coding:#include<stdio.h> #include<conio.h> main() { int i,j=0; char x[30],y[30]; clrscr(); printf("\n Enter any string:- "); gets(x); for(i=0;x[i]!='\0';i++); printf("\n"); for(i--;i>=0;i--) { y[j]=x[i]; j++; } y[j]='\0'; i=0; while(x[i]==y[i] && x[i]!='\0' && y[i]!='\0')

58

59

i=i+1;

} if(x[i]=='\0' && y[i]=='\0') printf("\n Palindrome"); else printf("\n not Palindrome");

getch(); } output:case1. alphabatically

Case2. numeric
59

60

Question 21 :- Write a program yo generate a series of armstrong number. Coding:#include<stdio.h> #include<conio.h> main() { int n,j,sum=0,a; clrscr(); printf(" \n");

RESULT

60

61

printf("***************** SERIES OF ARMSTRONG NO.******************\n\n"); for(n=1;n<=3000;n++) { for(j=n;j>0;j=j/10) { a=j%10; sum=sum+a*a*a; } if(sum= =n) printf("%d\t",sum); sum=0; } getch(); } Output:-

61

62

Question22:- Write a program to detect the occurance of a character in a given string. Coding:#include<stdio.h> #include<conio.h> main() { char a[20]; int i=0,j=0; int count=0; char ch; clrscr(); printf("\nEnter any String:- "); scanf("%s",a); fflush(stdin); printf("U have to find occur whom character:- "); scanf("%c",&ch); fflush(stdin); while(a[i]!=NULL) { if(a[i]==ch) { count=count+1; }

62

63

i++; } printf("occurence of given chracter is:- %d",count); getch(); }

Output:-

Question23:- Write a program to sort the element of an array using selection sort.

63

64

Coding:#include<stdio.h> #include<conio.h> main() { int a[10],i,j,temp; clrscr(); printf("\n Enter the elements to sort:- "); for(i=0;i<=9;i++) { scanf("%d",&a[i]); } printf("Before sorted array is:-\n"); for(i=0;i<=9;i++) { printf("%d ",a[i]); } for(i=0;i<=9;i++) { for(j=0;j<=9-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; }

64

65

} } printf("\n After Sorted Array is:- \n"); for(i=0;i<=9;i++) { printf("%d ",a[i]); } getch(); } Output:-

Question24:- Write a program to search an element an array using binary search technique. Coding:65

66

#include<stdio.h> #include<conio.h> main() { int a[10],i,n; clrscr(); printf("\n Enter the number:- "); for(i=0;i<=9;i++) { scanf("%d",&a[i]); } printf("\n List is:-" ); for(i=0;i<=9;i++) { printf("%d ",a[i]); } printf("\nEnter the number which u want to search:- "); scanf("%d",&n); for(i=0;i<=9;i++) { if(a[i]==n) { printf("\n positiom of item is %d",i); } } getch(); }

66

67

Output:-

Question25:- Write a program to count the vowel and digit in a given string. Coding:#include<stdio.h> #include<conio.h> main() { char a[30]; int i=0,vow=0,con=0; clrscr();
67

68

printf("\n Enter any text:- "); scanf("%s",a); while(a[i]!='\0') { switch(a[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': vow++; break; default: con++; } i++; } printf("\n Vowel=%d \n Con=%d",vow,con); getch(); }

Output:-

68

69

Question26:- Write a program to display the multication table using array. Coding:#include<conio.h> #include<stdio.h> main() { int i,j,n; i=1; clrscr(); do { j=1;
69

70

n=i*j; printf("%4d",n); j=j+1; }while(j<=10); printf("\n"); i=i+1; }while(i<=10); getch(); } Output:-

do {

Question27:- Write a program in c to create a matrix and display its element one by one. Coding:70

71

#include<stdio.h> #include<conio.h> main() { int i,j,a[3][3]; clrscr(); printf("\nEnter the Elements of Matrix:- "); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { scanf("%d",&a[i][j]); } } printf("\n The matrix is :-\n "); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { printf("%d ",a[i][j]); } printf("\n"); } getch(); }

71

72

Output:-

72

73

Question29:- Write a program in c to convert binary to decimal. Coding:#include<stdio.h> #include<conio.h> void main() { int num,sum=0,i=0,z=1,x,rem,j; clrscr(); printf("\n Enter the number:------>"); scanf("%d",&num); while(num>=0) { rem=num%10; if(i==0&&rem==1) { sum=sum+rem*(1); } else { x=i; z=1; for(j=1;j<=x;j++) {

73

74

z=z*2; } } sum=sum+rem*z;

i++; num=num/10;

printf("\n Decimal number=%ld",sum); getch(); } Output:-

74

75

Question30:- write a program to search a number by using binary searching and bubble sorting. Coding:#include<stdio.h> #include<conio.h> #define MAX 10 int A[MAX]={5,6,7,8,9,10,11,12,13,14}; int bsearchn(int l,int u, int k) { int m; while(u>=1) { m=(l+u)/2; if(k==A[m]) { return(m+1); } if(k<A[m]) { u=m-1; } else { l=m+1; }

75

76

} } main() { int i,lb=0,ub=10,key,loc; clrscr(); for(i=lb;i<ub;i++) { printf("%d ",A[i]); } printf("\n Enter the number which u want for Search:- "); scanf("%d",&key); loc=bsearchn(lb,ub,key); printf("%d found at %d location",key,loc); getch(); }

Output:-

76

77

Question31:- Write a program in c to obtain sum and product of two matrix. Coding: #include<stdio.h> #include<conio.h> main() { int a[3][3],b[3][3],m[3][3],s[3][3],i,j,k; clrscr(); printf("\n Enter the Elements of matrix:- "); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { scanf("%d",&a[i][j]); }
77

78

} printf("The matrix is:- \n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { printf("%d ",a[i][j]); } printf("\n"); } printf("\n Enter the Elements of Second Matrix:-\n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { scanf("%d",&b[i][j]); } } printf("\n The Second Matrix is:- \n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { printf("%d ",b[i][j]); } printf("\n"); }

78

79

printf("The Addition of two matrix is:- \n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { s[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { printf("%d ",s[i][j]); } printf("\n"); } printf("The Multiplication of matrix is:-\n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { m[i][j]=0; for(k=0;k<=2;k++) { m[i][j]=m[i][j]+(a[i][k]*a[k][j]); } }

79

80

} for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { printf("%d ",m[i][j]); } printf("\n"); } getch(); } Output:-

80

81

Question 32 :- WAP to calcute to the following:- Area of Ractangle, Area of Trapizium, Area of Triangle, Area of Circle, Area of Cylender,Area of parallelogram,Area of Rhambus,Cone of Volume,Sphere of Volume,Addition, Substrction, multipication, Division. #include<stdio.h> #include<conio.h> #include<math.h> void main() { float Area,a,b,c,x,y,l,h,r,d1,d2,S,Addition, Substraction,Multiplication,Division,Volume,salary,tax,nil,shpere, grosssalary; char choice; clrscr(); printf("\n Enter choice:---->"); scanf("%c",&choice);

switch(choice) { case 1: printf("\n Enter length and breath of the Ractangle:------->");

81

82

scanf("%f %f",&l,&b); Area = (l*b); printf("\n Area of Ractangle = %f",Area); break; case 2: printf("\n Enter the sides of Triangle:------->"); scanf("%f %f %f",&a,&b,&c); S = (a+b+c)/2; Area = sqrt(S*(S-a)*(S-b)*(S-c)); printf("\n Area of triangle = %f",Area); break; case 3: printf("\n Enter the Radius of the Circle :------>"); scanf("%f",&r); Area = (3.14*r*r); printf("\n Area of Circle = %f",Area); break; case 4: printf("\n Enter the radius and Height of the cylender---->"); scanf("%f %f",&r,&h); Area = (2*3.14*r*h); printf("\n Area of Cylender = %f",Area); break; case 5: printf("\n Enter the base and height of the parallelogram:------->");

82

83

scanf("%f %f",&b,&h); Area = (b*h); printf("\n Area of Parallelogram break;

%f",Area);

case 6: printf("\n Enter diagonals of the Rhambus:-------->"); scanf("%f %f",&d1,&d2); Area = (0.5*d1*d2); printf("\n Area of Rahambus = %f",Area); break; case 7: printf("\n Enter Radius And Height of the Cone:------->"); scanf("%f %f",&r,&h); Volume = (0.333*3.14*r*r*h); printf("\n Cone of volume = %f",Volume); break; case 8: printf("\n Enter the Radius of half sphere:------>"); scanf("%f",&r); Volume = (0.666*3.14*r*r*r); printf("\n Volume of HalfSphere = %f",Volume); break; case 9: printf("\n Enter the sides and height of the Trapizium:---->"); scanf("%f %f %f",&a,&b,&h);

83

84

Area =(0.5*(a+b)*h); printf("\n Area of Trapizium = %f",Area); break; case 10: printf("\n Enter Salary:------>"); scanf("%f",&salary); if(salary<=2000) { tax=nil; } if(2000<salary<=4000) { tax=.03; } if(4000<salary<=5000) { tax=.05; } if(salary>5000) { tax=.08; } printf("\n salary = %f",salary); printf("\n tax = %f",tax); break; case 11:

84

85

printf("\n Enter the numbers a & b for Sum:------->"); scanf("%f %f",&a,&b); Addition = a+b; printf("\n Addition = %f",Addition); break; case 12: printf("\n Enter the numbes a & b for Substract:----->"); scanf("%f %f",&a,&b); Substraction = a-b; printf("\n Substraction = %f",Substraction); break; case 13: printf("\n Enter the numbers a & b for Multiplication:----->"); scanf("%f %f",&a,&b); Multiplication = a*b; printf("\n Multiplication = %f",Multiplication); break; case 14: printf("\n Enter the numbers a & b for Division:"); scanf("%f %f",&a,&b); Division = a/b; printf("\n Division = %f",Division); break; case 15: if(grosssalary<=2000) printf("\n enter the gross salary:");

85

86

scanf("%f",&grosssalary); tax=grosssalary-grosssalary*0/100; printf("Payment with Tax = %f",tax); break; case 16: if(2000<grosssalary<=4000) printf("\n enter the gross salary:"); scanf("%f",&grosssalary); tax=grosssalary-grosssalary*3/100; printf("Payment with Tax = %f",tax); break; case 17: if(4000<grosssalary<=5000) printf("\n enter the gross salary:"); scanf("%f",&grosssalary); tax=grosssalary-grosssalary*5/100; printf("Payment eith Tax = %f",tax); break; case 18: if(grosssalary>5000) printf("\n enter the gross salary:"); scanf("%f",&grosssalary); tax=grosssalary-grosssalary*8/100; printf("\n Payment with Tax = %f",tax); break; case '+': printf("\n Enter x number:--------->");

86

87

scanf("%f",&x); printf("\n Enter y number:------->"); scanf("%f",&y); printf("%f",x+y); break; case '-': printf("\n Enter x number:--------->"); scanf("%f",&x); printf("\n Enter y number:------->"); scanf("%f",&y); printf("%f",x-y); break; case '*': printf("\n Enter x number:--------->"); scanf("%f",&x); printf("\n Enter y number:------->"); scanf("%f",&y); printf("%f",x*y); break; case '/': printf("\n Enter x number:--------->"); scanf("%f",&x); printf("\n Enter y number:------->"); scanf("%f",&y); printf("%f",x/y); break; default:

87

88

printf("\n Wrong Choice"); break; } getch(); }

Outputs:Case 1

Case 2

88

89

Case 3

Case 4

Case 5

89

90

Case 6

Case 7

90

91

Case 8

Case 9

91

92

Case 10

Case 11

92

93

Case 12

Case 13

93

94

Case 14

Case 15

94

95

Case 16

Case 17

95

96

Case 18

Question 33:- WAP for greatest of three number. #include<stdio.h> #include<conio.h> main() { int num1,num2,num3; clrscr(); printf("\n enter first num"); scanf("%d",&num1);

96

97

printf("\n enter second num"); scanf("%d",&num2); printf("\n enter third num"); scanf("%d",&num3); if(num1>num2&&num1>num3) { printf("\n num1 is greter"); } if else(num2>num1&&num2>num3) { printf("\n num2 is greater"); } else(num3>num1&&num3>num2) { printf("\n num3 is greater"); } getch();

Output

112 2!$te p!o#! m to co&%e!t oct l to (ec$m l &.m3e!+ A&s

97

98

4$&cl.(e5st($o+"6 4$&cl.(e5co&$o+"6 4$&cl.(e5m t"+"6 %o$( m $&78 { $&t o9 9s.m:09$:0* cl!sc!78* p!$&t)7;E&te! & oct l &.m3e!;8* sc &)7;<(;9=o8* '"$le7o608 { :o<10* $)7 6:88 3!e >* s.m:s.m+7 *po'789$88* o:o/10* $++* } $)7 588 p!$&t)7;t"e oct l e-.$% le&t o) e&te!e( &.m3e!:<(;9s.m8* else p!$&t)7;e&te!e( &.m3e! $s &ot & oct l &.m3e!;8* #etc"78* }

98

You might also like