Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
1
GOKARAJU RANGARAJU INSTITUTE
OF ENGINEERING AND
TECHNOLOGY (Autonomous)
B. Tech I Year (CSE,ECE,EEE,AIML,IT,DS,CE,MEBranches)
PROGRAMMING FOR PROBLEM SOLVING LAB
Preface
The main objective of the course entitled “Programming for Problem Solving lab” is to make first year
B.Techstudents familiar with the Computer Programming in a more systematic manner. This material is
written according to GRIET GR-20(Autonomous) syllabus. This book has been prepared to meet the
requirements of Programming for Problem Solving Lab.
This work is contributed and verified by the faculty of CPDS/CSE
1.Dr. B. Srinivasa Rao
2. S T G Y .Sandhya
3. K. Ch. Suneetha
5. A.Sravanthi
6. D. SugunaKumari
7. P. Rajesh
8. A.Sowmya
9. R.S.Shalini
10. M.Suresh Babu
PROGRAMMINGFORPROBLEM SOLVING
LABORATORYSYLLABUS
Prerequisite:Basicoperations of computer andknowledgeofmathematics COURSE OBJECTIVES:
Laboratory Objectives: The students will learn the following:
• To work with an IDE to create, edit, compile, run and debug programs
• To analyze the various steps in program development.
• To develop programs to solve basic problems by understanding basic concepts in C like operators,
control statements etc.
• To develop modular, reusable and readable C Programs using the concepts like functions, arrays etc.
• To write programs to create, read from and write to text and binary files.
COURSE OUTCOMES:
Laboratory Outcomes The student is expected to be able to:
• Formulate the algorithms for simple problems and translate algorithms to a working and correct program.
• Identify, analyse, correct syntax errors as reported by the compilers
• Identify and correct logical errors encountered during execution
• Represent and manipulate data with arrays, strings and structures and use pointers and recursion.
• Create, read and write to and from simple text and binary files and modularize the code with functions so
• that they can be reused
SYLLABUS:
Task-1:
a) Write a C program to implement operators inc?
b) Write a C program to find greatest and smallest among three numbers using conditional operator.
c) Write a C program to implicit and explicit type conversion inc?
Task-2:
a) Write a C program to swap two numbers using the following.
(i) Using thirdvariable
(ii) Without using thirdvariable
(iii) Using bitwiseoperators
b) Write a c program to add two numbers without using arithmetic operators inc?
Task-3:
a) Write a C program to find the roots of a quadratic equation usingif-else
b) The program should request the user to input two numbers and display one of the following as per
the desire of user. (a). Sum of numbers (b) difference of numbers (c) product of the numbers
(d)division of the numbers. Write a C program using switch statement to accomplish the abovetask.
Task-4:
a) Write a c Program check whether a given number is perfect number ornot.
b) Write a c Program check whether a given number is palindrome number or not.
c) Write a c Program check whether a given number is armstrong number ornot.
.
Task-5:
a) Write a C program to display the followingpatterns.
(i) 1 (ii) 1
2 3 2 3
4 5 6 4 5 6
7 8 9 10 7 8 9 10
b) write a C program to generate the prime numbers between x and y where x and y are starting and
ending values to be suppliedbythe user.
c) Write a C program to calculate the following Sum:
Sum=1+x/1!-x2/2!+x3/3!-x4/4!+ .................... xn/n!
Task-6 :
a) Write a C program to find sum, average and minimum and maximum in a list ofnumbers.
b) Write a C program to implement linearsearch.
c) Write a C program to implement binarysearch.
Task-7:
a) Write a C program to implement matrixaddition
b) Write a C program to implement matrixmultiplication.
Task-8:
a) Write a C program to implement the following string handlingfunctions.
(i) strlen() (ii) strcpy()(iii)strcmp()(iv) strcat()
b) Write a C program to read first name , middle name and last name of a student and display a string full
name without using string handlingfunctions.
Task-9:
a) Write a C program to read a string and find it is palindrome ornot.
b) Write a C program to sort the names of n students in the alphabeticalorder.
Taks-10:
a )Write a c program to implement the following using recursive and non-recursive functions to find the
factorial of a given integer.
b)Write a c program to implement the following using recursive and non-recursive functions to find the
GCD (greatest common divisor) of two given integers
Task-11:
a )Write a c program to implement transpose of a matrix using functions.
b)Write a c program to display binary equivalent of a given decimal number.
Task-12:
a) Create a structure student with name ,rollno,marks of 3 subjects as members . Write a c program to
sort student details based ontotalusing structures and functions .
b) Write a C program that uses structures and functions to perform the followingoperations:
i. Addition of two complex numbers
ii. subtraction of two complex numbers
iii. Multiplication of two complex numbers
Task-13:
`a) Write a c program using functions and pointers that compares two strings to see whether they are identical.
The function returns 1 if they are identical, 0 otherwise.
b) Write a C program to sort list of numbers using pointers.
Task-14:
a) Write a c program to implement following pre-processor directives. (i)define
(ii) ifdef (iii)undef (iv)ifndef.
b) Write a c program to create a user defined header file to find sum, product and greatest of two numbers?
Task-15:
a) Write a C program to merge two files into a thirdfile.
b) Write a C program to find some of n numbers using command linearguments.
TEXT BOOKS:
1. Byron Gottfried, Schaum’s Outline of Programming with C,McGraw-Hill
2. B.A. Forouzan and R.F. Gilberg C Programming and Data Structures, Cengage Learning, (3rd
Edition)
REFERENCE BOOKS:
1. Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice Hall of India
2. R.G. Dromey, How to solve it by Computer, Pearson (16th
Impression)
3. Programming in C, Stephen G. Kochan, Fourth Edition, Pearson Education.
4. HerbertSchildt, C: The Complete Reference, McGraw Hill, 4th
Edition
Index
S.No TaskNo. Page
No
Dates Signature
1 1a) Write a C program to implement operators inc?
2 1b) Write a C program to find greatest and smallest among three
numbers using conditional operator.
.
3
1c) Write a C program to implicit and explicit type conversion inc?
4 2a) Write a C program to swap two numbers using the following.
(i) Using thirdvariable
(ii) Without using thirdvariable (iii) Using bitwiseoperators
5
2b )Write a c program to add two numbers without using
arithmetic operators inc?
6
3a) Write a C program to find the roots of a quadratic equation
usingif-else.
7 3b) The program should request the user to input two numbers
and display one of the following as per the desire of user. (a). Sum of
numbers (b) difference of numbers (c) product of the numbers
(d)division of the numbers. Write a C program using switch
statement to accomplish the abovetask.
8 4a) Write a c Program check whether a given number is perfect
number ornot.
9 4b) Write a c Program check whether a given number is
palindrome number or not.
10 4c) Write a c Program check whether a given number is armstrong
number ornot.
11 5a) Write a C program to display the followingpatterns.
(i) 1 (ii) 1
2 3 2 3
4 5 6 4 5 6
7 8 9 10 7 8 9 10
12 5b) write a C program to generate the prime numbers between x
and y where x and y are starting and ending values to be
suppliedbythe user.
13 5c) Write a C program to calculate the following Sum:
Sum=1+x/1!-x2/2!+x3/3!-x4/4!+....+xn/n!
14 6a) Write a C program to find sum, average and minimum and
maximum in a list ofnumbers.
15 6b) Write a C program to implement linearsearch.
16 6c) Write a C program to implement binarysearch.
17 7a) Write a C program to implement matrixaddition
.
18 7b) Write a C program to implement matrixmultiplication.
19 c) Write a C program to implement the following string
handlingfunctions.
(i) strlen() (ii) strcpy()(iii)strcmp()(iv) strcat()
.
20 8b) Write a C program to read first name , middle name and last
name of a student and display a string full name without using string
handlingfunctions.
.
21 9a) Write a C program to read a string and find it is palindrome
ornot.
22 9b) Write a C program to sort the names of n students in the
alphabeticalorder.
.
23 10a) Write a c program to implement the following using
recursive and non-recursive functions to find the factorial of a given
integer.
.
24 10b) Write a c program to implement the following using
recursive and non-recursive functions to find the GCD (greatest
common divisor) of two given integers
.
25 11a) Write a c program to implement transpose of a matrix using
functions.
26 11b) Write a c program to display binary equivalent of a given
decimal number.
27 c) 12a) Create a structure student with name ,rollno,marks of
3 subjects as members . Write a c program to sort student details
based ontotal using structures and functions .
28 12b) Write a C program that uses structures and functions to
perform the followingoperations:
i. Addition of two complex numbers
ii. subtraction of two complex numbers
iii. Multiplication of two complex numbers
29 13a) Write a c program using functions and pointers that compares
two strings to see whether they are identical. The function returns 1 if
they are identical, 0 otherwise.
30 13b)Write a C program to sort list of numbers using pointers.
31 14a) Write a c program to implement following pre-processor
directives. (i)define (ii) ifdef (iii)undef (iv) ifndef.
32 14b) Write a c program to create a user defined header file to find
sum, product and greatest of two numbers?
33 15a) Write a C program to merge two files into a thirdfile.
34 15b) Write a C program to find some of n numbers using
command linearguments.
Task1
1a)Writeasimpleprogramthatprints the resultsofalltheoperators
availableinC(includingpre/postincrement,bitwiseand/or/not,etc.).Read
requiredoperandvaluesfromstandardinput.
Algorithm:
Step1:Start Step2:Read3numbersintoa,b,c. Step3:DisplayArithmeticoperators andvalueofa+b,a-b,a/b,a%b,a*b. Step4:DisplayRelationaloperators andvalueofa>b,a<b,a==b,a!=b,a>=b,a<=b.
Step5:Display Logicaloperatorsandvalueofa+b!=5&&a>=b,a<b||a==2,!(a==b).
Step6:Displayconditionaloperator andvalueofa>b?a:b. Step7:DisplayIncandDecoperators andvalueofa++,++c,--b,c--.
Step8:DisplayAssignmentoperators andvalueof a+=5,c-=4.
Step9:Displayspecial operators andvalueofsizeof(int),&a.
Step10:Display Bitwiseoperators andvalueofa&b,a|b,a^b,~a,a<<2,b>>2.
Step11:Stop
Flowchart:
Program:
Output:
1b). Write a C program to find greatest and smallest among three numbers using conditional
operator.
Algorithm:
Step1:Start Step2:Readthree numbers into a,b,c. Step3:Computemax=a>b?a>c?a:c:b>c?b:c;
Step4:Computemin=a<b?a<c?a:c:b<c?b:c;
Step5:Displaymax and min.
Step6:Stop
Flowchart:
Program:
Output:
1c) Write a C program to implicit and explicit type conversion inc?
Algorithm:
Step1:Start Step2:Readacharcterinch.
Step3:Displayasciivalueofch.
Step4:Readoneinteger,float,double,longintvaluesintoi,f,d,j.
Step5:Computex1/j+i*f-d.
c1/(float)j+i*f-d.
Step6:Displayx,c.
Step7:Stop
Flowchart:
Program:
Output:
Task2
2a) Write a C program to swap two numbers using the following.
(i) Using thirdvariable
(ii) Without using thirdvariable
(iii) Using bitwiseoperators
Algorithm:
Step 1: Start
Step 2: Read three numbers into a,b.
Step 3: Display The values before swapping – a,b.
Step 4: Compute c=a;a=b;b=c;
Step 5: Display The values after swapping – a,b.
Step 6: Stop
Flowchart:
Program:
Output:
(ii)Without using thirdvariable
Algorithm:
Step 1: Start
Step 2: Read three numbers into a,b.
Step 3: Display The values before swapping – a,b.
Step 4: Compute a=a+b; b=a-b;a=a-b;
Step 5: Display The values after swapping – a,b.
Step 6: Stop
Flowchart:
Program:
Output:
(iii)Using bitwiseoperators
Algorithm:
Step 1: Start
Step 2: Read three numbers into a,b.
Step 3: Display The values before swapping – a,b.
Step 4: Compute x=x^y;y=x^y;x=x^y;
Step 5: Display The values after swapping – a,b.
Step 6: Stop
Flowchart:
Program:
Output:
2b) Write a c program to add two numbers without using arithmetic operators inc?
Algorithm:
Step 1: Start
Step 2: Read three numbers into a , b.
Step 3: Repeat the step 3 until while (b!=0)
Step 3.1 : c=a&b;
Step 3.2 : a=a^b;
Step 3.3 : b=c<<1;
Step 4: Display Sum a.
Step 5: Stop
Program:
Output:
Task3
3a) Write a C program to find the roots of a quadratic equation usingif-else.
Algorithm:
Step1:start
Step2:reada,b,cvalues Step3:computed=b*b-4*a*c Step4:checkwhether the valueis equaltozeroif sogotostep7otherwisegoto step5
Step5:checkwhetherd>0ifsogotostep10otherwisegotostep6 Step6:displayroots areimaginaryandgotostep13 Step7:compute r1=-b/2*aandgotostep8 Step8:compute r2=-b/2*aandgotostep9 Step9:displayroots arerealanddisplayr1andr2values andgotostep13 Step10:compute r1=(-b+sqrt(d))/(2*a) andgotostep11 Step11:compute r2=(-b-sqrt(d))/(2*a)andgotostep12 Step12:displayroots arerealanddisplayr1andr2values andgotostep13 Step13:stop
Flowchart:
Program:
Output:
3b)The program should request the user to input two numbers and display one of the following
as per the desire of user. (a). Sum of numbers (b) difference of numbers (c) product of the numbers
(d)division of the numbers. Write a C program using switch statement to accomplish the abovetask.
Algorithm: Step1:start Step2:Readoption+or–or*or/or%
Step3:Reada,bvalue
Step4:ifoptionis + res=a+bop
Step5: ifoptionis- res=a-b
Step6: ifoptionis * res=a*b
Step7: ifoptionis / res=a/b
Step8: ifoptionis- res=a%b
Step9:ifoptiondoesnotmatchwith +or–or*or/or thenprintinvalid option
Step10:printa,b,result Step11:stop
FlowChart:
Program:
OUTPUT:
Task-4:
a) Write a c Program check whether a given number is perfect number ornot.
Algorithm:
Step 1: start
Step 2: Read n
Step 3: i=1
Step 4: s=0
Step 5: if (i<n) goto step 6 otherwise goto step 10
Step 6: if (n%i==0) goto step 7 otherwise goto step 8
Step 7: s=s+i
Step 8: i=i+1
Step 9: goto step 5
Step 10: if(s==n) goto step11 otherwise goto step 12
Step 11: print “The given number is perfect number “ andgoto step 13
Step 12: print “The given number is not perfect number “ and goto step 13
Step 13: stop
FlowChart:
Program:
OUTPUT:
4b)Write a c Program check whether a given number is palindrome number or not.
Algorithm:
Step1:Start Step2:Readnumaspositiveinteger Step3:Initializesum=0 Step4:While(num) Begin
digit<-num MOD10; num<-numDiv10
Step5:Repeatstep4tillnum!=0 Step6:DisplaysumofdigitsStep7:Readnumbe
ragain
Step8:While(num!=0) Begin rem=m%10
rev=rem+rev*10 m=m/10
Step9: Repeatstep8untillnum!=0
Step10:Ifnum==rev it’s a palindrome else not Step11:Stop
Flowchart:
Program:
Output:
4c)Write a c Program check whether a given number is armstrong number ornot.
Algorithm:
Step 1: Start
Step 2: Declare Variable sum, temp, num
Step 3: Read num from User
Step 4: Initialize Variable sum=0 and temp=num
Step 5: Repeat Until num>=0
5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]
5.2 num=num/10
Step 6: if sum==temp
Print "Armstrong Number"
else
Print "Not Armstrong Number"
Step 7: Stop
Flowchart:
Program:
Output:
Task-5:
a) Write a C program to display the followingpatterns.
(i) 1 (ii) 1
2 3 2 3
4 5 6 4 5 6
7 8 9 10 7 8 9 10
Algorithm:
Step 1:Start .
Step 2:Read the limit of n.
Step 3: Display pyramid of numbers and set to new line.
Step 4:Initialize the value of i=1.
Step 5:Initialize the value k=1.
Step 6: Repeat step5 to step 12 until i<=n otherwise go to stop step.
Step 7: Initialize the value j=1.
Step 8: Repeat step 7 to step 9 until j<=n otherwise go to stop step.
Step 9: incremented j by 1 and go to step 8.
Step 10: Display in 3 width k.
Step 11: Display set to new line.
Step 12: Increment i by 1 and go to step 5.
Step 13: stop.
Program:
Output:
2) Program:
Output:
5b)write a C program to generate the prime numbers between x and y where x and y are
starting and ending values to be suppliedby the user.
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i value to 2
Step 4: repeat steps 5,6,7,8,9,10,11,12 until i<=n otherwise goto step13
Step 5: initialize c value to 0 and j value to 2
Step 6: repeat steps 7,8,9 until j<=i-1 otherwise goto step10
Step 7: check whether i%j==0 if so goto 8 otherwise 10
Step 8: increment c value by 1
Step 9: increment j value by 1 and goto step6
Step 10: check whether c==2 if so goto step 11 otherwise goto step12
Step 11: display i value and goto step12
Step 12: increment i value by 1 and goto step4
Step 13: stop
Flowchart:
Program:
Output:
5c)Write a C program to calculate the following Sum:
Sum=1+x/1!-x2/2!+x3/3!-x4/4!+ .................... xn/n!
Algorithm:
Step 1: start
Step 2: read n value and x value
Step 3: initialize d=2 and sum=1 and sign=-1
Step 4: repeat steps 5,6,7,8 until m<=n
Step 5: check whether m<=n if yes goto step 6
Step 6: compute sum+=sign*((pow(x,m))/d)
Step 7: assign sign=-sign and d=d+2
Step 8: increment m value to 2 and goto step 4
Step 9: display sum value
Step 10: stop
Flowchart:
Program:
Output:
TASK6
6a)Write a C program to find sum, average and minimum and maximum in a list ofnumbers.
Algorithm:
Step1:Start. Step2:Readthesizeofarrayandstore inn. Step3:Readtheelements
ofanarray.
Step4:InitializeIto0andsto0.
Step5:Repeatsteps 6untili<n.
Step6:Computes<-s+a[i].
Step7:Calculateavg<-s/n.
Step8:Setmin=max=a[0].
Step9:InitializeIto0.
Step10:Repeatsteps 10, 11untili<n. Step11:Checkwhethera[i]<minifsoassigna[i]valuetomin.
Step12:Checkwhethera[i]>maxifsoassigna[i]valuetomax.
Step13:Displaymax.
Step14:Displaymin.
Step15:Displayavg.
Step16:Stop.
Program:
Output:
6b)Write a C program to implement linearsearch.
Algorithm:
Step1:Setito1 Step2:ifi>nthengotostep7 Step3:ifA[i]=xthengotostep6 Step4:Setitoi+1 Step5:GotoStep2 Step6:PrintElementxFoundatindexiandgoto step8 Step7:Printelementnotfound Step8:Exit
Program:
Output:
6c)Write a C program to implement binarysearch.
Algorithm:
Step1:start
Step 2:Readthesearchelementfromtheuser Step3:Findthemiddleelementinthesortedlist Step4:Compare,thesearchelementwiththemiddleelementinthesorted list.
Step5:Ifbotharematching,thendisplay"Givenelementfound!!!" andterminate thefunction
Step6:Ifbotharenotmatching,thencheckwhetherthesearchelementis smaller largerthanmiddleelement.
Step7:Ifthesearchelementis smallerthanmiddleelement, thenrepeatsteps
2,3,4and5fortheleftsublistofthemiddleelement. Step8:Ifthesearchelementislargerthanmiddleelement, thenrepeat steps 2,3, 4and5fortherightsublistofthemiddleelement. Step9:Repeatthesameprocess untilwefindthesearchelementinthelistor untilsublistcontains
onlyoneelement.
Step10:Ifthatelementalsodoesn'tmatchwiththesearchelement,
thendisplay"Elementnotfoundinthelist!!!" andterminatethefunction.
Step11:stop
Program:
Output:
7c)WriteaC programthauses functions toperformthefollowing:
i.AdditionofTwoMatrices
Algorithm:
Function:main() Step1:Start Step2:Readorderofmatrixaintor1,c1.
Step3:Readorderofmatrixbintor2, c2.
Step4:Checkwhetherr1==c1ANDr2==c2iftruegotostep5otherwisegotostep9.
Step5:Read(a,r1,c1).
Step6:Read(b,r2,c2). Step7:Add_Matrix(a,b,c,r1,c1). Step8:DisplaySUMMatrixC, Display(C,r1,c1). Andgotostep10.
Step9:DisplayMatrixadditionnotpossible.
Step10:Stop. Function:Read(int*x,intr,intc): Step1:Start Step2:Allocatememorydynamicallyformatrixa.
x=(int*)calloc(r*c,sizeof(int)).
Step3:Seti=0. Step4:Repeatsteps 4to9untilli<rotherwisegotostep10. Step5:setj=0.
Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.
Step7:Readelementinto*(x+i*c+j).
Step8:Incrementjby1.
Step9:Incrementiby1..Step10:Stop.
Function:Add_Matrix(int*a, int*b, int*Cintr,intc): tart Step2:Allocatememorydynamicallyformatrixa. C=(int*)calloc(r*c,sizeof(int)). Step3:Seti=0.
Step4:Repeatsteps 4to9untilli<rotherwisegotostep10. Step5:Setj=0.
Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9. Step7:Addelementsinto*(C+i*c+j)=*(a+i*c+j)+*(b+i*c+j). Step8:Incrementjby1.
Step9:Incrementiby1..Step10:Stop.
Function:Display(int*x,intr,intc): Step1:Start Step2:Seti=0. Step3:Repeatsteps 3to8untilli<rotherwisegotostep10. Step4:setj=0.
Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.
Step6:Displayelementin*(x+i*c+j).
Step7:Incrementjby1.
Step8:Incrementiby1..Step9:Stop
Program:
61
Output:
ii)MultiplicationofTwoMatrices
Algorithm:
Function:main(): Step1:Start Step2:Readorderofmatrixaintor1,c1.
Step3:Readorderofmatrixbintor2, c2.
61
Step4:Checkwhetherc1==r2iftruegotostep5otherwisegotostep9. Step5:Read(a,r1,c1).
Step6:Read(b,r2,c2). Step7:Multiply_Matrix(a,b,C,r1,c1,c2). Step8:DisplayProductMatrixC,Display(C,r1,c2).Andgotostep10.
Step9:DisplayMatrixmultiplicationnotpossible.
Step10:Stop. Function:Read(int*x,intr,intc): Step1:Start Step2:Allocatememorydynamicallyformatrixa.
x=(int*)calloc(r*c,sizeof(int)).
Step3:Seti=0. Step4:Repeatsteps 4to9untilli<rotherwisegotostep10. Step5:setj=0.
Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.
Step7:Readelementinto*(x+i*c+j).
Step8:Incrementjby1.
Step9:Incrementiby1..Step10:Stop.
Function:Multiply_Matrix(int*a,int*b, int*C,intr1,intc1,int c2): Step1:Start Step2:AllocatememorydynamicallyformatrixC. C=(int*)calloc(r1*c2,sizeof(int)). Step3:Seti=0.
Step4:Repeatsteps 4to12untilli<r1otherwisegotostep10. Step5:setj=0.
Step6:Repeatsteps 6to11untillj<c2otherwisegotostep9.
Step7:Set*(C+i*c2+j)=0,k=0.
Step8:Repeatsteps 8to10untillk<c1otherwisegotostep11. Step9:Multiplyelementsinto*(C+i*c2+j)+=*(a+i*c2+j)**(b+i*c2+j).
Step10:Incrementkby1.
Step11:Incrementjby1.
Step12:Incrementiby1. Step13:Stop.
61
Function:Display(int*x,intr,intc): Step1:Start Step2:Seti=0. Step3:Repeatsteps 3to8untilli<rotherwisegotostep10. Step4:setj=0.
Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.
Step6:Displayelementsin*(x+i*c+j).
Step7:Incrementjby1.
Step8:Incrementiby1.
Step9:Stop
Program:
64
64
.
69
Output:
iii)Transposeofamatrix
Algorithm:
Function:main(): Step1:Start Step2:Readorderofmatrixaintor, c. Step3:Read(a,r,c).
Step4:Trnspose_Matrix(a,b,r,c). Step5:DisplayTransposeMatrixb, Display(b,c,r).
Step6:Stop.
Function:Read(int*x,intr,intc):
69
Step1:Start
Step2:Allocatememorydynamicallyformatrixa. x=(int*)calloc(r*c,sizeof(int)).
Step3:Seti=0.
Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.
Step5:setj=0.
Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.
Step7:Readelementinto*(x+i*c+j).
Step8:Incrementjby1.
Step9:Incrementiby1..
Step10:Stop.
Function:Transpose_Matrix(int*a,int*b, intr,intc): Step1:Start
Step2:Allocatememorydynamicallyformatrixb. b=(int*)calloc(r*c,sizeof(int)).
Step3:Seti=0. Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.
Step5:setj=0. Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9
. Step7: Transposeelementsinto*(b+j*r+i)=*(a+i*c+j).
Step8:Incrementjby1.
Step9:Incrementiby1.
Step10:Stop.
Function:Display(int*x,intr,intc): Step1:Start Step2:Seti=0. Step3:Repeatsteps 3to8untilli<rotherwisegotostep10.
Step4:setj=0.
Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.
Step6:Displayelementsin*(x+i*c+j).
Step7:Incrementjby1.
Step8:Incrementiby1..Step9:Stop.
69
Program:
69
Output:
69
Task-8:
8a)Write a C program to implement the following string handlingfunctions.
(i) strlen() (ii) strcpy()(iii)strcmp()(iv) strcat()
Algorithm:
Step 1: Start.
Step 2: Read String into s1.
Step 3: Display Length of string as strlen(s1);
Step 4: Compute strcpy(s2,s1);
Step 5: Display Original string s1, Copied String s2.
Step 6: Check if(strcmp(s1,s2)==0) then
Step 7: Display Strings s1 and s2 are identical and goto Step 9
Step 8 : Otherwise Display Strings s1 and s2 are not identical.
Step 9: Compute strcat(s1," Hello");
Step 10: Display Concatenated String , s1.
Step 11:Stop.
Program:
69
Output:
69
8b)Write a C program to read first name , middle name and last name of a student and
display a string full name without using string handlingfunctions.
Algorithm:
Step 1: Start.
Step 2: Read first name into first.
Step 3: Read middle name into middle.
Step 4: Read last name into last.
Step 5: Set i = 0.
Step 6: Repeat Steps 4 to 6 until first[i]!='\0'
Step 7: Assign fullname[k++]=first[i];
Step 8: Increment i.
Step 9: Assign fullname[k]=' ';
Step 10: Repeat Steps 10 to 12 until middle[i]!='\0'
Step 11: Assign fullname[k++]=middle[i];
Step 12: Increment i.
Step 13: Assign fullname[++k]=' ';
Step 14: Repeat Steps 14 to 16 until last[i]!='\0'
Step 15: Assign fullname[k++]=last[i];
Step 16: Increment i.
Step 17: Assign fullname[++k]=' \0';
Step 18: Display Full Name of Student in fullname.
Step 19: Stop.
69
Program:
69
Output:
TASK-9
9a)WriteaCprogramtodetermineifthegivenstringisapalindromeornot(Spelledsameinbothdirectionswithorwithoutameaninglikemadam,civic,noon,abcba,etc.)
Algorithm:
Step1:Start Step2:Declaretwocharacterarraysstr, str1 Step3:Declarethevariablesx,I,j;
Step4:initializej=0;
Step5:readtheinputstringstr Step6:initializei=strlen(str)-1 Step7:repeatthestep7.1untili>=0
7.1 str1[j]=str[i]
Step8:Assignnulltostr1[j] Step9:Assignx=strcmp(str,str1) Step10: Ifx==0thendisplaystringis palindrome
elsedisplaystringisnotpalindrome
69
Step11:End
Program:
Output:
69
9b)Write a C program to sort the names of n students in the alphabeticalorder.
Algorithm:
Step 1:start
Step2: n=length(A)
Step3:repeat
Step4: swapped=false Step5:fori=1ton-1inclusivedo Step6:if(strcmp(A[i-1],A[i])>0)then Step7:swap(A[i-1],A[i])
Step8: swapped=true
Step9:endif
Step10:endfor Step11:n=n-1 Step12:untilnotswapped Step13endprocedure
Program:
69
Output:
69
TASK-10
WriteC programs thatusebothrecursiveandnon-recursivefunctions
i. Tofindthefactorialofagiveninteger.
ii. TofindtheGCD(greatestcommondivisor)oftwogivenintegers. i) Tofindthefactorialofagiveninteger.
Algorithm:
MainFunction
Step1:Start Step2:Readnumbern Step3:checkif(n==0)is true Display factorialof0is 1. Step4:elseCallfunctionrec_fact(n)
Step5:callfunctioniter_fact(n)
Step6:Printfactorialofanumber Step5:Stop Recursionfunctionrec_fact(n)
Step1:checkif(n==1)is truereturn1 Step2:elserepeatstepsfrom3to4tilli<=n Step3:fact=fact*i Step4:incrementi(i++) Step5:returnfact
NonRecursionfunctioniter_fact(n)
Step1:start Step2:checkif(n==1)return1 Step3:repeatsteps 4to5tilli<=n Step4:fact=fact*i Step5:i++ Step6:return(fact)
Program: (Recursive and Non Recursive)
Output:
ii)TofindtheGCD(greatestcommondivisor)oftwogivenintegers.
Algorithm:
Step1: Start Step2:Readaandbnumbers Step3:callrec_gcd(a,b)
Step4:calliter_gcd(a,b)
Step5:stop
Function rec_gcd(n1,n2)
Step1:checkif(n2>n1)is true
returnrec_gcd(n2,n1);
Step2:checkif(n2==0)
returnn1;
Step3:elsereturnrec_gcd(n2,n1%n2) Function iter_gcd(n1,n2)
Step 1: Repeat steps2 to 4 till (n2!=0)
Step 2: calculate temp=n1%n2
Step 3: n1=n2
Step 4: n2=temp
Step 5: return n1
Program: (Recursive and Non Recursive)
Output:
Task-11:
11a )Write a c program to implement transpose of a matrix using functions.
Algorithm:
Function : main( )
Step 1: Start
Step 2: Read order of matrix into r, c.
Step 3: Set i=0.
Step 4: Repeat steps 4 to 9 untill i<r otherwise goto step 10. Step
5: Set j=0.
Step 6: Repeat steps 6 to 8 untill j<c otherwise goto step 9.
Step 7: Read element into matrix[i][j].
Step 8: Increment j by 1.
Step 9: Increment i by 1.
Step 10: Display “original matrix”.
Step 11: Call function Display(matrix,r,c).
Step 12: Call function findTranspose(matrix,m,n);
Step 13 : Stop.
Function: findTranspose(int a[][5], int r, int c):
Step 1: Start
Step 2: Set i=0.
Step 3: Repeat steps 3 to 8 untill i<r otherwise goto step 9.
Step 4: Set j=0.
Step 5: Repeat steps 5 to 8 untill j<c otherwise goto step 9.
Step 6: Compute transpose[j][i]=a[i][j].
Step 7: Increment j by 1.
Step 8: Increment i by 1..
Step 9: Stop.
Function : Display(int x[][5], int r, int c):
Step 1: Start
Step 2: Set i=0.
Step 3: Repeat steps 3 to 8 untill i<r otherwise goto step 9. Step
4: set j=0.
Step 5: Repeat steps 5 to 7 untill j<c otherwise goto step 9.
Step 6: Display element in a[i][j].
Step 7: Increment j by 1.
Step 8: Increment i by 1.
Step 9: Stop
Program:
Output:
11b)Write a c program to display binary equivalent of a given decimal
number.
Algorithm:
Function : main( )
Step 1: Start
Step 2: Read a number n.
Step 3: Set i=0.
Step 4: Display Decimal number n.
Step 5: Call Function bin = Convert(n);
Step 6: Display Binary Number bin.
Step 7: Stop.
Function: long longconvert(int n) :
Step 1: Start
Step 2: Set bin =0, i=1.
Step 3: Repeat steps 3 to 7 untill (n!=0) otherwise goto step 8.
Step 4: Compute rem=n%2;
Step 5: n=n/2;
Step 6: bin += rem * i;
Step 7: i *= 10;
Step 8: return bin.
Step 9: Stop.
Program:
Output:
Task-12:
12a)Create a structure student with name ,rollno,marks of 3 subjects as members . Write a c
program to sort student details based ontotal using structures and functions .
Algorithm:
Main Function:
Step 1: Start
Step 2: Declare a Structure with rollno, name, marks and total as structure
members.
Step 3: Read number of students n.
Step 4: Set i=0.
Step 5: Repeat steps 4 to 5 until i<n to read the data of n students.
Step 6: Read Rollno, name of student i into s[i].rollno, s[i].name.
Step 7: Set j=0.
Step 7: Repeat Step 7 to 9 until j<3 to read marks of 3 subjects.
Step 8 : Read marks of student i into s[i].marks[j].
Step 9 : Increment j.
Step 10 : Increment i.
Step 11 : Call the Function StudentSort(s,n);
Step 12 : Stop
Function StudentSort(struct student s[],int n) :
Step 1: Start
Step 2: Set i =0.
Step 3: Repeat Steps 3 to 8 until i<n otherwise goto Step 9.
Step 4: Set j=0.
Step 5: Repeat steps 5 to 7 until j<n otherwise goto Step 8.
Step 6: Check if (s[i].total<s[j].total) then
Step 6.1: Assign temp=s[i] where temp is struct student type
variable.
Step 6.2: s[i]=s[j];
Step 6.3: s[j]=temp;
Step 7: Increment j.
Step 8: Increment i.
Step 7: Set i=0.
Step 8 : Repeat the steps 8 to 10 until i< n to display the details of students
.
Step 9 : Display Rank ,Student rollno , name and totalmarks.
Step 10 : Increment i.
Step 11 : Stop.
Program:
Output:
12b)Write a C program that uses structures and functions to perform the
followingoperations:
i. Addition of two complex numbers
ii. subtraction of two complex numbers
iii. Multiplication of two complex numbers
Algorithm :
Main Function :
Step 1: Start
Step 2: Define a Structure with real and imaginary members.
Step 3: Read Complex number c1.
Step 4: Call Function Read (&c1).
Step 5: Read Complex number c2.
Step 6: Call Function Read (&c2).
Step 7: Display Complex number c1.
Step 8: Call Function Display (c1).
Step 9: Display Complex number c2.
Step 10: Call Function Display (c2).
Step 11: Add Complex number c1, c2.
Step 12: Call Function res= Add (c1,c2);
Step 13: Display Complex number res.
Step 14: Call Function Display(res).
Step 15: Subtract Complex number c1, c2.
Step 16: Call Function res= subtract (c1,c2);
Step 17: Display Complex number res.
Step 18: Call Function Display(res).
Step 19: Multiply Complex number c1, c2.
Step 20: Call Function res= Multiply (c1,c2);
Step 21: Display Complex number res.
Step 22: Call Function Display(res).
Step 23:Stop.
Function read (complex *c):
Step 1: Start
Step 2: Display “Enter real and imaginary part of complex number”.
Step 3: Read c->rel;
Step 4: Read c->img;
Step 5: Stop.
Function display (complex c):
Step 1: Start
Step 2: Check if (c.img< 0) then
Step 3: Display c.rel - i c.img in c.rel,-c.img and goto step 5
Step 4: Otherwise Display c.rel + i c.img in c.rel,c.img.
Step 5: Stop.
Function Add( complex c1,complex c2) :
Step 1: Start
Step 2: Declare complex c.
Step 3: c.rel=c1.rel+c2.rel;
Step 4: c.img=c1.img+c2.img;
Step 5: return(c) the sum of c1,c2.
Step 6: Stop.
Function Subtract (complex c1, complex c2) :
Step 1: Start
Step 2: Declare complex c.
Step 3: c.rel=c1.rel-c2.rel;
Step 4: c.img=c1.img-c2.img;
Step 5: return(c) the difference of c1,c2.
Step 6: Stop.
Function Multtiply (complex c1, complex c2) :
Step 1: Start
Step 2: Declare complex c.
Step 3: c.rel= c1.rel*c2.rel- c1.img*c2.img;
Step 4: c.img= c1.rel*c2.img+c1.img*c2.rel;
Step 5: return(c) the product of c1,c2.
Step 6: Stop.
Program:
OUTPUT:
Task-13:
`13a) Write a c program using functions and pointers that compares two strings to see
whether they are identical. The function returns 1 if they are identical, 0 otherwise.
Algorithm :
Main Function:
Step 1: Start
Step 2: Read the string1 into str1.
Step 3: Read the string1 into str2.
Step 4: Call the function x=StringCompare(str1,str2);
Step 5: Check if (x==1) then goto step 6 otherwise goto Step 7
Step 6: Display Strings str1,str2 are identical and goto Step 8.
Step 7: Display Strings str1,str2 are not identical.
Step 8: Stop.
Function StringCompare(char *s1,char *s2) :
Step 1: Start
Step 2: Set i =0.
Step 3: Repeat Steps 3 to 4 until *(s1+i)==*(s2+i) && *(s1+i)!='\0' && *(s2+i)!='\0'
Step 4: Increment i by 1.
Step 5: Check if(*(s1+i)=='\0'&& *(s2+i)=='\0') then return 1 otherwise return 0.
Step 6: Stop.
Program:
Output:
13b) Write a C program to sort list of numbers using pointers.
ALGORITHM:
Function : main( )
Step 1: Start
Step 2: Read the size of the array n.
Step 3: Read n elements in to array a.
Step 4: Display Unsorted List.
Step 5: Call the Function display(a,n);.
Step 6: Call the Function sort(a,n);.
Step 7: Display Sorted List.
Step 8: Call the Function display(a,n);.
Step 9: Stop.
Function: sort(int *a, int n):
Step 1: Start
Step 2: Set i=0.
Step 3: Repeat steps 3 to 8 untill i< n-1 otherwise goto step 9.
Step 4: Set j=0.
Step 5: Repeat steps 5 to 7 untill j<n-i-1 otherwise goto step 8.
Step 6: Check if (*(a+j)>*(a+j+1)) then
Step 6.1: temp=*(a+j);
Step 6.2: *(a+j)=*(a+j+1);
Step 6.3: *(a+j+1)=temp;
Step 7: Increment j by 1.
Step 8: Increment i by 1..
Step 9: Stop.
Function : Display(int x[][5], int r, int c):
Step 1: Start
Step 2: Set i=0.
Step 3: Repeat steps 3 to 5 untill i<n otherwise goto step 6.
Step 4: Display element in *(a+i).
Step 5: Increment i by 1.
Step 6: Display newline.
Step 7: Stop
Program:
Output:
Task-14:
14a)Write a c program to implement following pre-processor
directives.
(i)define
Algorithm:
Step 1: Start
Step 2: Define PI 3.142
Step 3: Read radius R
Step 4: Compute ar=PI*R*R;
Step 5: Display area of Circle ar.
Step 6: Stop.
Program:
Output:
(ii)ifdef
Algorithm:
Step 1: Start
Step 2: check ifdef ABC then
Step 2.1: undef ABC
Step 2.2: Define ABC 500
Step 3: Otherwise
Step 3.1: Define ABC 100
Step 4: Display ABC.
Step 5: Stop.
Program:
Output:
(iii)undef
Algorithm:
Step 1: Start
Step 2: Define MAX 10
Step 3: Undef MAX
Step 4: Define MAX 20
Step 5: Display MAX.
Step 6: Stop.
Program:
Output:
(iv)ifndef.
Algorithm:
Step 1: Start
Step 2: check ifndef ABC then
Step 2.1: Define ABC 500
Step 3: Otherwise undef ABC
Step 3.1: Define ABC 100
Step 4: Display ABC.
Step 5: Stop.
Program:
Output:
14b)Write a c program to create a user defined header file to find sum, product and greatest
of two numbers?
Algoirthm:
Header File :
Step 1: Start
Step 2: Define the function - intsum(int a, int b)
Step 2.1 : return(a+b);
Step 3: Define the function - void product(int a, int b)
Step 3.1 : Print a, b, a*b;
Step 4: Define the function – int FindMax(int a, int b)
Step 4.1 : return(a>b?a:b);
Step 5: Save file as mylib.h or mylib.c – compile and run the file.
Step 6: Stop
Main Program:
Step 1: Start
Step 2: Include “mylib.h”
Step 3: Read a,b values.
Step 4: Call the Function sum(a,b);
Step 5: Display sum.
Step 6: Call the Function product(a,b);
Step 7: Call the Function FindMax(a,b);
Step 8: Display Max value.
Step 9: Stop
Program:
Output:
Task-15:
15a)Write a C program to merge two files into a thirdfile.
Algorithm:
Step1:Open file1.txtandfile2.txtinreadmode.
Step2:Open file3.txtinwritemode.
Step3:Runalooptoonebyonecopycharacters offile1.txttofile3.txt.
Step4:Runalooptoonebyonecopycharacters offile2.txttofile3.txt.
Step5:Closeallfiles.
Program:
Output:
15b)Write a C program to find some of n numbers using command linearguments.
Algorithm:
Step 1: Start
Step 2: Read the inputs name of executable file and the n numbers through command
prompt.
Step 3: Declare a variable sum =0.
Step 4: Set i =1.
Step 4: Repeat the steps 4 to 6 untill i <agrc .
Step 5: Compute sum =sum + atoi(argv[i]).
Step 6: Increment I by 1.
Step 7: Display the sum
Step 8: Stop
Program:
Output:
sample Viva Questions
1. What do you mean by Hardware and Software?
2. Mention the main components of a computer and their functions.
3. What is Operating System(OS) ?
4. What is Algorithm?
5. What is Flowchart ?
6. Name the four basic data types in “C” language?
7. Describe atleast five different format specifiers?
8. Define and explain printf () function?
9. Define and explain scanf () function?
10. What is preprocessor?
11. What exactly is a ‘variable scope’, ‘local variables’ and ‘global variables’?
12. What are signed values?
13. What are unsigned values?
14. Mention the different types of operators used in C
15. What is identifier
16. What is Loop control statements?
17. explain while loop
18. explain do while loop
19. Explain for loop
20. What is an array
21. What is Multidimensional Arrays
22. What is string?
23. Mention four important string handling functions in C languages
24. Define and explain about ! Operator?
25. What is operator precedence
26. 29) What is operator associativity?
27. Define function
28. Differentiate built-in functions and user – defined functions.
29. Distinguish between actual and formal arguments.
30. Explain the concept and use of type void.
31. What is recursion
32. Explain increment and decrements operators
33. What is a Keyword with examples?
34. What are the Back Slash character constants or Escape sequence charactersavailable in
C?
35. What is a Unary operator and what are the unary operators present in C?
36. Explain the working of break and continue?
37. Explain the difference between break and continue
38. What are the differences between structure and an array?
39. What is a pointer?
40. What is command line argument?
41. What is File?
42. How to open and close a file?
43. Explain about storage classes available in C
44. What is a self-referentialstructure.
45. Differentiate between Unformatted and formatted i/o statements.
46. How does the function definition differ from function declaration?
47. Differentiate between union and structure.
48. Why is the function main() special?
49. What are the two types of comments, and how do they differ?
50. Can comments be nested?
51. If a function doesn't return a value, how do you declare the function?
52. What is scope of a variable?
53. What is Dangling pointer?
54. What is void pointer?