Upload
prakash-sharma
View
230
Download
0
Embed Size (px)
Citation preview
8/17/2019 Ccp New Manual
1/39
Program 1
Design and develop a flowchart or an algorithm that takes tree coefficients (a, b and c) of Quadratic equation
(ax!bx!c"#) as input and compute all possible roots$ %mplement a & program for the developed
flowchart'algorithm and execute the same to the possible roots for a given set of coefficients with appropriate
messages$
lgorithm To compute all possible roots of the quadratic equationStep 1: StartStep 2: [Input coefficients of quadratic equation]
read a, b, cStep 3: [Check for the alid coefficients]
if a!" and b!" then print #Inalid coefficients$
else a!" thenroot1 ! %c&b
print #'inear equation$
print rootStep (: [Compute discriminate alue]disc ! b)b%(*a*c
Step +: [ased on discriminate alue, classif- and calculate all possible roots and print them]if disc!" then
root1 ! root2 ! %b&.2*a/ print #The roots are real and equal$ print root1,root2
else if disc0" thenroot1 ! .%b)⎷ disc/&.2*a/root2 ! .%b %⎷ disc/&.2*a/
print #The roots are real and distinct$
print root1,root2else
real ! %b&.2*a/ima ! ⎷ disc & .2*a/
print #The roots are real and comple$ print root1,root2
end if end if
Start : Stop
8/17/2019 Ccp New Manual
2/39
T
4
T
4
T 4 4
T
*tart
read a, b, c
*top
print root1 print root1,root
is disc+# is disc"#
is
a"#
is a"# and b"#
root1"(-b!⎷disc)'(.a)
root" -b -⎷disc ' .a
real " -b'(.a)
imag "⎷disc ' (.a)
root1"root" -b'(.a)root1" -c'b
print /0oots are real and
complex
print /0oots are real and
distinct
print /0oots are real and
equal
print /2inear equation
disc " b!b-3.a.c
print /%nvalid coefficients
8/17/2019 Ccp New Manual
3/39
5include6stdio7h05include6math7h0oid main./8
float a,b,c,disc,root1,root2,real,im9 printf.enter a,b,c alues;n/9
scanf.
8/17/2019 Ccp New Manual
4/39
Program
Design and develop an algorithm to find the reverse of an integer number 456 and check whether it is
P2%4D0768 or 479$ %mplement a & program for the developed algorithm that takes an integer number
as input and output the reverse of the same with suitable messages$
lgorithm To check @hether the ien number is palindrome or notStep 1: StartStep 2: [Input an inteer number]
read numStep 3: [Set the alue of num into the ariable n]
n!numStep (: [?eerse the ien number]
@hile nA!" thenrem ! num
8/17/2019 Ccp New Manual
5/39
5include6stdio7h0oid main./8
int n,a,rem,re!"9
printf.Fnter the alue;n/9scanf. printf.?eerse number!
8/17/2019 Ccp New Manual
6/39
Program <
< a) Design and develop a flowchart to find the square root of a given number 4$ implement a & program for
the same and execute for all possible inputs with appropriate messages$ 4ote don=t use librar> function sqrt
(n)$
lgorithm To find the square root of the ien number7
Step 1: StartStep 2: [Input the number to find the square root]
read nStep 3: [Check if the ien number is positie or neatie number]
if n0!" thenoto to step (
else
print #Do square root for neatie number$7end if
Step (: [Compute the square root of the ien number][Calculate decimal part of the square root]for s!1 to n
decrement send for[Calculate the fractional part]for d!"7""1 to 17"
!s)dif *0n then
!%"7""1oto to Step +end if
end for Step +: [Gispla- the calculated square root result]
print Step : [ Gispla- the square root of the built in function sqrt./]
print sqrt.n/Step E: Stop
8/17/2019 Ccp New Manual
7/39
*tart
read n
is
n+"#
T
T s"1
s.s?"n
s!!
s - -
4 d"$##1
?
d"d!$##
Tx"s!d
4is
x.x+n
T
x"x-$##1
print /4o square root for
ne ative number
print sqrt(n)
stop
8/17/2019 Ccp New Manual
8/39
5include6stdio7h05include6math7h0
oid main./8int s9float n,d,9
printf.Fnter the alue;n/9scanf.
8/17/2019 Ccp New Manual
9/39
< b) Design and develop a & program to read a >ear as an input and find whether it is leap >ear or not $ lso
consider end of the centuries$
lgorithm To find the ien -ear is leap -ear or notStep 1: StartStep 2: [Input the alid -ear] read -ear Step 3:[Check for leap -ear] if -ear
8/17/2019 Ccp New Manual
10/39
scanf.
value of x and its coefficients using Aorner=s method$ %mplement a & program for the developed algorithm
and execute for different sets of values of coefficients and x$
lgorithm To ealuate the pol-nomial epressionStep 1:StartStep 2:[Input the number of elements] read nStep 3:[ Input the alues into the arra-]
for i!n to "
read a[i] end forStep (:[Input the alue of ] read Step +:[IntialiJe sum to Jero] sum!"Step :[Calculate sum ] for i!n to 1 sum!.sum)a[i]/* end forStep E:[Kdd last term to the sum] Sum!sum)a["]
Step L:[Butput the result] print sum
Step M:Stop
5include6stdio7h0oid main./8
int a[1"],i,n,sum,9 printf.Fnter the number of co%efficients;n/9
scanf.
8/17/2019 Ccp New Manual
11/39
8sum!.sum)a[i]/*9
>sum!sum)a["]9
printf.sum!
@
9
@
9
Start
read n
i%% i ! n
!
read a[i]
read
sum!"
i%% i ! n
!
sum!.sum)a[i]/*
sum!sum)a["]
print sum
8/17/2019 Ccp New Manual
12/39
Program B
Draw the flowchart and write & program to compute sin(x) using 9a>lor series approximation given b> sin(x)
" x
1 !−
x3
3 !+ x
5
5 !−
x7
7 !+…. &ompare the result with the built in librar> functions and print the both the
results with appropriate messages$
lgorithm To compute sin./ usin Ta-lor seriesStep 1:Start
Step 2:[Input the deree] read dereeStep 3:[Gefine I] I!371(2Step (:[Compute the alue of ] !deree*.I&1L"/Step +:[Kssin the alues for num,den and i]
num! den!1 i!2Step :[Calculate the sum of all terms] do
term!num&den num!num** den!den*i*.i)1/ sum!sum)term i!i)2 @hile.fabs.term/0!"7""""1// end do @hileStep E:[rint calculated and predefined sine alues] print sumStep L:Stop
Stop
8/17/2019 Ccp New Manual
13/39
T
4
Start
?ead deree
I!371(2
!deree*.I&1L")
num!
deno!1
!
term!num&den
num!num**
den!den*i*.i)1/
sum!sum)term
i!i)2
fabs.term/0!"7""""1
rint calculated sine alue=predefined sine alue
Stop
8/17/2019 Ccp New Manual
14/39
5include6stdio7h05include6math7h05define I 371(2oid main./8
int deree,i9float ,nume,deno,term,sum!"9
printf.Fnter the alue of deree;n/9scanf.
8/17/2019 Ccp New Manual
15/39
Program C
Develop an algorithm, implement and execute a & program that reads 4 integer numbers and arrange them
in ascending order using ubble sort$
lgorithm To sort D inteer numbers usin ubble sortStep 1: StartStep 2: [Input the number of elements]
read nStep 3: [Input unsorted elements in arra-]
read a[ ]Step (: [Butput the oriinal elements]
print a[ ]Step +: [Sort the elements in ascendin order]
for each alue i in arra- a[i] to nfor each alue N in arra- a[N] to n%i
if a[N] 0 a[N)1] thentemp ! a[N]a[N] ! a[N)1]a[N)1] ! temp
end if end for end for
Step : [Butput the sorted elements] print a[ ]
Step E: Stop
8/17/2019 Ccp New Manual
16/39
T
4
T
4
T
4
T
4
T
*tart
read n
4
read aEiF
i!! i"#
i?n
*topprint aEiF
i!! i"#
i?n
temp " aEGF
aEGF " aEG!1F
a !1 " tem
is aEGF+aEG!1F
G!! i"#
i?n
i!! i"1
i?n
print aEiF
i!! i"#
i?n
8/17/2019 Ccp New Manual
17/39
T
5include6stdio7h0oid main./8
int a[1"],n,i,N,temp9 printf.Fnter the number of elements;n/9scanf.
8/17/2019 Ccp New Manual
18/39
Program H
Develop, implement and execute a & program that reads matrices (m.n) and (p.q) and compute the
product and $ 0ead matrix in maGor order and matrix in column maGor order$ Print both the input
matrices and resultant matrix with suitable headings and in matrix format$ Program must check the
compatibilit> of orders of the matrices for multiplication$ 0eport appropriate message in case of
incompatibilit>$
lgorithm To compute product of t@o matricesSTF 1: StartSTF 2: [input order of matri K]
read m,nSTF 3: [input order of matri ]
read p,qSTF (: [check for compatibilit-]
if.nA!p/ rint #multiplication not possible$ return end if
STF +: [input elements of matri K] for .i!"9i6m9i))/ for.N!"9N6n9N))/ read a[i][N] end for end for
STF : [input elements of matri ] for.i!"9i6p9i))/ for.N!"9N6q9N))/ read b[i][N] end forend for
STF E: [find product of 2 matrices] for.i!"9i6m9i))/ for.N!"9N6q9N))/ c[i][N]!"9 for.k!"9k6n9k))/ c[i][N]!c[i][N])a[i][k]*b[k][N] end for end for end for
STF L: [print matri K]
8/17/2019 Ccp New Manual
19/39
for.i!"9i6m9i))/ for.N!"9N6n9N))/ print a[i][N] end forend for
STF M: [print matri ] for .i!"9i6p9i))/ for.N!"9N6q9N))/
print b[i][N] end for end for
STF 1": [print resultant matri C] for .i!"9i6m9i))/ for.N!"9N6q9N))/ print c[i][N] end for end for
STF 11: Stop
4
T
4
T
4
T
Start
?ead m,n
?ead p,q
.nA!p/
A rint #multiplication not possible$
i++ i=0
i
8/17/2019 Ccp New Manual
20/39
T
4
T
i++ i=0
i
8/17/2019 Ccp New Manual
21/39
4
T
4
T
4
T
4
T
4
T
T
4
T
B
i++ i=0
i
8/17/2019 Ccp New Manual
22/39
4
T
4
T
5include6stdio7h0oid main./8
int a[1"][1"],b[1"][1"],c[1"][1"],i,N,k,m,n,p,q9 printf.Fnter the order of matri K;n/9
scanf.
C
i++ i=0
i
8/17/2019 Ccp New Manual
23/39
for.i!"9i6m9i))/8
for.N!"9N6q9N))/8
c[i][N]!"9for.k!"9k6n9k))/8
c[i][N]!c[i][N])a[i][k]*b[k][N]9
>>
> printf.The elements of matri K;n/9for.i!"9i6m9i))/8
for.N!"9N6n9N))/8
printf.
printf.;n/9>
printf.The elements of matri ;n/9for.i!"9i6p9i))/8
for.N!"9N6q9N))/8
printf.
printf.;n/9>
printf.The resultant matri C;n/9for.i!"9i6m9i))/
8 for.N!"9N6q9N))/8
printf.
printf.;n/9>
>else
printf.Oatri multiplication not possible;n/9>
8/17/2019 Ccp New Manual
24/39
Program I
Develop, implement and execute a & program to search a name in a list of names using inar> searching
technique$
lgorithm To search a name usin inar- search
Step 1: Start
Step 2: [Input the number of elements] read nStep 3: [ Input the arra- items]
for i!" to n read name[i] end forStep (: [Input the ke-] ?ead ke-Step +: [IntialiJe lo@ to Jero and hih to last element] lo@!" hih!n%1Step : [Calculate mid element and compare @ith ke- ] @hile. lo@6!hih/ mid!lo@)hih&2 if name[mid] ! ke- then print$search successful,print in mid)1 location$ oto step L else if name[mid]0ke- then hih!mid)1 else
lo@!mid)1 end if
end @hile
Step E:[rint a messae if not successful] rint $search unsuccessful name not present in the list$Step M:Stop
8/17/2019 Ccp New Manual
25/39
4
T
4
T
Start
i)) i !"
?ead n
@
9
?ead name[i]
?ead ke-
'o@!"
Pih!n%1
lo@6!hih rint #unsuccessful search$
Oid!.lo@)hih/&2
rint #search successful$If.name[mid]!!ke-/
If.name[mid]0ke-/ @Pih!mid%1
9
lo@!mid)1
8/17/2019 Ccp New Manual
26/39
5include6stdio7h05include6strin7h0oid main./8
char name[1"][2"],ke-[2"]9 int n,i,lo@,hih,mid,found!"9 printf.#enter the number of names to read,n!$/9 scanf.#if.found!!1/
printf.#name found in position:
stop
8/17/2019 Ccp New Manual
27/39
Program J
J a) Krite and execute a c program that implements string cop> operation *90&7PL(str1,str) that copies a
string str1 to another string str without using librar> function$
lgorithm: To cop- contents of one strin to another @ithout librar- function7Step 1: StartStep 2: [Input source strin]
?ead str1Step 3: [call the function strcop- b- passin str1 and str2]
Strcop-.str1,str2/Step (: [print destination strin] rint str2Step +: Stop
lgorithm: 4unction to implement strcop- @ithout librar- function7Step 1: [?eceie strin from main function]Step 2: [To cop- the strin]
@hile .s1[i]A!R;"R/ s2[i] ! s1[i]9 i))9end @hile
Step 3: [Kppend the null strin to the destination strin] s2[i] ! Q;"R
Step (: [end of function]
@
9
*tart
0ead str1
*trcop>(s1,s)
s1EiF:"=M#=
*top *EiF " NM#=
sEiF " s1EiF
i!!Print str
*trcop>(str1,str)
8/17/2019 Ccp New Manual
28/39
5include6stdio7h0oid strcop-.char s1[+"],char s2[+"]/
8 int i!"9 @hile .s1[i]A!R;"R/
8 s2[i]!s1[i]9
i))9>
s2[i]!R;"R9>oid main. /8 char str1[+"],str2[+"]9 printf .#enter the source strin;n$/9 ets .str1/9
strcop- .str1,str2/9 printf .#destination strin is;n$/9
puts .str2/9>
J b) Krite and execute a c program that reads a sentence and prints frequenc> of each of the vowels and total
count of consonants$
lgorithm: To count number of consonants and o@elsStep 1: StartStep 2: [input the sentence]
read sStep 3: [check @hether the letter of the sentence are alphabets]
for i!" to strlen[s] if isalpha.s[i]/ then
[email protected][i]/ if ch!RaR or ch!ReR or ch!RiR or ch!RoR or ch!RuR then increment c
else increment cc
end if end if
end for Step +: [print no of o@els]
rint cStep : [print no of consonants]
rint ccStep E: Stop
8/17/2019 Ccp New Manual
29/39
@
T
4
T
T 4
*tart
0ead s
i!! i"#
i?strlen(s)
*top
Print vc
%s ch""=a= or ch""=e= or ch""=%= or ch""=o=
or ch""=u=
&h"tolower(sEiF)
is(isalphasEiF)
Print cc
%ncrement vc!!%ncrement cc!!
8/17/2019 Ccp New Manual
30/39
5include6stdio7h05include6strin7h0 oid main. /8
char s[1""],ch9 int i, c, cc!"9
printf.#enter the sentence;n$/9 ets.s/9 for.i!"9i6strlen.s/9i))/ 8
if.isalpha.s[i]//8
[email protected][i]/9 if.ch!!RaRch!!ReRch!!RoRch!!RuR/ c))9
else cc))9
> >
>
8/17/2019 Ccp New Manual
31/39
Program 1#
1# a) Design and develop a & function rightOshift(x,n) that takes two integers x and n as input and return
value of the integer x rotated to the right b> n positions$ ssume the integers are unsigned$ Krite a &
program that invokes this function with different values for x and n and tabulate the result with suitable
headings$
lgorithm To riht shift a ien number b- n position7*tep 1 Start7*tep [Input and n]
read ,n*tep < [Call function riht shift b- passin and n]
result ! rihtshift.,n/7*tep 3 [Gispla- the result]
print result*tep B Stop7
lgorithm 4unction to shift the number to riht7*tep 1 [receie the alue of and n]*tep [erform shift operation]
for i ! " to nif
8/17/2019 Ccp New Manual
32/39
5include6stdio7h05include6math7h0unsined int rihtshift.unsined int,unsined int/9oid main./8
unsined int ,n,result9 printf.Fnter the alue and the number of bits to be rotated;n/9scanf.return 9
>
1# b) Design and develop a & function isprime(num) that accepts an integer argument and returns 1 if the
argument isprime, a # otherwise$ Krite a & program that invokes this function to generate prime numbers
between the given ranges$
lgorithm To check @hether a ien number is prime or not7*tep 1 Start7*tep [Input the rane]7
read and -7*tep < [Kssin " to fla] fla !"*tep 3 [rint the prime numbers]
for i ! to -if isprime.i/ then
print ifla !1
end if
return
8/17/2019 Ccp New Manual
33/39
end for *tep B [rint the messae if there is no number in the ien rane]
if fla !" then print #no prime numbers @ithin the rane$7
*tep C Stop7
lgorithm 4unction to find the number is prime are not7*tep 1 [?eceie the alue of n]*tep [Check @hether the number is equal to " or 1]
if n!" or n!1return "
*tep < [Check @hether ien number is prime or not]for i ! 2 to n
if n < i!" thenreturn "
end if end for return 17
*tep 3 Fnd of function7
5include6stdio7h05include6math7h0int isprime.int/9oid main./8
int ,-,i,fla!"9
printf.enter the rane;n/9scanf.
8/17/2019 Ccp New Manual
34/39
return 19>
T
T
T
isprime(n)
i++ i =
i
8/17/2019 Ccp New Manual
35/39
Program 11
Draw the flowchart and write a recursive & function to find the factorial of number, n:, defined b>
fact(n)"1,if n"#$otherwise fact(n)"n.fact(n-1)$ 5sing the function, wirte a & program to compute binomial
coefficient nC
r❑ $9abulate the result for different values of n and r with suitable message$
lgorithm To compute the binomial co%efficient nCr *tep 1 Start7
*tep [Input the alues for n and r] read n,r *tep < [Call function to find factorial]
res!fact.n/&.factc.n%r/*fact.r//*tep 3 [Gispla- the result]
print res*tep B Stop7
lgorithm ?ecursie function to find factorial of the number *tep 1 [receie the alue of n]*tep [Check the base condition]
if n!" then
return 1*tep < ECall function recursiel-]
return n*fact.n%1/*tep 3 Fnd of function7
fact.n/Start
Isn!" U
read n,r
%res! *
return "
print res
return n*fact.n%1/Start
8/17/2019 Ccp New Manual
36/39
5include6stdio7h0int fact.n/8
if.n!!"/return 19
elsereturn .n*fact.n%1//9
>oid main./
8int n,res,r9
printf.enter the alue of n and r;n/9scanf.
8/17/2019 Ccp New Manual
37/39
Program 1
iven two universit> information files /studentname$txt and /studentusn$txt that contents students 4ames
and 5*4 respectivel>$ Krite a & program to create a new file called /output$txt and cop> the content of files
/studentname$txt and /studentusn$txt into output file in the sequence show below$ Displa> the contents of
output file /output$txt on the screen$
5include6stdio7h0
oid main. /84I'F *fp1,*fp2,*fp39char name[2"],VSD[2"]9fp1!fopen.sname7tt,r/9if.fp1! !DV''/
printf.file doesnot eist;n/9fp2!fopen.susn7tt,r/9if.fp2! !DV''/
printf.file doesnot eist;n/9fp3!fopen.output7tt,@/[email protected]/==Afeof.fp2//
8fscanf.fp1,fclose.fp3/9
>
8/17/2019 Ccp New Manual
38/39
Program 1<
Krite a & program to maintain a record of /n student details using an arra> of structures with four fields
(0oll number, 4ame, 6arks and rade)$ 8ach field is of an appropriate datat>pe$ Print the marks of all
student given student name as input$
5include6stdio7h05include6strin7h0struct student8
int rollno,marks9char name[2"],rade9
>9oid main./8
struct student s[2"]9int n,i,found!"9char sname[2"]9
printf.enter the number of students;n/9scanf.
8/17/2019 Ccp New Manual
39/39
if.found!!"/ printf.name not found;n/9
>
Program 13
Krite a & program using pointers to compute sum, mean and standard deviation of all elements stored in an
arra> of n real numbers$
5include6stdio7h05include6math7h0
oid main./8
float a[1"],*ptr, mean, std,sum!",sumstd!"9int i,n9
printf.enter the no of elements;n/9scanf.mean!sum&n9
ptr!a9for.i!"9i6n9i))/8
sumstd!sumstd)po@..*ptr%mean/,2/9 ptr))9
>std!sqrt.sumstd&n/9
printf.sum!