8/12/2019 c and ds manuaL
1/259
JYOTHISHMATHI COLLEGE OF ENGINEERING&TECHNOLOGY.
C and Data Structures Lab manua
Lab Manual for the Academic Year 2010-2011
I B.Tech
In!c"ar#e HOD $r%nc%a
$reared b'( Ar)*ed &Re*%e+ed b'(
Issued b'( +.e., Date(
D.RAMBABU 08-oct-2010
1
8/12/2019 c and ds manuaL
2/259
No. Name of the Program Page No. !emar"
1 #ee" l.a$ Write a C program to find the sum of individual digits of a
positive integer.b$ A Fibonacci sequence is defined as follows: the first andsecond terms in the sequence are 0 and 1.Subsequent terms are found b adding the preceding two termsin the sequence. Write a C program to generate the first n termsof the sequence.c$ Write a C program to generate all the prime numbers between1 and n! where n is a value supplied b the user
1
" #ee" 2.a$ Write a C program to calculate the following Sum:Sum#1$%"&"' (%)&)'$%*&*'(%+&+'$%10&10'
b$ Write a C program to find the roots of a quadratic equation
11
, #ee" %a$ Write C programs that use both recursive and non$recursivefunctionsi- o find the factorial of a given integer.ii- o find the /C greatest common divisor- of two givenintegers.iii- o solve owers of 2anoi problem.
1+
) #ee" &
a$ he total distance travelled b vehicle in 3t4 seconds is givenb distance # ut(1&"at"where 3u4 and 3a4 are the initial velocitm&sec.- and acceleration m&sec"-. Write C program to find thedistance travelled at regular intervals of time given the values of3u4 and 3a4. he program should provide the fle%ibilit to theuser to select his own time intervals and repeat the calculationsfor different values of 3u4 and 3a4.b$ Write a C program! which ta5es two integer operands andone operator from the user! performs the operation and thenprints the result. Consider the operators (!$!6! &! 7 and useSwitch Statement-
,8
9 #ee" 'a$ Write a C program to find both the largest and smallest number in a listof integers.b$ Write a C program that uses functions to perform the following:i- Addition of wo atricesii- ultiplication of wo atrices
)*
"
8/12/2019 c and ds manuaL
3/259
* #ee" (a$ Write a C program that uses functions to perform the followingoperations:i- o insert a sub$string in to a given main string from a given
position.ii- o delete n Characters from a given position in a given string.b$ Write a C program to determine if the given string is apalindrome or not.
9;
; #ee" )a$ Write a C program that displas the position or inde% in thestring S where the string begins! or < 1 if S doesn4t contain .b$ Write a C program to count the lines! words and characters ina given te%t.
;1
No. Name of the Program Page No. !emar"+ #ee" *
a$ Write a C program to generate =ascal4s triangle.b$ Write a C program to construct a pramid of numbers.
;8
8 #ee" +Write a C program to read in two numbers! % and n! and thencompute the sum of this geometricprogression:1(%(%"(%,(>>>>.(%n
For e%ample: if n is , and % is 9! then the program computes
1(9("9(1"9.=rint %! n! the sum=erform error chec5ing. For e%ample! the formula does not ma5esense for negative e%ponents < if n is less than 0. 2ave ourprogram print an error message if n?0! then go bac5 and read inthe ne%t pair of numbers of without computing the sum.Are an values of % also illegal @ f so! test for them too.
+*
10 #ee" 10a$ "4s complement of a number is obtained b scanning it fromright to left and complementing all the bits after the first
appearance of a 1. hus "4s complement of 11100 is 00100.Write a C program to find the "4s complement of a binarnumber.b$ Write a C program to convert a Boman numeral to itsdecimal equivalent.
80
,
8/12/2019 c and ds manuaL
4/259
11 #ee" 11Write a C program that uses functions to perform the followingoperations:i- Beading a comple% number
ii- Writing a comple% numberiii- Addition of two comple% numbersiv- ultiplication of two comple% numbersote: represent comple% number using a structure.-
101
1" #ee" 12a$ Write a C program which copies one file to another.b$ Write a C program to reverse the first n characters in a file.ote: he file name and n are specified on the command line.-
10*
1, #ee" 1%
a$ Write a C program to displa the contents of a file.b$ Write a C program to merge two files into a third file i.e.! the contents of the first file followed b those of thesecond are put in the third file-
110
1) #ee" 1&Write a C program that uses functions to perform the followingoperations on singl lin5ed list.:i- Creation ii- nsertion iii- eletion iv- raversal
11)
19 #ee" 1'Write C programs that implement stac5 its operations- usingi- Arras ii- =ointers
1)*
1* #ee" 1(Write C programs that implement Dueue its operations- usingi- Arras ii- =ointers
1*0
No. Name of the Program Page No. !emar"
1; #ee" 1)Write a C program that uses Stac5 operations to perform thefollowing:i- Converting infi% e%pression into postfi% e%pressionii- Evaluating the postfi% e%pression
1;)
1+ #ee" 1*Write C programs that use both recursive and non recursive
functions to perform the following searching operations for ae value in a given list of integers :i- Ginear search ii- Hinar search
1+"
18 #ee" 1+Write a C program that implements the following sortingmethods to sort a given list of integers in ascending orderi- Hubble sortii- Selection sort
"0,
)
8/12/2019 c and ds manuaL
5/259
"0 #ee" 20Write C program that implements the following sorting methodto sort a given list of integers in ascending order:i- Duic5 sort
"0;
"1 #ee" 21Write C program that implement the following sorting methodto sort a given list of integers in ascending order:i- erge sort
"19
"" #ee" 22Write C programs to implement the Gagrange interpolation andewton$ /regor forward interpolation.
""0
", #ee" 2%Write C programs to implement the linear regression andpolnomial regression algorithms.
"";
") #ee" 2&Write C programs to implement rapeIoidal and Simpsonmethods.
",*
9
8/12/2019 c and ds manuaL
6/259
,bectie / 1
o find the sum of individual digits of a given number
ecrition/Sum of the individual digits means adding all the digits of a numberE%: 1", sum of digits is 1("(,#*
Algorithm/
Step 1: startStep ": read nStep ,: initialiIe the s#0Step ): if n?0 goto Step ;Step 9: if n'#0 goto Step * else goto step ;
Step *: store n710 value in pAdd p value to sAssign n&10 value to n/oto Step 9
Step ;: print the outputStep +:stop
*
8/12/2019 c and ds manuaL
7/259
lo3chart/
BJE FAGSE
BJE
;
SAB
S#0
BEA
F ?0
KJ=J
W2GE
' #0
SK= =#710
S#S(=
#&10
8/12/2019 c and ds manuaL
8/259
Program/
Linclude?stdio.hMmain-
N int n!s!pO clrscr-O printfPenter the vaue for n:QnP-O scanfP7dP!Rn-O s#0O ifn?0- printfPhe given number is not validP-O else N whilen'#0- &6 chec5 the given value #0 or not 6&
N p#n710O n#n&10O s#s(pO
printfPsum of individual digits is 7dP!s-O getch-O,utut/
1.Enter the value for n: ,,, Sum of individual digits is 8".Enter the value for n: );,, Sum of individual digits is 1;,. Enter the value for n: $111 he given number is not valid4oncluion /he program is error free
5I5A 6789ATI,N9/
1- What is the mean of sum of the individual digits@ Ans: Sum of the individual digits means adding each digit in a number
"- What is positive integer@Ans: if the integer value is grater than Iero then it is called positive integer,- efine preprocessor @Ans: Hefore compiling a process called preprocessing is done on the source code b aprogram called the preprocessor.
+
8/12/2019 c and ds manuaL
9/259
,bectie/2
o print the Fibonacci series for 1 to n valueecrition
A fibonacci series is defined as followshe first term in the sequence is 0he second term in the sequence is 1he sub sequent terms 1 found b adding the preceding two terms inthe sequenceFormula: let t1!t"!>>>>tn be terms in fibinacci sequence
t1#0! t"#1tn#tn$"(tn$1>>where nM"
algorithm/
Step 1: startStep ": initialiIe the a#0! b#1Step ,: read n
Step ): if n## 1 print a go to step ;. else goto step 9Step 9: if n## " print a! b go to step ; else print a!bStep *: initialiIe i#,
i- if i?# n do as follows. f not goto step ;c#a(bprint ca#bb#cincrement valuegoto step *i-
Step ;: stop
8
8/12/2019 c and ds manuaL
10/259
lo3chart/
SAB
A#0!b#1
Bead n
f n##1
Kutput af n##"
Kutput a!b Kutput a!b
#" i((
?n
C # a ( b
Kutput c
A # b H # c
Stop
rue False
False
rue
False
10
8/12/2019 c and ds manuaL
11/259
Program/
Linclude?stdio.hM
void main-N int a!b!c!n!iO clrscr-O printfPenter n valueP-O scanfP7dP!Rn-O a#0O b#1O ifn##1- printfP7dP!a-O else
ifn##"- printfP7d7dP!a!b-O else N printfP7d7dP!a!b-O
&&GKK= WGG BJ FKB " E GESS SEBES AS 2ESE WAS=BE ATACE
fori#"Oi?nOi((- N
c#a(bOprintfP7dP!c-Oa#bOb#cO
getch-O
,utut/
1. Enter n value : 90 1 1 " ,
". Enter n value : ;0 1 1 " , 9 +
,. Enter n value : $*0 1
4oncluion /he program is error free
11
8/12/2019 c and ds manuaL
12/259
5I5A 6789ATI,N9/
1$ #hat i ibonacci erie :
An/ A fibonacci series is defined as followshe first term in the sequence is 0he second term in the sequence is 1he sub sequent terms 1 found b adding the preceding two terms inthe sequenceFormula: let t1!t"!>>>>tn be terms in fibinacci sequence
t1#0! t"#1tn#tn$"(tn$1>>where nM"
"- What are the various tpes of unconditional statements@ Ans: goto!Hrea5 and continue
,-What are the various tpes of conditional statements@
Ans: if ! if else !switch statements)- E%pand ?SK.2 M@Ans: standard input output header file
1"
8/12/2019 c and ds manuaL
13/259
,bectie /%
o print a prime numbers up to 1 to n
ecrition/=rime number is a number which is e%actl divisible b one and itself onlE%: "! ,!9!;!>>>O
Algorithm/
Step 1: startStep ": read nStep ,: initialiIe i#1!c#0Step ):if i?#n goto step 9
f not goto step 10Step 9: initialiIe U#1Step *: if U?#1 do as the follow. f no goto step ;
i-if i7U##0 increment cii- increment Uiii- goto Step *
Step ;: if c## " print iStep +: increment iStep 8: goto step )Step 10: stop
1,
8/12/2019 c and ds manuaL
14/259
lo3 chart/
Start
Bead n
# 1
?#n
((
V # 1 V((
V?#i
ffact##"
f 7 U ##0
Fact ((
true
false
true
false
Kutputfact
false
stop
false
1)
8/12/2019 c and ds manuaL
15/259
Program/
Linclude?stdio.hMLinclude?conio.hM
void main-N int n!i!fact!UO clrscr-O printfPenter the number:P-O scanfP7dP!Rn-O fori#1Oi?#nOi((- N fact#0O
&&2S GKK= WGG C2EC A K K HE =BE K. KB K. forU#1OU?#iOU((-
N ifi7U##0-fact((O
iffact##"- printfPQn 7dP!i-O getch -O
,utut/
Enter the number : 9" , 9
Enter the number : 10" , 9 ;Enter the number : 1"" , 9 ; 11
4oncluion /he program is error free
5I5A 6789ATI,N9/
1$ #hat i rime number :
An/ =rime number is a number which is e%actl divisible b one and itself onl
"-What is an algorithm@Ans : A step b step procedure is called algorithm
,-What is flow chart@Ans: A pictorial representation an algorithm is called a flow chart)-What is program@
19
8/12/2019 c and ds manuaL
16/259
Ans : A collection of statements is called
,bectie/&
o find the roots of the quadratic equationecrition/
ature of roots of quadratic equation can be 5nown from the quadrant
# b"
$)acf b"$)ac M0 then roots are real and unequalf b"$)ac #0 then roots are real and equalf b"$)ac ?0 then roots are imaginar
Algorithm/
Step 1: startStep ": read the a!b!c valueStep ,: if b6b$)acM0 then
Boot 1# $b( powb6b$)6a6c-!0.9--&"6aBoot "# $b$powb6b$)6a6c-!0.9--&"6a
Step ): if b6b$)ac#0 then
Boot1 # Boot" # $b&"6a-Step 9: Ktherwise =rint maginar roots. /oto step ;.Step *: print rootsStep ;: stop
1*
8/12/2019 c and ds manuaL
17/259
lo3chart/
Start
Bead a!b!c
# powb6b$)6a6c-!0.9
f d M 0
B1 # $b(- & "6a--
B" # $b$- &"6a--
Kutput B1! B"
Stop
f d## 0
B1#$b & " 6 a -
B"# $b & " 6 a-
maginarroots
true
false
1;
8/12/2019 c and ds manuaL
18/259
Program/
Linclude?stdio.hMLinclude?math.hMvoid main-
Nfloat a!b!c!r1!r"!dOclrscr-OprintfPEnter the values for equation:P-OscanfP7f7f7fP!Ra!Rb!Rc-O&6 chec5 the condition 6&ifa##0-printfPEnter value should not be Iero P-OelseN d#b6b$)6a6cO
&6 chec5 the condition 6& ifdM0- N r1#$b(sqrtd-&"6a--O r"#$b$sqrtd-&"6a--O printfProots are realand unequalQnP-O printfP7fQn7fQnP!r1!r"-O
else ifd##0-
N r1#$b&"6a-O r"#$b&"6a-O printfProots are real and equalQnP-O printfProot#7fQnP!r1-O printfProot#7fQnP!r"-O
else printfProots are imaginarP-O getch-O
,utut/
1. Enter the values for equation: 1! *! 8 Boots are real and equal Boot# $,.0000 Boot# $,.0000
1+
8/12/2019 c and ds manuaL
19/259
". Enter the values for equation: "! ;! * Boots are real and unequal Boot# $*.;9 Boot# $;."9,. Enter the values for equation: 1! "! ,
Boots are imaginar
4oncluion/he program is error free
5I5A 6789ATI,N9/
1$ What are various tpes of loop statements@Ans : While! do$ while! for loop statements
9- What is the difference between while and do$while statements@Ans: n while the condition will be chec5ed first and then enter into a loop. Hut in do$ while the statements will be e%ecuted first and then finall chec5 the
Condition.,- 2ow to find the roots of qudratric equtations @Ans: ature of roots of quadratic equation can be 5nown from the quadrant # b"$)ac
f b"$)ac M0 then roots are real and unequalf b"$)ac #0 then roots are real and equal
f b"$)ac ?0 then roots are imaginar
)- Gist out the C features @Ans: =ortabilit!fle%ibilit! wide acceptabilit etc..!
18
8/12/2019 c and ds manuaL
20/259
,bectie/'
o calculate the sum. Sum#1$%"&"'( %)&)'$ %*&*'( %+&+'$ %10&10'
Algorithm/main program:Step 1: start
Step ": declare %!i!n!s#0!cStep ,: read % valueStep ): for i#0 ! n#0O i?#10O i#i("! n(( goto step 9Step 9: s#s(pow$1!n-6pow%!i-&facti--Step *: print s valueStep ;: stop
9ub rogram/Step 1: while %'#0 goto Step "Step ": #(%O %Step ,: return
Step ): return to main program
"0
8/12/2019 c and ds manuaL
21/259
lo3chart/
Start
Bead %!!n!c
S # 0
#0!n#0 i?#10
# ( "! n((
S#s(pow$1!n-6pow%!i- & facti--
=rint s
Stop
Sub =rogram
While %'#0
X#6%
%$$
Beturn
Fact -
Beturn to mainprogram
"1
8/12/2019 c and ds manuaL
22/259
Program/
Linclude?stdio.hMLinclude?math.hMlong factint-Ovoid main-
N int %!i!nO float s#0!cO clrscr-O printfPQn enter the value of %QtP-O scanfP7dP!R%-O
&6perform the looping operation6& fori#0!n#0Oi?#10Oi#i("!n((- s#s(pow$1!n-6pow%!i-&facti--O printfPQn the result is 7fP!s-O getch-O
&6 calling sub program6&long factint %-N long int #1O while%'#0- N #6%O %$$O return O,utut/
1.Enter the valueof % : 1 he result is 0.9)0,0"" Enter the value of %: " he result is $0.)1*1994oncluion/he program is error free5I5A 6789ATI,N9/
1- What is function @Ans: A function is a sub program it returns a value.
"- What is procedure @Ans: A procedure is a sub program it does not returns a value
,- What are the basic data tpes in C @Ans: int! char! float! double)- 2ow to define preprocessor @Ans: H using the L smbal E%: Linclude?stdio.hM
""
8/12/2019 c and ds manuaL
23/259
,bectie/ (
=rograms that use recursive function to find the factorial of a given integer.
ecrition/
Factorial of a number is nothing but the multiplication of numbers from a given
number to 1
Algorithm/main programStep 1: startStep ": read nStep ,: call sub program as f#factn-Step ): print f valueStep 9: stop
9ub rogram/Step 1: initialiIe the f
Step ": if n# # 0 or n ## 1 return 1 to main program if not goto step ,Step ,: return n6factn$1- to main program
",
8/12/2019 c and ds manuaL
24/259
lo3chart/
Start
Bead n
Call subprogram F # factn-
output
Stop
9ub rogram
Fact -
f n#0 YY n#1
Beturn n6factn$1-
Beturn to mainprogram
true
false
")
8/12/2019 c and ds manuaL
25/259
Program/
Linclude?stdio.hMLinclude?conio.hMint factint n-
N int fO ifn##0-YYn##1-- && chec5 the condition for the n value returnn-O else f#n6factn$1-O &&calculate the factorial of n returnf-Ovoid main-N int nO
clrscr-O printfPenter the number :P-O scanfP7dP!Rn-O printfPfactoria of number7dP!factn--O getch-O,utut/
1. Enter the number : 9 Factorial of number: 1"0". Enter the number : , Factorial of number: *,. Enter the number : 8 Factorial of number: $,0,,*
4oncluion/the program is error free
5I5A 6789ATI,N9/
1- What is the meaning of factorial number@An / Factorial of a number is nothing but the multiplication of numbers from a given
number to 1"- What is the meaning of recusive function @
Ans: A function call it self is called recursive function,- define librar functions @
Ans: he functions have alread been written! compiled and placed in libraries and arecalled librar functions.
)- efine formal parameters @Ans: Formal parameters are the parameters given in the function declaration as
function definition.
"9
8/12/2019 c and ds manuaL
26/259
,bectie / )
=rogram that use non recursive function to find the factorial of a given integer.ecrition/
Factorial of a number is nothing but the multiplication of numbers from a givennumber to 1
E%: 9' #96)6,6"61# 1"0Algorithm/main programStep 1: startStep ": read nStep ,: call the sub program factn-Step ): print the f valueStep 9: stop9ub rogram/
Step 1: initialiIe the f#1Step ": if n##0 or n#1 return 1 to main program.f not goto step ,Step ,: perform the looping operation as follows
For i#1 i?#nO i((Step ): f#f6iStep 9: return f value to the main program
"*
8/12/2019 c and ds manuaL
27/259
lo3chart/
actorial nonrecurie
start
Bead i
Call subprogram Factn-
=rint outputTalue of fact
Stop
9ub rogram
Fact -
F # 1! i
f n ## 0 YYn ## 1
Beturn to main
program
# 1 i((
?#n
F # f 6 i
";
8/12/2019 c and ds manuaL
28/259
Program/
Linclude?stdio.hMLinclude?conio.hMint factint n- &&starting of the sub program
N int f#1!iO ifn##0-YYn##1-- && chec5 the condition for n value return1-O else fori#1Oi?#nOi((- && perform the looping operation for calculating the factorial f#f6iO
returnf-Ovoid main-N
int nO clrscr-O printfPenter the number :P-O
scanfP7dP!Rn-O printfPfactoria of number7dP!factn--O getch-O
,utut/
1.Enter the number: ; Factorial of number: 90)0". Enter the number: * Factorial of number: ;"0,. Enter the number: + Factorial of number: $"9"1*
4oncluion/
he program is error free
5I5A 6789ATI,N9/
1- What is meant b call b value @Ans: passing values to the function as arguments"- What is meant b call b reference @
Ans: passing address to the function as arguments,-define actual parameters @
Ans: he actual parameters often 5nown as arguments are specified in the function call.
"+
8/12/2019 c and ds manuaL
29/259
,bectie/ *
o find the /C of two given integers b using the recursive function
ecrition/
/C means /reatest Common ivisor. i.e the highest number which divides the
given numberE%: /C1"!")- is 1"Formula: /C# product of numbers& GC of numbers
Algorithm/main programStep 1: startStep ": read a!bStep ,: call the sub program /Ca!b- for print the valueStep ): stop
9ub rogram/
Step 1: if nMm return /Cn!m-Step ": if n##0 return m else goto step ,Step ,: return /C n!m7n-Step ): return to main program
"8
8/12/2019 c and ds manuaL
30/259
lo3chart/
Start
Bead a!b
Call sub program/#gcda!b-
=rint gcdvalue
Stop
/cd -
fnMm
Beturn/cd n!m-
f
n##0
Beturn m Call the same functionBeturn gcd n!m7n-
Beturn to mainprogram
true false
trueF
,0
8/12/2019 c and ds manuaL
31/259
Program/
Linclude?stdio.hMLinclude?conio.hMint gcdrecursiveint m!int n- && starting of the sub program
N ifnMm- return gcdrecursiven!m-O
ifn##0- return mO
else return gcdrecursiven!m7n-O && return to the mainprogramvoid main-N int a!b!igcdO
clrscr-O printfPenter the two numbers whose gcd is to be found:P-O scanfP7d7dP!Ra!Rb-O printfP/C of a!b is 7dP!gcdrecursivea!b--O && return to the sub program getch-O,utut/
1. enter the two numbers whose gcd is to be found:9!"9 /C of a!b is : 9". enter the two numbers whose gcd is to be found:,*!9) /C of a!b is : 1+,. enter the two numbers whose gcd is to be found:11!1, /C of a!b is : 14oncluion/
he program is error free
5I5A 6789ATI,N9/
1- What is meaning of /C @Ans: /C means /reatest Common ivisor. i.e the highest number which divides
the given number"- efine scope of a variable @Ans: he scope of a variable can be define as the region over which the variable is
accessible,- Show an scope resolution operator @Ans: double colon//-
)- efine e%tent of a variable @ Ans: he period of time during which memor is associated with a variable is called
e%tent of the variable.
,1
8/12/2019 c and ds manuaL
32/259
,bectie/ +
o find the /C of two given integers b using the non recursive function
ecrition/
/C means /reatest Common ivisor. i.e the highest number which divides the
given numberE%: /C1"!")- is 1"Formula: /C# product of numbers& GC of numbers
Algorithm/
Step 1: startStep ": read a!bStep ,: call sub program g#/Ca!b-Step ): print the g valueStep 9: stopSub program:
Step 1: initialiIe the p#1! q! remainderStep ": remainder#p$p&q6q-Step ,: remainder#0 return q else goto step )Step ): /Cq!remainder- return to main program
,"
8/12/2019 c and ds manuaL
33/259
lo3chart/
start
Bead a! b
Call subprogram g#gcda!b-
output
stop
/cd -
Bemainder#p$p&q6q-
fremainder##0
/cdq!remainder- Beturn q
Beturn to main program
false rue
,,
8/12/2019 c and ds manuaL
34/259
Program/
Linclude?stdio.hMLinclude?conio.hMLinclude?math.hMint gcdnonrecursiveint m!int n-
N int remainderO remainder#m$m&n6n-O ifremainder##0- return nO else gcdnonrecursiven!remainder-O
void main-N
int a!b!igcdO clrscr-O printfPenter the two numbers whosegcd is to be found:P-O scanfP7d7dP!Ra!Rb-O printfP/C of 7dP!gcdnonrecursivea!b--O getch-O
,utut/
1. enter the two numbers whose gcd is to be found:9!"9 /C of a!b is : 9". enter the two numbers whose gcd is to be found:,*!9) /C of a!b is : 1+,. enter the two numbers whose gcd is to be found:11!1, /C of a!b is : 1
4oncluion/
he program is error free
5I5A 6789ATI,N9/
1$What is meaning of /C @Ans:
/C means /reatest Common ivisor. i.e the highest number which divides thegiven number
,)
8/12/2019 c and ds manuaL
35/259
,bectie /10
o solve the towers of 2anoi problem b using the recursive functionecrition/
owers of 2anoi problem means we have three towers
2ere source !intermediate and destination are the three towers. We have totransfer all the dis5s from source to destination towers. 2ere the restriction is not
to place a big dis5 on smaller one . for this we use intermediate tower. Finall thearrangements in the destination tower must be as same as the dis5s in the sourcetower at first.
Algorithm/ main rogram
Step 1: startStep ": initialiIe the source#a! intermediate#c! destination # dStep ,: read nStep ): call the sub program 2anoi recursion n value!a !b! c-Step 9: stopSub program:
Step 1: if n## 1 call the sub program 2anoi recursion num$1! a! c! b-
Step ": print the output from a to bStep ,: call the sub program 2anoi recursionnum$1! b! c! a-Step ): return to main program
Source intermediate destination
,9
8/12/2019 c and ds manuaL
36/259
lo3chart/
SAB
SKJBCE # AEBEAE # CESAK # H
BEA n
Call subprogram2anoinum!source!intermediate!destination- A!C! H
Stop
=rint A!C
Beturn to mainprogram
hanoirecursive -
f num##1
=rint A!C
Call ubprogram2anoinum$1!H!C!A--
true false
Call sbgroram
um$1!source ZA[
,*
8/12/2019 c and ds manuaL
37/259
Program/
Linclude?stdio.hMLinclude?conio.hMvoid 2anoirecursionint num!char ndl1!char ndl"!char ndl,-N
ifnum##1- N printfPove top dis5 from needle 7c to needle 7cP!ndl1!ndl"-O returnO 2anoirecursionnum$1!ndl1!ndl,!ndl"-O printfPove top dis from needle 7c to needlle 7cP!ndl1!ndl"-O 2anoirecursionnum$1!ndl,!ndl"!ndl1-O
void main-
N int noO clrscr-O printfPEnter the no. of dis5 to be transferred:P-O scanfP7dP!Rno-O ifno?1- printfPQn here\s nothing to moveP-O else printfPQn recursiveP-O 2anoirecursionno!\A\!\H\!\C\-O getch-O,utut/
1. Enter the no. of dis5 to be transferred :, ove top dis5 from needle a to needle b
ove top dis5 from needle a to needle c ove top dis5 from needle b to needle c ove top dis5 from needle a to needle b ove top dis5 from needle c to needle a ove top dis5 from needle c to needle b ove top dis5 from needle a to needle b4oncluion/
he program is error free5I5A 6789ATI,N9/
1$ #hat i uroe of to3er of ;anoi :
An/ We have to transfer all the dis5s from source to destination towers. 2ere therestriction is not to place a big dis5 on smaller one . for this we use intermediatetower. Finall the arrangements in the destination tower must be as same as thedis5s in the source tower at first.
,;
8/12/2019 c and ds manuaL
38/259
,bectie /11
o solve the towers of 2anoi problem b using the non recursive functionecrition/
owers of 2anoi problem means we have three towers
2ere source !intermediate and destination are the three towers. We have totransfer all the dis5s from source to destination towers. 2ere the restriction is not
to place a big dis5 on smaller one . for this we use intermediate tower. Finall thearrangements in the destination tower must be as same as the dis5s in the sourcetower at first.
Algorithm/
Step 1: startStep ": declare the noStep ,: read the no value
Step ): if no?1- =rint nothing to moveElse =rint nonrecursion
Step 9:2anoi non recursionno!.A4!4H4!4C4-Step *:stopSub program:
Step 1: eclare num!sndl!indl!dndl! st5n] ^!st5sndl] ^!st5dndl ] ^!st5add] ^! temp!top!addStep ": declare the top#JGGStep ,: one:
fnum##1-then=rint the out put value/oto four
Step ): two: op#top(1St5n]top^#numSt5sndl]top^#sndlSt5indl]top^#indlSt5dndl]top^#dndlSt5add]top^#,um#num$1
Source intermediate destination
,+
8/12/2019 c and ds manuaL
39/259
Sndl#sndlemp#indlndl#dndlndl#temp/oto one. /oto step ,
Step 9: hree:=rint the outputop#top(1St5n]top^#numSt5sndl]top^#sndlSt5indl]top^#indlSt5dndl]top^#dndlSt5add]top^#9um#num$1temp#sndl
sndl#indlndl#tempndl#dndl/oto one. /oto step ,
Step *:Four:
ftop##JGG-Beturn to main programum# st5n]top^Sndl# st5sndl]top^ndl# st5indl]top^ndl#st5dndl]top^Add#st5add]top^op#top$1fadd##,-/oto three. /oto step 9Elsefadd##9-/oto four. /oto step *.
Step ;: return to main program
,8
8/12/2019 c and ds manuaL
40/259
lo3 chart/
)0
Start
Bead no
f no? 1 =rint nothing tomove
true false
=rintnonrecursion
2anoinonrecursion no!.A4!4H4!4C4-
Stop
8/12/2019 c and ds manuaL
41/259
2anoi nonrecursion -
eclare num!sndl!indl!dndl! st5n] ^!st5sndl] ^!st5dndl ] ^!st5add] ! temp!top!add
op # JGG
f num##1=rint the value
true
ftop#JGG
um# st5n]top^Sndl# st5sndl]top^ndl# st5indl]top^
ndl#st5dndl]top^Add#st5add]top^op#top$1
fadd##,
false
fadd##9
op#top(1St5n]top^#numSt5sndl]top^#sndlSt5indl]top^#indlSt5dndl]top^#dndlSt5add]top^#,um#num$1Sndl#sndlemp#indl
ndl#dndlndl#temp
one
four
two
=rint value
op#top(1St5n]top^#numSt5sndl]top^#sndlSt5indl]top^#indlSt5dndl]top^#dndlSt5add]top^#9
um#num$1temp#sndlsndl#indlndl#tempndl#dndlop#top(1
)1
8/12/2019 c and ds manuaL
42/259
Program/
Linclude?stdio.hMLinclude?conio.hMvoid 2anoinonrecursionint num!char sndl!char indl!char dndl-N
char st5n]100^!st5sndl]100^!st5indl]100^!st5dndl]100^!st5add]100^!tempOint top!addOtop#JGGOone: ifnum##1- N printfPQn ove top dis5 from needle 7c to needle 7cP!sndl!dndl-O goto fourO two: top#top(1O
st5n]top^#numO st5sndl]top^#sndlO st5indl]top^#indlO st5dndl]top^#dndlO st5add]top^#,O num#num$1O sndl#sndlO temp#indlO indl#dndlO dndl#tempO goto oneO
three: printfPQn ove top dis5 from needle 7c to needle 7cP!sndl!dndl-O top#top(1O st5n]top^#numO st5sndl]top^#sndlO st5indl]top^#indlO st5dndl]top^#dndlO st5add]top^#9O num#num$1O temp#sndlO sndl#indlO indl#tempO dndl#dndlO goto oneO
four: iftop##JGG- returnO
)"
8/12/2019 c and ds manuaL
43/259
num#st5n]top^O sndl#st5sndl]top^O indl#st5indl]top^O dndl#st5dndl]top^O add#st5add]top^O
top#top$1O ifadd##,- goto threeO else ifadd##9- goto fourO void main-N int noO clrscr-O printfPEnter the no. of diss to be transferred:P-O
scanfP7dP!Rno-O ifno?1- printfPQn here\s nothing to moveP-O else printfPQn nonrecursiveP-O 2anoinonrecursionno!\A\!\H\!\C\-O getch-O
,utut/
1.Enter the no. of diss to be transferred:, nonrecursiveove top dis5 from needle A to needle Cove top dis5 from needle A to needle Hove top dis5 from needle C to needle Hove top dis5 from needle A to needle Cove top dis5 from needle H to needle Aove top dis5 from needle H to needle Cove top dis5 from needle A to needle C4oncluion/he program is error freed
5I5A 6789ATI,N9/
1$ #hat i uroe of to3er of ;anoi :
An/ We have to transfer all the dis5s from source to destination towers. 2ere therestriction is not to place a big dis5 on smaller one . for this we use intermediatetower. Finall the arrangements in the destination tower must be as same as thedis5s in the source tower at first.
2$ #hat i an arra< :
Ans: An arra is a sequence of memor location of same data tpe.
),
8/12/2019 c and ds manuaL
44/259
,bectie/ 12
he total distance travelled b vehicle in \t\ seconds is given b distance #ut(1&"at" where \u\ and \a\ are the initial velocit m&sec.- and accelerationm&sec"-. Write C program to find the distance travelled at regular intervals oftime given the values of \u\ and \a\. he program should provide the fle%ibilit to
the user to select his own time intervals and repeat the calculations for differentvalues of \u\ and \a\.ecrition/
he total distance travelled b vehicle in \t\ seconds is given b distance #ut(1&"at" where \u\ and \a\ are the initial velocit m&sec.- and accelerationm&sec"-.
Algorithm/
Step 1:Start
Step" : Bead t !dt
Step ,: Set i to 1
Step ):Set 5 to dt
Step 9: Bead u!a
Step *: set s to u65(0.96d6565
Step ;: Write s
Step +: f5?#t- and i#1 thenHeginStep +.1 go to step *
And Else
HeginStep +." :readStep +., :ifU#0- thenHegin
Step +.,.1:Set to 0End
ElseHegin
Step +.,.": Set to 1Step +.,.,: go to step )
EndStep 8: Stop
Step 10: End
))
8/12/2019 c and ds manuaL
45/259
lo3chart/
)9
8/12/2019 c and ds manuaL
46/259
Program/
Linclude?stdio.hMmain-
N int a!u!t!t1!t"!iOfloat sOclrscr-OprintfPEEB 2E TAGJES KF a!u!t!t1!t":P-OscanfP7d7d7d7d7dP!Ra!Ru!Rt!Rt1!Rt"-Ofori#t1Oi?#t"Oi#i(t- && performing the looping operation for time intervalsN
s#u6i-(0.96a6i6i-O && calculate the total distanceprintfPQnQnthe distance travelled in 7d seconds is 7f P!i!s-O
getch-O
Inut=,utut/
1.EEB 2E TAGJES KF a!u!t!t1!t":1 " , 1 9 the distance travelled in 1 seconds is ".900000
the distance travelled in ) seconds is 1*.000000".EEB 2E TAGJES KF a!u!t!t1!t":0 1 " , ) the distance travelled in , seconds is ,.000000concluion/he program is error free
5I5A 6789ATI,N9/
1- 2ow man tpes of arras are there @Ans: hree tpes. he are one dimensional !two dimensional and multi dimensionalarrs
)*
8/12/2019 c and ds manuaL
47/259
,bectie/1%
wo integer operands and one operator form user! performs the operationand then prints the result.
Consider the operators (!$!6! &! 7 and use Switch Statement-ecrition/o ta5e the two integer operands and one operator from user to performthe some arithmetic operations b using the following operators li5e(!$!6! &! 7E%: "(,#9
Algorithm/
Step 1: Start
Step ": Bead the values of a!b and operator
Step ,: if the operator is 3(4 thenB#a(b/o to step +Hrea5
Step ): Else if the operator is 3$3 thenB#a$b/o to step +
Step 9: Else if the operator is 363 thenB#a6b/o to step +
Step *: Else if the operator is 3&3 thenB#a&b/o to step +
Step ;: Else if the operator is 373 thenB#a7b/o to step +
Step +: write B
Step 8:End
);
8/12/2019 c and ds manuaL
48/259
lo3chart/
)+
8/12/2019 c and ds manuaL
49/259
Program/
Linclude?stdio.hMmain-N
char opOfloat a!b!cOclrscr-OprintfPenter two operands:P-OscanfP7d7dP!Ra!Rb-OprintfPenter an operator:P-OscanfP 7cP!Rop-Oswitchop- && used to select particular case from the userNcase \(\:printfPsum of two numbers 7"d 7"d is: 7dP!a!b!a(b-O
brea5O
case \$\:printfPsubtraction of two numbers 7"d 7"d is:7dP!a!b!a$b-Obrea5O
case \6\:printfPproduct of two numbers 7"d 7"d is:7dP!a!b!a6b-O
brea5O case \&\:printfPquotient of two numbers 7"d 7"d is:
7dP!a!b!a&b-O brea5O
case \7\:printfPreminder of two numbers 7"d 7"d is:7dP!a!b!c-O
brea5O default:printfPplease enter correct operatorP-O
brea5O getch-O
Inut=,utut/
1.enter two operands:" , enter an operator:( sum of two numbers " , is: 9
".enter two operands:, ) enter an operator: $ subtraction of two numbers , ) is: $1
,.enter two operands:, 9
)8
8/12/2019 c and ds manuaL
50/259
enter an operator:6 product of two numbers , 9 is: 19
).enter two operands:9 " enter an operator:&
quotient of two numbers 9 " is: "9. enter two operands:9 " enter an operator:7 reminder of two numbers 9 " is: 1
concluion/he program is error free
5I5A 6789ATI,N9/
1- What are the various tpes of arithemetic operators@
Ans: addition (-! multiplication6-! subtraction $-! division&- ! modulo7-."- What are the tpes of relational operators @ Ans: less than?-! grater thanM-! less than or equal to?#-!equal to##-! etc..!
,- ,- What are the tpes of logical operators @Ans: logical A RR-! logical KBYY-! logical K'-
90
8/12/2019 c and ds manuaL
51/259
,bectie / 1&
o find both the largest and smallest number in a list of integersecrition/
his program contains n number of elements! in these elements we can find thelargest and smallest numbers and displa these two numbers
Algorithm/Step 1: startStep ": read nStep ,: initialiIe i#0Step ): if i?n do as follows. f not goto step 9
Bead a]i^ncrement i/oto step )
Step 9: min#a]0^! ma%#a]0^Step *: initialiIe i#0Step ;: if i?n do as follows. f not goto step +
f a]i^?minAssign min#a]i^ncrement i goto Step ;
Step +: print min!ma%Step 8: stop
91
8/12/2019 c and ds manuaL
52/259
Bead n!a]i^!min!ma%!
in # a]0^ a% # a]0^
=rintmin!ma%
# 0 i((
? n
a%?min
A]i^Mma
%
in#a]i^
a%#a]i^
# 0
?n i((
stop
Bead a]i^
Start
true
false
true
false
false
true
lo3chart/
9"
8/12/2019 c and ds manuaL
53/259
Program/
Linclude?stdio.hMvoid main-N int a]10^!i!n!min!ma%O
clrscr-O printfPenter the arra siIe:P-O scanfP7dP!Rn-O printfPEnter the elements of arraP-O fori#0Oi?nOi((- && read the elements of an arra scanfP7dP!Ra]i^-O min#a]0^O ma%#a]0^O fori#0Oi?nOi((-&& read the elements of an arra N ifa]i^?min-&& chec5 the condition for minimum value
min#a]i^O ifa]i^Mma%-&&chec5 the condition for ma%imum value ma%#a]i^O printfPma%imum value is:7dQnP!ma%-O printfPminimum value is:7dQnP!min-O getch-O,utut/
1.enter the arra siIe:) Enter the elements of arra ,* 1, " )9 ma%imum value is:)9 minimum value is:"".enter the arra siIe:9 Enter the elements of arra * " 1 , + ma%imum value is:+ minimum value is:1,.enter the arra siIe:9 Enter the elements of arra$* 8 $8 " 9 ma%imum value is:8 minimum value is:$8concluion/the program is error free5I5A 6789ATI,N9/
1$ What is an arra:Ans: he collection of similar elements is called arra
"- 2ow man tpes of arras are there @Ans: hree tpes. he are one dimensional !two dimensional and multi dimensionalarrs
9,
8/12/2019 c and ds manuaL
54/259
,bectie/1'
o perform the addition of two matricesecrition/
program ta5es the two matri%es of same siIe and performs the addition an alsota5es the two matri%es of different siIes and chec5s for possibilit of
multiplication and perform multiplication if possible.algorithm/Step 1: startStep ": read the siIe of matrices A!H < m!nStep ,: read the elements of matri% AStep ): read the elements of matri% HStep 9: select the choice for ou want. f ou select case 1 then goto matric
addition. Else goto Step ;.Step *: print Sum of matri% A and HStep ;: if ou select case " then goto matri% multiplicationStep +: chec5 if n#p! if not print matrices can not be multiplied
Step 8: Ktherwise perform the multiplication of matricesStep 10: =rint the resultant matri%Step 11: Stop
9)
8/12/2019 c and ds manuaL
55/259
start
Bead the siIeof A!H matri%m!n
#0
? r1 i((
eclare a]^]^!b]^
]^!c]^]^!ch!!U!5!m!n!p!q!r1!c1
Bead the choice ch
f ch?#"RchM0
=rintvalidchoice
Switchch
Bead the siIeof A matri%m!n
Bead A]i^]U^
V#0
U? c1 Ui((
#0
? r1 i((
U#0
U?c1 U((
Bead H]i]U^
#0
? r1 i((
U#0
U?c1 U((
Bead A]i^]U^
Bead siIe ofmatri% H: p.q
#0
? p i((
H
Case 1Case "
lo3 chart/
99
8/12/2019 c and ds manuaL
56/259
H
#0
? r1 i((
U#0
U? c1 U((
=rint A]i^]U^(H]i^]U^
Stop
H
U#0
U? q U((
Bead H]i^]U^
fn##p
atri% cannotbe mutiplied
i#0
i?m i((
U#0
U? q U((
C]i^]U^#0
50
U? n 5((
C]i^]U^#C]i^]U^(A]i^]5^6H]5^]U^
i#0
i?m i((U((
U#0
U?q U((
=rint C]i^]U^
A
9*
8/12/2019 c and ds manuaL
57/259
Program/
Linclude?stdio.hM
void main-Nint ch!i!U!m!n!p!q!5!r1!c1!a]10^]10^!b]10^]10^!c]10^]10^Oclrscr-OprintfP666666666666666666666666666666666666P-OprintfPQnQtQtEJP-OprintfPQn6666666666666666666666666666666666P-OprintfPQn]1^AK KF WK ABCESP-OprintfPQn]"^JG=GCAK KF WK ABCESP-OprintfPQn]0^E_P-OprintfPQn6666666666666666666666666666666666P-O
printfPQnQtEnter our choice:QnP-OscanfP7dP!Rch-O
ifch?#" R chM0-N
printfPTalid ChoiceQnP-O
switchch-N
case 1: printfPnput rows and columns of A R H atri%:P-O scanfP7d7dP!Rr1!Rc1-O
printfPEnter elements of matri% A:QnP-O fori#0Oi?r1Oi((- N forU#0OU?c1OU((- scanfP7dP!Ra]i^]U^-O
printfPEnter elements of matri% H:QnP-O fori#0Oi?r1Oi((- N forU#0OU?c1OU((- scanfP7dP!Rb]i^]U^-O printfPQn #####atri% Addition#####QnP-O fori#0Oi?r1Oi((- N ForU#0OU?c1OU((- printfP79dP!a]i^]U^(b]i^]U^-O
9;
8/12/2019 c and ds manuaL
58/259
printfPQnP-O brea5O
case ":
printfPnput rows and columns of A matri%:P-O scanfP7d7dP!Rm!Rn-O printfPnput rows and columns of H matri%:P-O scanfP7d7dP!Rp!Rq-O ifn##p- N printfPmatrices can be multipliedQnP-O printfPresultant matri% is 7d67dQnP!m!q-O printfPnput A matri%QnP-O read`matri%a!m!n-O printfPnput H matri%QnP-O
&6Function call to read the matri%6& read`matri%b!p!q-O &6Function for ultiplication of two matrices6& printfPQn #####atri% ultiplication#####QnP-O fori#0Oi?mO((i-
forU#0OU?qO((U- N c]i^]U^#0O for5#0O5?nO((5-
c]i^]U^#c]i^]U^(a]i^]5^6b]5^]U^O
printfPBesultant of two matrices:QnP-O write`matri%c!m!q-O &6end if6&
else N
printfPatrices cannot be multiplied.P-O
&6end else6& brea5O
case 0: printfPQn Choice erminatedP-O e%it-O brea5O
default: printfPQn nvalid ChoiceP-O
9+
8/12/2019 c and ds manuaL
59/259
getch-O
&6Function read matri%6&
int read`matri%int a]10^]10^!int m!int n- N int i!UO fori#0Oi?mOi((-
forU#0OU?nOU((-scanfP7dP!Ra]i^]U^-O return 0O
&6Function to write the matri%6&int write`matri%int a]10^]10^!int m!int n-
N int i!UO fori#0Oi?mOi((- N
forU#0OU?nOU((-printfP79dP!a]i^]U^-OprintfPQnP-O
return 0O
,utut/
1.666666666666666666666666666666666666 EJ6666666666666666666666666666666666]1^AK KF WK ABCES]"^JG=GCAK KF WK ABCES]0^E_6666666666666666666666666666666666 Enter our choice:1Talid Choicenput rows and columns of A R H atri%:""Enter elements of matri% A:"""
98
8/12/2019 c and ds manuaL
60/259
"Enter elements of matri% H:"""
"
#####atri% Addition##### ) ) ) )666666666666666666666666666666666666 EJ6666666666666666666666666666666666]1^AK KF WK ABCES]"^JG=GCAK KF WK ABCES]0^E_
6666666666666666666666666666666666 Enter our choice:"Talid Choicenput rows and columns of A matri%:",nput rows and columns of H matri%:""atrices cannot be multiplied.
666666666666666666666666666666666666 EJ6666666666666666666666666666666666]1^AK KF WK ABCES]"^JG=GCAK KF WK ABCES]0^E_6666666666666666666666666666666666 Enter our choice:"Talid Choicenput rows and columns of A matri%:""nput rows and columns of H matri%:""matrices can be multipliedresultant matri% is "6"nput A matri%"""
*0
8/12/2019 c and ds manuaL
61/259
"nput H matri%"""
"
#####atri% ultiplication#####Besultant of two matrices: + + + +4oncluion /he program is error free
5I5A 6789ATI,N9/
1- What is condition for performing an matric addition @
Ans: program ta5es the two matri%es of same siIe and performs the addition"- What is condition for performing an matric addition @Ans: he two matri%es of different siIes and chec5s for possibilit of multiplicationand perform multiplication if possible
*1
8/12/2019 c and ds manuaL
62/259
,bectie /1(
Functions to insert a sub string into given main string from a given position
ecrition/in this program we need to insert a string into another string from a specified
position.Algorithm/Step 1: startStep ": read main string and sub stringStep ,: find the length of main stringr-Step ): find length of sub stringn-Step 9: cop main string into sub stringStep *: read the position to insert the sub string p-Step ;: cop sub string into main string from positionp$1Step +: cop temporar string into main string from position p(n$1Step 8: print the strings
Step 10: stop
*"
8/12/2019 c and ds manuaL
63/259
Start
Bead the strings A
R H
# 0
? r
C]i^ # A]i^
((
S # n ( rK # p ( n
#p ((
? s
_# C]i^
f t ? n
A
H
lo3 chart/
*,
8/12/2019 c and ds manuaL
64/259
A
A]i^ # H]t^
#t(1
A]o^#%
K # o ( 1
H
=rint output
Stop
*)
8/12/2019 c and ds manuaL
65/259
Program/
Linclude ?stdio.hMLinclude ?conio.hMLinclude ?string.hM
void main-N
char a]10^Ochar b]10^Ochar c]10^Oint p#0!r#0!i#0Oint t#0Oint %!g!s!n!oOclrscr-O
putsPEnter First String:P-Ogetsa-OputsPEnter Second String:P-Ogetsb-OprintfPEnter the position wherethe item has to be inserted: P-OscanfP7dP!Rp-Or # strlena-On # strlenb-Oi#0O
&& Coping the input string into another arrawhilei ?# r-N
c]i^#a]i^Oi((O
s # n(rOo # p(nO
&& Adding the sub$stringfori#pOi?sOi((-N
% # c]i^Oift?n-N
a]i^ # b]t^O t#t(1O
a]o^#%Oo#o(1O
*9
8/12/2019 c and ds manuaL
66/259
printfP7sP! a-Ogetch-O
,utut/
1.enter first string: computer".enter second string: gec,.enter the position where the item has to be inserted:, comgecputerconcluion /the program is error free
5I5A 6789ATI,N9/
1- What is string @Ans: A string is an collection of characters"- Which command is used to combined the two strings @Ans: Strcat-,- Which command is used to cop the strings @Ans: H using the strcp- function copies one string to another
**
8/12/2019 c and ds manuaL
67/259
,bectie/1)
o delete n characters from a given position in a given stringecrition/
in this program we need to delete a string from the given string at a specifiedposition.
Algorithm/Step 1: startStep ": read stringStep ,: find the length of the stringStep ): read the value of number of characters to be deleted and positionedStep 9: string cop part of string from position to end! and position(number of
characters to end-Step *: stop
*;
8/12/2019 c and ds manuaL
68/259
Start
Bead string
Bead position! no ofcharacters
elchar string! n! pos-
Stop
Subprogramelchar -
f a(b$1`?#
strlen%--
StrcpR%]b$1^!R%]a(b$1^-
=uts%-
Beturn tomainprogram
lo3 chart/
*+
8/12/2019 c and ds manuaL
69/259
Program/
Linclude ?stdio.hMLinclude ?conio.hMLinclude ?string.hM
void delcharchar 6%!int a! int b-O
void main-N char string]10^O int n!pos!pO clrscr-O
putsPEnter the stringP-O getsstring-O printfPEnter the position from where to deleteP-O
scanfP7dP!Rpos-O printfPEnter the number of characters to bedeletedP-O scanfP7dP!Rn-O delcharstring! n!pos-O getch-O
&& Function to delete n charactersvoid delcharchar 6%!int a! int b-N if a(b$1- ?# strlen%--
N strcpR%]b$1^!R%]a(b$1^-O puts%-O ,utut/
1.enter the string nagaraUu Enter the position from where to delete:) Enter the number of charcters to be deleted, nagUu". enter the string 5aliraUu Enter the position from where to delete:0 Enter the number of charcters to be deleted) BaUu4oncluion/the program is error free
*8
8/12/2019 c and ds manuaL
70/259
5I5A 6789ATI,N9/
1- Which command is used to delete the strings @Ans: delstr-O"- What are the various tpes of string functions @Ans: Strcat-! strcp-! delstr-! substr- !strlen-etc..!
;0
8/12/2019 c and ds manuaL
71/259
,bectie/ 1*
o determine if the given string is a palindrome or not
ecrition/
if the reverse of a string is equal to original string then it is called palindrome
Algorithm/
Step 1:startStep ": read the stringStep ,: store reverse of the given string in a temporar stringStep ): compare the two stringsStep 9: if both are equal then print palindromeStep *: otherwise print not palindromeStep ;: stop
;1
8/12/2019 c and ds manuaL
72/259
Start
Bead string
fspalindromestring-
spalindromestring-
=rintpalindrome
true
=rint notpalindrome
Stop
lo3 chart/
;"
8/12/2019 c and ds manuaL
73/259
spalindrome -
Enum Hoolean matched# true
f len## 0
Beturn 0
Geft # 0
Bight#len$1
Whileleft?rightRRmatched
fstring]left^'#string]right^-
atched#false
true
Geft ((Bight $$
Beturnmatched
Beturn to mainprogram
;,
8/12/2019 c and ds manuaL
74/259
Program/
Linclude?stdio.hMLinclude?string.hM
enum HooleanNfalse!trueOenum Hoolean s=alindromechar string]^-N int left!right!len#strlenstring-O enum Hoolean matched#trueO
iflen##0- return 0O left#0O
right#len$1O
&6 Compare the first and last letter!second R second last R so on 6& whileleft?rightRRmatched- N
ifstring]left^'#string]right^- matched#falseO else N left((O right$$O return matchedO
int main-N char string])0^O clrscr-O printfP6666=rogram to test if the given string is a palindrome6666QnP-O printfPEnter a string:P-O scanfP7sP!string-O ifs=alindromestring-- printfPhe given string 7s is a palindromeQnP!string-O else printfPhe given string 7s is not a palindromeQnP!string-O getch-O return 0O
;)
8/12/2019 c and ds manuaL
75/259
,utut/
1. Enter the string:malaalam he given string malaalam is a palindrome". Enter the string:india he given string india is not a palindrome
4oncluion/he program is error free
5I5A 6789ATI,N9/
1- What is meant b palindrome @Ans: f the reverse of a string&number is equal to original string& number then it iscalled palindrome.
"- What is the use of gets- function @Ans: o read the string at a time
,- What is the use of puts- function @Ans: o write the string at a time
;9
8/12/2019 c and ds manuaL
76/259
,bectie/ 1+
=rogram that displas the position or inde% in the string S where the string
begins ! or $1 if S doesn4t contain Algorithm/Step 1: startStep ": read the string and then displaedStep ,: read the string to be searched and then displaedStep ): searching the string in string S and then perform the following steps
i. found#strstrS!-ii. if found print the second string is found in the first string at the
position. f not goto step 9Step 9: print the $1Step *: stop
;*
8/12/2019 c and ds manuaL
77/259
Start
nitialiIe s] ^!t] ^! found variables
Bead first string
ispla thestring
Bead string to besearched
ispla thestring
Found#strstrs!t-
f found
=rint $1=rint the stringFound$s
Stop
no es
lo3 chart/
;;
8/12/2019 c and ds manuaL
78/259
Program/
Linclude?stdio.hMLinclude?string.hMLinclude?conio.hM
void main-N char s],0^! t]"0^O
char 6foundO clrscr-O
&6 Entering the main string 6& putsPEnter the first string: P-O getss-O
&6 Entering the string whose positionor inde% to be displaed 6& putsPEnter the string to be searched: P-O getst-O
&6Searching string t in string s 6& found#strstrs!t-O iffound- printfPSecond String is found in the First String at 7d position.QnP!found$s-O else printfP$1P-O getch-O
,utut/
1.enter the first string: 5ali Enter the string to be seareched: li second string is found in the first string at"position".enter the first string: nagaraUu Enter the string to be seareched: raUu second string is found in the first string at)position,.enter the first string: nagarUuna Enter the string to be seareched:
ma $14oncluion/he program is error free
;+
8/12/2019 c and ds manuaL
79/259
5I5A 6789ATI,N9/
1- What is the difference between printf- and puts- @Ans: puts- is used to displa the string at a time and it doesn4t ta5e an integersvalues but printf- ta5es an values as defined b the user
"- define pointer variable @Ans: pointer variables are defined as variable that contain the memor addresses ofdata or e%ecutable code.,- What is use of the strcmp- function @Ans: his function compares two strings character b character and returns a value 0if both strings are equal and non Iero value if the strings are different.
;8
8/12/2019 c and ds manuaL
80/259
,bectie/ 20
o count the lines!words R charcters in a given te%tecrition/
n this program we have to count the no of lines! no of words and no ofcharacters in a given program or given te%t b using the string function
Algorithm/Step 1: StartStep ": Bead the te%t until an empt lineStep ,: Compare each character with newline char 3Qn4 to countno of linesStep ): Compare each character with tab char 3QtQ4 or space char 3 3 to count no
of wordsStep 9: Compare first character with JGG char 3Q04 to find the end of te%tStep *: o of characters # length of each line of te%tStep ;: =rint no of lines! no of words! no of charsStep +: Stop
+0
8/12/2019 c and ds manuaL
81/259
Start
nitialiIe end#0!chars#0!words#0!lines#0
WhileEnd##0
C # 0
true
f ctr#getchar--'#4Qn4
false
Gine]c^#4Q04
Gine]c((^#ctr
true
fline]0^#4Q04
true
Words (( # 0
(( line]i^'#4Q0Q
false
f line]i^##4 3YYGine]i^##4Qt4
Gines((Chars(#strlenline-
Words ((
=rint lines!Words!chars
stop
lo3 chart/
+1
8/12/2019 c and ds manuaL
82/259
Program/
Linclude ?stdio.hMmain-N
char line]+1^! ctrO
int i!c! end # 0!characters # 0!words # 0!lines # 0O
printfPEX 2E E_.QnP-OprintfP/TE KE S=ACE AFEB EAC2 WKB.QnP-OprintfPW2E CK=GEE! =BESS \BEJB\.QnQnP-Owhile end ## 0-N
&6 Beading a line of te%t 6&
c # 0Owhilectr#getchar-- '# \Qn\-line]c((^ # ctrO
line]c^ # \Q0\O&6 counting the words in a line 6&ifline]0^ ## \Q0\-
brea5 OelseN
words((Ofori#0O line]i^ '# \Q0\Oi((-
ifline]i^ ## \ \ YY line]i^ ## \Qt\-words((O
&6 counting lines and characters 6&lines # lines (1Ocharacters # characters ( strlenline-O
printf PQnP-OprintfPumber of lines # 7dQnP! lines-OprintfPumber of words # 7dQnP! words-OprintfPumber of characters # 7dQnP! characters-O
,utut
1.EX 2E E_. /TE KE S=ACE AFEB EAC2 WKB. W2E CK=GEE! =BESS \BEJB\. Admiration is a ver short$lived passion. Admiration involves a glorious obliquit of vision. Alwas we li5e those who admire us but we do not
+"
8/12/2019 c and ds manuaL
83/259
li5e those whom we admire. Fools admire! but men of sense approve. umber of lines # 9 umber of words # ,* umber of characters # "09
4oncluion/he program is error free
5I5A 6789ATI,N9/
1- What is use of strlen- @Ans: to read a string length"- what is the use of getc- function @Ans: o read the character one b one.,- What is the use of strstr - @
Ans: he function strstr- searches one string for the occurrence of another.t accepts
two strings as parameters and searches the first string for an occurrence of thesecond
+,
8/12/2019 c and ds manuaL
84/259
,bectie 21/
o generate =ascal4s triangleecrition/
=ascal4s triangle which is used for a coefficient in the equation in
polnominals.Alogrithm/ Step 1: Start
Step ": nitialiIe m#0Step ,: Bead nStep ): f m?n goto step 9.if not goto step 1"Step 9: initialiIe i#)0$mStep *: f iM0 is true do as follows. f not goto step ;
i. print white spaceii. decrement i
iii. goto Step *
Step ;: nitialiIe U#0Step +: f U#m do as follows. f not goto Step 10i- ifU##0YYm##0-ii- nitialiIe b#1 if not b#b6m$U(1-&Uiii- =rint white space! b .iv- /oto Step 8
Step 8: increment U! goto Step +Step 10: print new line controlStep 11: increment m! goto step )Step 1": Stop
+)
8/12/2019 c and ds manuaL
85/259
lo3 chart/
Start
Bead p
#0 ? p
((
trueB#)0$ r$$
rM0
=rint whitespace
true
_#0 %?#i
_((
false
true
f %##0YY
##0
falsetrue
H#1 H#b6i$%(1-&%
=tint b
F
=rintnewlin
e
Stop
false
+9
8/12/2019 c and ds manuaL
86/259
Program/
Linclude?stdio.hMLinclude?conio.hMvoid main-
N int i!p!r!%!binom#1O clrscr-O
printfPenter the how man lines to printP-O scanfP7dP!Rp-O
i#0O whilei?p- && chec5 the condition
N forr#)0$iOrM0Or$$- && perform the looping operation until 0 printfP P-O for%#0O%?#iO%((-
N if%##0-YYi##0-- && chec5 the conditionbinom#1Oelsebinom#binom6i$%(1-&%OprintfP7dP!binom-OprintfP P-O
printfPQnP-O i((O
getch-O
,utut/
1.enter the how man lines to print9 1 1 1 1 " 1 1 , , 1 1 ) * ) 1
".enter the how man lines to print, 1 1 1 1 " 14oncluion/the program is error free
+*
8/12/2019 c and ds manuaL
87/259
5I5A 6789ATI,N9/
1$ What is meant b =ascal4s triangle @
An/=ascal4s triangle which is used for a coefficient in the equation in polnominals"-define structure @
Ans: A structure in c is a heterogenous user efined data tpe. A structure ma containdifferent data tpes.t groups variables into a single entit.
+;
8/12/2019 c and ds manuaL
88/259
,bectie 22: o construct a pramid of numbersecrition/
n this program the we have to construct output in the pramid shape mannerAlgorithm/
Step 1: Start Step": initialiIe the num!!! %#,9 Step,: read the num Step):perform the loop operation
For#0O?#numO((- Step9:/oto%%!(1- Step*: perform the loop operation for displaing digits towards the left and right Fori#0$Oi?#Oi((- Step;: print absi-O Step+: %#%$"O Step8: Stop
++
8/12/2019 c and ds manuaL
89/259
lo3 chart/
Start
nitialiIenum!!!%#,9
Bead num
X # 0 ?#num
((
true
/oto%%!(1-
# 0 i((
?#
=rintabsi-
_#%$"
Stop
+8
8/12/2019 c and ds manuaL
90/259
Program/
Linclude?stdio.hMLinclude?conio.hM
void main-N int num!i!!%#,9O
clrscr-OprintfPQnEnter the number to generate the pramid:QnP-OscanfP7dP!Rnum-O
for#0O?#numO((-N
&6%$coordinate!$coordinate-6& goto%%!(1-O
&6for displaing digits towards the left and right of Iero6& fori#0$Oi?#Oi((-
printfP7,dP!absi--O %#%$,O
getch-O
,utut/
1.enter the number: 0) 1 0 1 " 1 0 1 " , " 1 0 1 " , ) , " 1 0 1 " , )
".enter the number: 0 , 1 0 1 " 1 0 1 " , " 1 0 1 " ,4oncluion/he program is error free5I5A 6789ATI,N9/
1- What is the use of dot operator in structures @Ans: he use of dot.- operator to access the members of a structure independentl.he dot operator connects a member with the structure variable."- efine unions @Ans: A union is a data tpe in c which allows the overla of more than one variable inthe same memor area.
80
8/12/2019 c and ds manuaL
91/259
,bectie 2%/
o read in two numbers % and n and then compute the sum of this geometricprogression 1(%(%"(%,(>>>.(%n
ecrition/n this program we have to read the two numbers and the calculate the sum of this
geometric progression in above mention .Algorithm/Step 1: StartStep ": read values of % and n! sum$1! i#1Step ,: chec5 for n R _
i- if n?#0 YY %?#0ii- print values are not validiii- read values of % and n
Step ): perform the loop operationi- fori#1Oi?#nOi((- then followsii- sum#sum(pow%!i-
Step 9: print sumStep *: Stop
81
8/12/2019 c and ds manuaL
92/259
Start
nitialiIe
Sum!!%!n
Bead %!n
fn?#0YY%?#0
=rint notvalid
#1 i?#n
((
Sum#sum(pow%!i-
=rint sum
Stop
false
truefalse
Sum # 1
true
lo3 chart/
8"
8/12/2019 c and ds manuaL
93/259
Program/
Linclude?stdio.hMLinclude?conio.hM
Linclude?math.hM
void main-N
int s`sum!i!%!nO
clrscr-OprintfPEnter the values for % and n:P-OscanfP7d 7dP!R%!Rn-O
ifn?#0 YY %?#0-
N printfPTalue is not validQnP-OelseN
printfPTalue is validQnP-O s`sum#1O fori#1Oi?#nOi((-
N s`sum#s`sum(pow%!i-O
printfPSum of series#7dQnP!s`sum-O
getch-O
,utut/
1.Enter the values for % and n:" , Talue is valid Sum of series#19".Enter the values for % and n:) 8 Talue is valid Sum of series#"1+)9,.Enter the values for % and n:0 1 Talue is not valid4oncluion/the program is error free
8,
8/12/2019 c and ds manuaL
94/259
5I5A 6789ATI,N9/
1- what are the difference between structures and unions @Ans: 2ere the maUor difference is with in the structure all elements must be allocatedmemor. Hut in union highest memor allocation must be allocated the all these
elements.
8)
8/12/2019 c and ds manuaL
95/259
,bectie 2&/
o convert the given binar number to "4s complementecrition/
n this program the given binar number is first covert the numbers 0 to1 and
1 to 0. And finall add the 1 to the converted number. hen we will get the"4s complement number.Algorithm/ main program
Step 1: StartStep ": declare the subprogram Zcomplementchar 6a-[Step ,: initialiIe the variable iStep ): read the binar numberStep 9: perform the loop operation. if it is true then follows. if not goto step ;
i- fori#0Oa]i^'#4Q04Oi((-ii- ifa]i '#404RRa]i^'#414- then displaed the number is not valid.
enter the correct number.
iii- E%it the loopStep *: call sub program 3complemta-4Step ;: stop9ub rogram/
Step 1: initialiIe the variable !c#0!b]1*0Step ": 1#strlena-Step ,: perform the loop operation. if it is true then follows. if not goto
i-fori#l$1OiM#0Oi$$-ii-ifa]i^##404- then b]i^#414 elseiii-b]i^#404
Step ): fori#l$1OiM#0Oi$$- is truei- ifi##l$1- thenii- ifb]i^##404- then b]i^#414 elseiii- b]i^#404!c#1 if not goto step 9
Step 9: ifc##1RRb]i^##404- is true theni- b]i^#414! c#0 if not goto Step *
Step *: ifc##1RRb]i^##414- then b]i^#404!c#1Step ;: displaed b]l^#4Q04Step +: print b and return to main program
89
8/12/2019 c and ds manuaL
96/259
lo3 chart/
Start
nitialiIe ! a] ^
Bead a
# 0 i((
A]i^'#4Q04
true
f a]i^'#404RRa]i^'#414
umber isnot valid
true
false
Call sub programComplementa-
false
Stop
8*
8/12/2019 c and ds manuaL
97/259
Complement-
nitialiIe
!!c#0!b] ^
1#strlena-
#i$1 $$
iM#0
true
fa]i^##404
H]i^#414
true
H]i^#^0^
false
#l$1 i$$
iM#0
true
f i##l$1
true
fb]i^##404
H]i^#414
true b]i^#404
C#1
false
f
c##1RRH]i^##404
H]i^#414 C # 0
A H
false
true
C
8;
8/12/2019 c and ds manuaL
98/259
Program/
Linclude ?stdio.hMLinclude?conio.hM
void complementchar 6a-Ovoid main-N
char a]1*^Oint iO
clrscr-OprintfPEnter the binar numberP-Ogetsa-Ofori#0Oa]i^'#\Q0\O i((-N
if a]i '#\0\ RR a]i^'#\1\-
N printfPhe number entered is not a binar number. Enter thecorrect numberP-O
e%it0-O
complementa-Ogetch-O
A
f c##1RRb]i^##1
H]i^#40^C#1
true
H
C
H]i^#4Q04
=rint 3b4
Beturn tomainprogram
8+
8/12/2019 c and ds manuaL
99/259
void complement char 6a-N int l! i! c#0O char b]1*^O
l#strlena-Ofor i#l$1O iM#0O i$$-N
if a]i^##\0\- b]i^#\1\O else b]i^#\0\O
fori#l$1O iM#0O i$$-N
ifi##l$1-
N if b]i^##\0\- b]i^#\1\O else
N b]i^#\0\O
c#1O
else
N ifc##1 RR b]i^##\0\- N b]i^#\1\O c#0O
else if c##1 RR b]i^##\1\-N
b]i^#\0\O c#1O
b]l^#\Q0\OprintfPhe "\s complement is 7sP! b-O
88
8/12/2019 c and ds manuaL
100/259
,utut/
1.Enter the binar number101010he "\s complement is 010110
Enter the binar number11111he "\s complement is 00001Enter the binar number""""he number entered is not a binar number. Enter the correct number4oncluion/the program is error free
5I5A 6789ATI,N9/
1- E%pand ASC @Ans: American standarad code for information interchange"-What is binar number @Ans: he number which contains onl 0 and 1 is called binar number.,- efine "[s complement @Ans: he given binar number is first covert the numbers 0 to1 and 1 to 0. And finalladd the 1 to the converted number. hen we will get the "4s complement number.
100
8/12/2019 c and ds manuaL
101/259
,bectie 2'/
o convert roman number to it4s decimal equivalentecrition/
n this program we have to ta5e the roman value. his value is converted into ait4s equivalent decimal number.
E%: _#10Algoritham/Step 1: StartStep ": read the roman numerical as stringStep ,: find length of roman numericalStep ): for each charcter in the string
i- ifchar#- then decimal#1ii- ifchar#T- then decimal#9iii- ifchar#_- then decimal#10iv- ifchar#G- then decimal#90v- ifchar#C- then decimal#100
vi- ifchar#- then decimal#900vii- ifchar#- then decimal#1000viii- otherwise invalid character
Step 9: repeat step ) until the length of the stringStep *: 5#char]length$1^Step ;: for each character of decimal string
i- ifdecimal]i^Mdec]i$1^- then 5#5$decimal]i$1^ii- else ifdecimal]i^#decimal]i$1 or decimal]i^?decimal]i$1- then
5#5(decimall]i$1^Step +: repate step ; until the length of decimal stringStep 8: print decimal valueStep 10: Stop
101
8/12/2019 c and ds manuaL
102/259
lo3 chart/
Start
Bead
roman value
Gen#strlenrom-
#0 i((
? len
from]i^##44
A]i^#1
rue
false
from]i^##4T4
A]i^#9
true
false
from]i^##4_4
A]i^#10
true
false
H
C
#a]len$1^
#len$1 i
M 0
true
A]i^Ma]i$
1^
true5#5$a]i$1^
F
A]i^##a]i$1^YYa]i^?a]i$1^
true
5#5(ai$1^
10"
A
8/12/2019 c and ds manuaL
103/259
H
from]i^##4G4
A]i^#90
true
false
from]i^##4C4
A]i^#100
true
false
from]i^##44
A]i^#900
true
from]i^##4C4
A]i^#1000
false
true
C
=rintinvalid
A
=rint 5
Stop
10,
8/12/2019 c and ds manuaL
104/259
Program/
Linclude?stdio.hMLinclude?conio.hM
Linclude?string.hMLinclude?stdlib.hM
void main-N
int 6a!len!i!U!5Ochar 6romO
clrscr-O
printfPEnter the Boman umeral:P-OscanfP7sP!rom-O
len#strlenrom-O
fori#0Oi?lenOi((- && loop will continue until is not graterthan length.N ifrom]i^##\\- a]i^#1O else ifrom]i^##\T\- a]i^#9O else ifrom]i^##\_\-
a]i^#10O else ifrom]i^##\G\- a]i^#90O
else ifrom]i^##\C\- a]i^#100O
else ifrom]i^##\\- a]i^#900O
else ifrom]i^##\\- a]i^#1000O else
N printfPQnnvalid TalueP-O getch-O e%it0-O 5#a]len$1^Ofori#len$1OiM0Oi$$- && loop will continue until lessthan Iero
10)
8/12/2019 c and ds manuaL
105/259
N ifa]i^Ma]i$1^- && chec5 the condition 5#5$a]i$1^O
else ifa]i ##a]i$1^ YY a]i^?a]i$1^- 5#5(a]i$1^O
printfPQnts ecimal Equivalent is:P-OprintfP7dP!5-Ogetch-O
,utut/
Enter the Boman umeral:
ts ecimal Equivalent is:900
Enter the Boman umeral:_
ts ecimal Equivalent is:10Enter the Boman umeral:",
nvalid Talue4oncluion/he program is error free
5I5A 6789ATI,N9/
1- What is difference between structure and unions @Ans : he amount of memor required to store a structure variable is the sum of siIe allthe members in addition to the padding btes that ma be provided b the compiler. ncase of a union the amount of memor required is the same as that required b its largestmember."- What are various operations performed on union @Ans: i-An union variable can be assigned to another union variable ii- A union variable can be passed to a function as a parameter iii- he address of the union variable can be e%tracted b using the address of
operator R-.
109
8/12/2019 c and ds manuaL
106/259
,bectie 2(/
o read the two comple% numbers and perform the addition and multiplicationof these two numbers.
ecrition/
n this program the comple% number means it contains the two parts . first one is
real part and second one is imaginar part"(,i-.b ta5ing these two comple%numbers we can perform the addition and multiplication operation.Algorithm/
Step 1: StartStep ": declare structure for comple% numbersStep ,: read the comple% numberStep ): read choiceStep 9: if choice#1 then addition operation will perform and it contains following
steps i- w.realpart # w1.realpart(w".realpartO
ii- w.imgpart # w1.imgpart(w".imgpartO goto step )
Step *: if choice#" then multiplication operation will perform and it containsfollowing steps i- w.realpart#w1.realpart6w".realpart-$w1.imgpart6w".imgpart-O
ii- w.imgpart#w1.realpart6w".imgpart-(w1.imgpart6w".realpart-O gotostep )
Step ;: if choice#0 then e%it operation will perform
Step +:if w.imgpartM0 then print realpart(imgpart else =rint realpart.
Step 8: Stop
10*
8/12/2019 c and ds manuaL
107/259
Start
eclare structure
Beadoption
foption#addor mul-
truefalse Bead real part of first numberimage part of first numberBead real part of second numberimage part of second number
foption#add
false
w.realpart#w1.realpart(w".realpartw.imgpart#w1.imgpart(w".imgpart f
option#mul
w.realpart#w1.realpart6w".realpart-w.imgpart#w1.imgpart6w".imgpart-f
w.imgpartM0
true
=rintw.realpart(w.img
part i
false
=rint realpart
Stop
lo3 chart/
10;
8/12/2019 c and ds manuaL
108/259
Program/
Linclude?stdio.hMLinclude?math.hMvoid arithmeticint opern-O
struct compNdouble realpartOdouble imgpartO
Ovoid main-Nint opernOclrscr-OprintfPQnQn QtQtQt66666 A EJ 66666P-OprintfPQnQn Select our option: Qn 1 : AQn " : JG=GXQn 0 : E_ QnQnQtQt Enter
our Kption ] ^QbQbP-OscanfP7dP!Ropern-OifopernM"-
N printfPinvalid optionP-O
else N switchopern- N
case 0: e%it0-O case 1: case ": arithmeticopern-O
default: main-O
getch-Ovoid arithmeticint opern-Nstruct comp w1! w"! wOprintfPQn Enter two Comple% umbers %(i-:Qn Beal =art of First umber:P-OscanfP7lfP!Rw1.realpart-OprintfPQn maginar =art of First umber:P-OscanfP7lfP!Rw1.imgpart-OprintfPQn Beal =art of Second umber:P-OscanfP7lfP!Rw".realpart-O
10+
8/12/2019 c and ds manuaL
109/259
printfPQn maginar =art of Second umber:P-OscanfP7lfP!Rw".imgpart-Oswitchopern-N &6addition of comple% number6&
case 1: w.realpart # w1.realpart(w".realpartO w.imgpart # w1.imgpart(w".imgpartO brea5O
&6multiplication of comple% number6& case ":
w.realpart#w1.realpart6w".realpart-$w1.imgpart6w".imgpart-O w.imgpart#w1.realpart6w".imgpart-(w1.imgpart6w".realpart-O brea5O if w.imgpartM0-
printfPQn Answer # 7lf(7lfiP!w.realpart!w.imgpart-OelseprintfPQn Answer # 7lf7lfiP!w.realpart!w.imgpart-O
getch-Omain-O
,utut/
66666 A EJ 66666
Select our option:1 : A" : JG=GX0 : E_ Enter our Kption ] 1^
Enter two Comple% umbers %(i-:Beal =art of First umber:"
maginar =art of First umber:"
Beal =art of Second umber:"
maginar =art of Second umber:"
Answer # ).000000().000000i
66666 A EJ 66666
108
8/12/2019 c and ds manuaL
110/259
Select our option:1 : A" : JG=GX0 : E_
Enter our Kption ] "^
Enter two Comple% umbers %(i-:Beal =art of First umber:"
maginar =art of First umber:"
Beal =art of Second umber:"
maginar =art of Second umber:"
Answer # 0.000000(+.000000i 66666 A EJ 66666
Select our option:1 : A" : JG=GX0 : E_
Enter our Kption ] ,^invalid option
66666 A EJ 66666
Select our option:1 : A" : JG=GX0 : E_
Enter our Kption ] 0^
4oncluion/he program is error free5I5A 6789ATI,N9/
1- efine structure @Ans: Structure is amethod for pac5ing data of different tpes. Structure help to organiIecomple% data in a more meaninigful wa.
"- What is use of ?math.hM header file @Ans: t is used to access the mathematical functions in programs.
110
8/12/2019 c and ds manuaL
111/259
,bectie 2)/
=rogram which copies one file to anotherecrition/
n this program we have to use the file functions to perform the copoperation from one file to another file.
Algorithm/Step 1: StartStep ": read command line argumentsStep ,: chec5 if no of arguments #, or not. f not print invalid no of argumentsStep ): open source file in read modeStep 9: if JGG pointer! then print source file can not be openStep *: open destination file in write modeStep ;: if JGG pointer! then print destination file can not be openStep + : read a character from source file and write to destination file until EKFStep 8: Close source file and destination fileStep 10: Stop
111
8/12/2019 c and ds manuaL
112/259
Start
Bead arguments
from command line
farg'
#,
=rint invalid noof arguments
truefalse
Fs#fopenarg]1^![r[
ffs#JG
G
true
=rint sourcefile can notbe opened
false
Ft#fopenarg]"^![w[
fft##JGG
=rint target filecan not be opened
Fclosefs-false
Ch#fgetcfs-
fch##EKF
Fclosefs-Fcloseft-
true
false
Fputcch!ft-
Stop
lo3 chart/
11"
8/12/2019 c and ds manuaL
113/259
Program/
Linclude ?stdio.hMLinclude ?conio.hM
Linclude ?process.hM
void mainint argc! char 6argv]^-NFGE 6fs!6ftOchar chOclrscr-Oifargc'#,- N putsPnvalid number of arguments.P-O e%it0-O
fs # fopenargv]1^!PrP-Oiffs##JGG- N
putsPSource file cannotbe opened.P-Oe%it0-O
ft # fopenargv]"^!PwP-Oif ft##JGG- && chec5 the condition if the file pointer is JGG or not
NputsParget file cannot be opened.P-Ofclosefs-Oe%it0-O
while1-N ch#fgetcfs-O if ch##EKF- && chec5 the condition if the file is end or not
brea5O else fputcch!ft-O fclosefs-O fcloseft-O getch-O
11,
8/12/2019 c and ds manuaL
114/259
,utut/
source.c this is source te%touput.c
Command line argumentssource.c ouput.csource.c this is source te%touput.c
this is source te%t
Command line argumentssource.cnvalid number of arguments.
4oncluion/the program is error free
5I5A 6789ATI,N9/
1- What is file @Ans: he collection of alphabets iscalled file
"- What are the various operations performed on the file @Ans: fopen-! fread-! fwrite-! fclose- etc..!
,- What is the use of file pointer @Ans: he file pointer must be used in subsequent operations on the file
11)
8/12/2019 c and ds manuaL
115/259
,bectie 2*/
o reverse the first n characters in afileecrition/
his program perform the reverse operation of n characters in the fileAlgorithm/
Step 1: StarStep ": read the command line argumentsStep ,: chec5 if arguments#, or not
f not print invalid no of argumentsStep ): open source file in read modeStep 9: if JGG pointer! then print file can not be openStep *: Store no of chars to reverse in 5
# 6argv]"^$)+Step ;: read the item from file stream using freadStep +: Store chars from last position to initial position in another stringtemp-Step 8: print the temp string
Step 10: Stop
119
8/12/2019 c and ds manuaL
116/259
Start
Bead command
line args
fargc'#,
false
Fp#fopenargv]1^![r[
=rint invalidno of args
ffp##JG
G
=rint file cannot be
opened
true
false
5#6argv]"^$)+ n # freada!1!5!fp- a]n^#\Q0\ len#strlena-
# len$1 i
M#0
true
S]U^#a]i^
=rint s]U^
V((
false
S]U(1^#4Q04
Stop
lo3 chart/
11*
8/12/2019 c and ds manuaL
117/259
Program/
Linclude ?stdio.hMLinclude ?conio.hMLinclude ?string.hM
Linclude ?process.hM
void mainint argc! char 6argv]^-N char a]19^O char s]"0^O char nO int 5O int U#0O int iO int lenO
FGE 6fpO
ifargc'#,- N putsPmproper number of arguments.P-O e%it0-O fp # fopenargv]1^!PrP-O iffp ## JGG- N
putsPFile cannot be opened.P-O e%it0-O
5#6argv]"^$)+O n # freada!1!5!fp-O a]n^#\Q0\O len#strlena-O fori#len$1OiM#0Oi$$- N s]U^#a]i^O printfP7cP!s]U^-O U#U(1Os]U(1^#\Q0\Ogetch-O
11;
8/12/2019 c and ds manuaL
118/259
,utut/
source.c this is sourceouput.c
Command line argumentssource.c ouput.csource.c this is source
ecruos si siht
Command line argumentssource.cnvalid number of arguments.
4oncluion/the program is error free
5I5A 6789ATI,N9/
1- Gist out the file handling functions @Ans: fopen-! fprintf-!fclose-!fscanf-!fgetc-!fputc-! etc..!
"- What is the use of fsee5- function @Ans: he function fsee5 sets the file pointer associated with a stream to a new position
,- What is use of the fflush- function @Ans: f the given stream has a buffered output! fflush writes the output of the stream tothe associate file.
11+
8/12/2019 c and ds manuaL
119/259
,bectie 2+/
=rogram that uses functions to perform the following operations on single lin5edlists. i- creation ii- insertion iii- deletion iv- traversalecrition/
n this program we have to create a single lin5ed list! insert the elements into that
list !delete the some elements from that list and then perform the sorting operationand traversal operation on that created lin5edlist Algorithm /
Step 1: Start
Step ": eclare a structure named lin5ed$list
Step ,: eclare the pointers ne%t! first! fresh! ptr
Step ): =rint main menu
Step 9: Bead choice
Step *: Switchchoice-
Step ;: fchoice##1-;.1 Assign fresh#mallocsiIe of node--;." Bead the element fresh$Mdata;., Bead the choice where to insert;.):Switchchoice-
;.).1: f choice##1;..).": Call the function Hegin-
;.).,: f choice##";.).): Call the function end-;.).9: f choice##,;.).*: Call the function middle-
Step +: fchoice##"-+.1: Bead the position to delete+.": Switchchoice-
+.".1: f choice##1+..".": Call the function Hegin-
+.".,: f choice##"+.".): Call the function end-+.".9: f choice##,+.".*: Call the function middle-
Step 8: f choice##,8.1 Call function view
118
8/12/2019 c and ds manuaL
120/259
Step 10: f choice##)10.1 E%it-
Step 11: Start insert function
Step 1": ffirst##null-
Step 1,: First$Mdata#e
Step 1): First$Mne%t#null
Step 19: Else declare new node
Step 1*:fresh$Mdata#e
Step 1;: f choice#1
Step 1+: frsh$Mne%t#first
Step 18: first#fresh
Step "0:if choice#"
Step "1: ptr#first
Step "": ptr$Mne%t#fresh
Step ",: fresh$Mne%t#full
Step "): f choice #,
Step "9: Enter the position
Step "*:at p$1 node
Step ";: fresh$Mne%t# ptr$Mne%t
Step "+: ptr$Mne%t#fresh
Step "8: for delete function
Step ,0: f first'#null
Step ,1: Enter the position to delete
Step ,": f choice#1
1"0
8/12/2019 c and ds manuaL
121/259
Step ,,: d#first$Mdata
Step ,): first#first$Mne%t
Step ,9: if choice#"
Step ,*: ptr#first
Step ,;: raverse to last node
Step ,+: d#ptr$Mne%t$Mdata
Step ,8: ptr $Mne%t#ptr$Mne%t$Mne%t
Step )0: =rint d value
Step )1: for function view
Step )": for ptr#first and ptr'#null and ptr#ptr$Mne%t
Step ),: =rint ptr$Mdata
Step )): End
1"1
8/12/2019 c and ds manuaL
122/259
lo3chart/
1""
8/12/2019 c and ds manuaL
123/259
Program/
L include?stdio.hML include?malloc.hM
int ch!i!n!U!p!itemO &6 TABAHGE ECGABAK 6&
&6 SAB KF SBJCJBE EFK 6&
struct lin5N
int dataOstruct lin5 6ne%tO
6start!6new!6l!6l1!6start1!6tO
&6 E KF SBJCJBE EFK 6&
&6 SAB KF A FJCK 6&
main-Nclrscr-Ostart#JGGOstart1#JGGOprintfP 6666 EJ 6666 P-OprintfPQn 1.nsertionQn ".eletionQn ,.raverseQn ).SearchQn 9.SortQn *.ergeQn;.BeverseQnP-Owhile1-N
printfPenter the choice:P-OscanfP7dP!Rch-Oswitchch-Ncase 1: insert-O
brea5Ocase ": delete-O
brea5Ocase ,: traverse-O
brea5Ocase ): search-O
brea5Ocase 9: sort-O
brea5Ocase *: merge-O
brea5Ocase ;: reverse-O
1",
8/12/2019 c and ds manuaL
124/259
brea5Ocase +:e%it-O
getch-O
&6 E KF A FJCK 6&
&6 SAB KF SEB FJCK 6&
insert-Nl#startOprintfPenter the item to be inserted:P-OscanfP7dP!Ritem-O
new#mallocsiIeofstruct lin5--Onew$Mdata#itemOifstart##JGG-N
new$Mne%t#JGGOstart#newO
elseN
printfP1.startQn".middleQn,.endQnP-OprintfPenter the place to place the item:P-O
scanfP7dP!Rch-Oifch##1-N
new$Mne%t#startOstart#newO
ifch##"-N
printfPenter the position to place item:P-OscanfP7dP!Rp-Ofori#1Oi?p$1Oi((-l#l$Mne%tOnew$Mne%t#l$Mne%tOl$Mne%t#newO
ifch##,-N
whilel$Mne%t'#JGG-l#l$Mne%tO
1")
8/12/2019 c and ds manuaL
125/259
new$Mne%t#JGGOl$Mne%t#newO
&6 E KF SEB FJCK 6&
&6 SAB KF S=GAX FJCK 6&
traverse-N
ifstart##JGG-printfPGS S E=XQnP-OelseN
forl#startOl$Mne%t'#JGGOl#l$Mne%t-
ifl##start-printfPQnstart:7d$MP!l$Mdata-OelseprintfPQn7;d$MP!l$Mdata-Oifl$Mne%t##JGG-printfPQn last:7d$MQnP!l$Mdata-O
&6 E KF S=GAX FJCK 6&
&6 SAB KF EGEE FJCK 6&
delete-N
l#startOifstart##JGG-printfPK ES 2E GSQnP-OelseN
printfP1.startQn".middleQn,.endQnP-OprintfPenter the place to delete the item:P-OscanfP7dP!Rch-Oifch##1-N
item#start$MdataOprintfPdeleted item is:7dQnP!item-Ostart#start$Mne%tO
ifch##"-
1"9
8/12/2019 c and ds manuaL
126/259
NprintfPenter the position to delete item:P-OscanfP7dP!Rp-Oifl$Mne%t##JGG-N
item#l$MdataOprintfPdeleted item is:7dQnP!item-Ol#start#JGGO
elseN
fori#1Oi?p$1Oi((-l#l$Mne%tOitem#l$Mne%t$MdataOprintfPdeleted item is:7dQnP!item-Ol$Mne%t#l$Mne%t$Mne%tO
ifch##,-N
ifl$Mne%t##JGG-Nitem#l$MdataOprintfPdeleted item is:7dQnP!item-Ol#start#JGGO
elseN
whilel$Mne%t$Mne%t'#JGG-l#l$Mne%tOitem#l$Mne%t$MdataOprintfPdeleted item is:7dQnP!item-Ol$Mne%t#JGGOl#l$Mne%tO
&6 E KF EGEE FJCK 6&
&6 SAB KF SEABC2 FJCK 6&
search-Nint f#0O
1"*
8/12/2019 c and ds manuaL
127/259
printfPenter the search item:P-OscanfP7dP!Ritem-Oifstart##JGG-printfPGS S E=XP-Oelse
N forl#start!i#1Ol'#JGGOl#l$Mne%t!i((-ifl$Mdata##item-N
f#1Obrea5O
iff##1-printfPitem 7d found at position :7dQnP!item!i-OelseprintfPitem 7d not foundQnP!item-O
&6 E KF SEABC2 FJCK 6&
&6 SAB KF SKB FJCK 6&
sort-Nint tOifstart##JGG-printfPGS S E=XP-OelseN
forl1#startOl1$Mne%t'#JGGOl1#l1$Mne%t-N
forl#startOl$Mne%t'#JGGOl#l$Mne%t-ifl$Mdata M l$Mne%t$Mdata-Nt#l$MdataOl$Mdata#l$Mne%t$MdataOl$Mne%t$Mdata#tO
printfP2E SKBE KBEB S:P-Oforl#startOl'#JGGOl#l$Mne%t-printfP7,dP!l$Mdata-OprintfPQnP-O
1";
8/12/2019 c and ds manuaL
128/259
&6 E KF SKB FJCK 6&
&6 SAB KF EB/E FJCK 6&
merge-NprintfPenter no of elements to be inserted in second list :P-OscanfP7dP!Rn-OforU#1OU?#nOU((-N
l1#start1OprintfPenter the item to be inserted:P-OscanfP7dP!Ritem-Onew#mallocsiIeofstruct lin5--Onew$Mdata#itemO
new$Mne%t#JGGOifstart1##JGG-start1#newOelseN
printfP1.startQn".middleQn,.endQnP-OprintfPenter the place to place the item:P-OscanfP7dP!Rch-Oifch##1-
Nnew$Mne%t#start1Ostart1#newO
ifch##"-
NprintfPenter the position to place item:P-OscanfP7dP!Rp-Ofori#1Oi?p$1Oi((-l1#l1$Mne%tOnew$Mne%t#l1$Mne%tOl1$Mne%t#newO
ifch##,-
Nwhilel1$Mne%t'#JGG-l1#l1$Mne%tOl1$Mne%t#newO
1"+
8/12/2019 c and ds manuaL
129/259
ifstart##JGG-start#start1OelseN
l#startO
whilel$Mne%t'#JGG-l#l$Mne%tOforl1#start1Ol1$Mne%t'#JGGOl1#l1$Mne%t-N
l$Mne%t#l1Ol#l$Mne%tO
printfP 666 GS S EB/E 666 QnP-O
&6 E KF EB/E FJCK 6&
&6 SAB KF BETEBSE FJCK 6&
reverse-Nifstart##JGG-printfPGS S E=XQnP-OelseN
l#startOl1#t#JGGOwhilel'#JGG-N
l1#tOt#lOl#l$Mne%tOt$Mne%t#l1O
start#tOprintfP 666 GS S BETEBSE 666QnP-O
&6 E KF BETEBSE FJCK 6&
66666 KJ=J 666666666 EJ 66661.nsertion
1"8
8/12/2019 c and ds manuaL
130/259
".eletion,.raverse).Search9.Sort*.erge
;.Beverseenter the choice:1enter the item to be inserted:1enter the choice:1enter the item to be inserted:"1.start".middle,.endenter the place to place the item:1enter the choice:1enter the item to be inserted:,
1.start".middle,.endenter the place to place the item:,enter the choice:1enter the item to be inserted:)1.start".middle,.endenter the place to place the item:"enter the position to place item:,enter the choice:,
start:"$M 1$M )$Mlast:,$Menter the choice:)enter the search item:)item ) found at position :,enter the choice:*enter no of elements to be inserted in second list :,enter the item to be inserted:9enter the item to be inserted:*1.start".middle,.endenter the place to place the item:1enter the item to be inserted:;1.start
1,0
8/12/2019 c and ds manuaL
131/259
".middle,.endenter the place to place the item:"enter the position to place item:"666 GS S EB/E 666
enter the choice:,
start:"$M 1$M )$M ,$M *$M ;$Mlast:9$Menter the choi