You are on page 1of 276

COMPUTER PROGRAMMING LAB MANUAL

I B-TECH

COMPUTER PROGRAMMING LAB MANUAL

VIDCOMPUTER SCIENCE AND


ENGINEERINGI

SVIDYA VIKAS INSTITUTE OF


TECHNOLOGY

D KOTESWARARAO, 2

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 3

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

INDEX
S.No. 1. 33 2. 34 3. 4. 5. 40 6. 43 7. 44 8. 45 9. 46 10. 47 NAME OF THE PROGRAM Sum of Digits of Given Number Fibonacci Series Prime Numers between 1 to N Evaluate the expression Roots of Quadratic equations Factorial of Given Number using Non-Recursive Factorial of Given Number using Recursive GCD of Two numbers using Non-Recursive GCD of Two numbers using Non-Recursive Towers of Hanoi Problem 35 39 PAGE No.

D KOTESWARARAO, 4

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


11. 51 12. 53 13. 57 14. 59 15. 61 16. 67 17. 69 18. 19. 74 20. 79 21. 80 22. 83 23. 87 24. 88 25. 93 Total Distance Travelled by Vehicle Arithmetic Operations using Switch case Largest and Smallest number in a list of Integer Matrix Addition Matrix Multiplication Insert and Delete sub-string in main String Given String is Palindrome or not To Display portion of string if string in Main String 73 To count no. of characters, words and lines in a Text Pascals Tr iangle

Pyrmid of Numbers Geometric Regression To Find 2s Complement of Given Number To Convert Roman To decimal Number Complex Numbers

D KOTESWARARAO, 5

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


26. 99 27. 100 28. 103 29. 113 30. 125 31. 129 32. 137 33. 141 34. 149 35. 152 36. 157 37. 165 38. 166 39. 168 40. 173 To Copy one File into another File To Reverse the first n Characters in a File Single Linked List Double Linked List Stack implements using Arrays Stack implements using Pointers Queue implements using Arrays Queue implements using Pointers Infix to Postfix Conversion Postfix Evaluation Binary Tree Linear search Binary search using Non-Recursive Binary search using Recursive Bubble Sort

D KOTESWARARAO, 6

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


41. 175 42. 179 43. 180 44. 185 Quick Sort Insertion Sort Merge Sort To implement the Lagrange Interpolation

45. To implement the Newton-Gregory Forward Interpolation186 46. 191 47. 195 48. 201 To implement the Linear Regression To implement Simpson Method To implement Tr apezoidal Method

D KOTESWARARAO, 7

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 8

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

DOS COMMANDS

D KOTESWARARAO, 9

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 10

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

DOS COMMANDS: 1. Copy con: Definition: It is used to create a new file.

D KOTESWARARAO, 11

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Syntax: Example: C:\copy con <file name> C:\copy con ABC F6 (or) Ctrl +Z are used to save file. 2. Type: Definition: It is used to give contents of a file. Syntax: Example: C:\type <file name> C:\type ABC

3. MD (Make Directory): Definition: It is used to create a new Directory. Syntax: Example: C:\md <directory name> C:\md XYZ

4. CD (Change Directory): Definition: It is used to change the new Directory. Syntax: Example: C:\cd <directory name> C:\cd XYZ

5. RD (Remove Directory): Definition: It is used to remove the existing directory. Syntax: Example: C:\rd <directory name> C:\rd XYZ

D KOTESWARARAO, 12

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


6. DIR (Disk Information Report) : Definition: This is used to display the list of all existing files, sub directory and directory on the specified storage device. Syntax: Example: C:\dir C:\dir

7. Date: Definition: It is used to move or modify the current system date. Syntax: Example: C:\ date C:\date Current date is: Fri 10/23/2009 Enter the new date: (mm-dd-yy) 8. Time: Definition: It is used to view or modify the current system time. Syntax: Example: C:\time C:\time Current time is: 17:08:08.39 Enter the new time: (hh: mm: ss.ms) 9. Del (Delete): Definition: It is used to delete a file from the storage device. Syntax: Example:
D KOTESWARARAO, 13

C:\del <file name> C:\del XYZ

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


10. Del /P:

Definition: It is used to create a new file. Syntax: Example: C:\copy con <file name> C:\copy con ABC F6 (or) Ctrl +Z are used to save file. 11. Edit : Definition: It is used to edit the existing file and modify the existing file. Syntax: Example: 12. Copy : Definition: It is used to copy the data from existing file to a new file. Syntax: Example: 13. Move : Definition: It is used to existing file data will be removed to move to a new file. Syntax: Example: C:\move <existing file name> <new file name> C:\move ABC CBA C:\copy <existing file name> <new file name> C:\copy ABC DEF C:\edit <file name> C:\edit ABC

F6 (or) Ctrl +Z are used to save file.

14. Cls: Definition: It is used to clear the out-put screen.


D KOTESWARARAO, 14 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Syntax: Example: 15. Ren : Definition: It is used to rename an existing file. Syntax: Example: 16. Ver : Definition: It display the version of currently used operating system. Syntax: Example: 17. help: Definition: It helps in giving complete information about specified command. Syntax: Example: C:\help <command name> C:\help copy C:\ver Microsoft Windows XP [Version 5.1.2600] C:\ren <old file name> <new file name> C:\ren ABC PSR C:\cls C:\cls

******************

D KOTESWARARAO, 15

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 16

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

INTRODUCTION
TO

C - LANGUAGE

D KOTESWARARAO, 17

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 18

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Introduction: C is a general purpose programming language C Language was developed by Dennis Ritchie. at AT & T Bell Laboratories of USA in 1972. C language was developed based on BCPL (Basic Combined Programming language) called B language Applications of C: C is used for writing
Operating systems Language Translators( Compiles, Interpreters and Assemblers) Editors Worksheets Database Management Systems games

Advantages of C:
1. C is robust language.

That means C language contains rich set of operators and built-in functions and several data types.
2.

It is a middle-level language.
Associate Professor, CSE Dept.

D KOTESWARARAO, 19

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


That means it support both high level and low level language features so It is suitable for developing application software as well as system software.
3.

C is a portable language.

Portable means Programs written in C in one machine can be easily run on some other machine without any modifications.
4.

Fast execution of programs when compared to other languages. For example compare to BASIC if you want to increment I value 1 to

20000.It takes nearly 60sec but our C language takes only 1sec 5. It has the ability to deal efficiently with bits, bytes, addresses, etc. 6. it is easy to extends itself That means easily add the new features and develop the other programming languages 7. C language have only 32 keywords In C language have only 32 keywords but other languages have above 200 keywords like COBOL so easily remember the keywords to develop the programs
8.

It is a structured language. Structured programs means large programs divide into sub programs

that is called modules or functions, easy to understand, easy to debug, maintenance is very easy, parallel executions Loading, editing, saving and compiling a C-program: Now before entering the program into the computer, C-compiler should be loaded into the computers main memory.
D KOTESWARARAO, 20 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


A Turbo-C compiler can be loaded by tying TC at DOS prompt in the subdirectory where Turbo-C compiler has been stored on the hard disk. For example, suppose you have Turbo-C compiler in the Turbo-C directory in the d disk drive, then first of all change the directory to Turbo-C and then type TC (because TC is an executable file having extension TC.EXE). This is called loading of Turbo-C compile into the main memory.

Save: Compile: Run: Output: New Program: Open(Load):

F2 Alt+F9 Crtl+F9 Alt+F5 Alt+F and press N F3

How to enter C editor: The procedure can be done as described below:(Note: if your C stored in tc then type cd tc instead of cd turboc2)

D KOTESWARARAO, 21

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Whenever you type that command, you are immediately enter to the below editor:

D KOTESWARARAO, 22

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

GENERAL OR BASIC STRUCTURE OF A COMPLETE C-PROGRAM Documentation section


D KOTESWARARAO, 23 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Link Section Definition Section Global Declaration Section main () { Declaration section Executable section } Sub-program Section Function 1() { User defined program area; } Function 2() { Sub program or subroutine; }

D KOTESWARARAO, 24

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 25

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

SYNTAX:

C
LANGUGAE

D KOTESWARARAO, 26

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 27

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Syntax
1) Variable declaration: data_type variable1, variable2,variablen;

2) scanf() scanf(controlstring,&var1,&var2,.&varn);

3) printf() printf(controlstring,var1,var2,.varn);

4) CONDITIONAL CONTROL (OR BRANCHING) STATEMENTS:

(a) if statement (b) if-else statement (c) nested if else


D KOTESWARARAO, 28 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


(d) else if ladder (e) switch statement a) if statement: The general form of the if statement is:

if (expression) Statement; Or if (expression) statement; Or if (expression) { Statement1; Statement2; Statement3; .. .. Statement-n; }

D KOTESWARARAO, 29

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


b)

if else statement:

the general form of the if else statement is

if(expression) { Statement-block-1; } else { Statement-block-2; }

c)

nested if-else statement: The general form of the nested if-else statement is:

if(expression1) { if(expression2) {
D KOTESWARARAO, 30 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Statement-block-1; } else { Statement-block-2; } } else { Statement-block-3; }

d)

else if ladder statement: The general form of the else if ladder statement is:

if (expression1) { Statement-1; Statement-2;


D KOTESWARARAO, 31 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} else if (expression2) { Statement-1; Statement-2; } else if (expression3) { Statement-; Statement-2; } else { Statement-1 Statement-2
D KOTESWARARAO, 32 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

e)

switch statement:

The general form of the switch statement is:

switch (expression) { case constant1: Statement(s) Break; case constant2: Statement(s) Break; .. .. case constantn: constant(s) Break; default:
D KOTESWARARAO, 33 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Statement(s) }

5) LOOPING STATEMENTS:

a) for loop b) while loop c) do-while loop a) for Loop:

The general form of the for loop is:

for (expression1; expression2; expression3) { Statement-1; Statement-2 Statement-3; }


D KOTESWARARAO, 34 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Components of for: The three expressions in the for statement are optional. However, even if an expression is not present, the associated semicolon should be present. For example: for ( ; test expression ; { Statement-1; Statement-2; Statement-3; . . } )

b)

while loop:

while(expression) {

Statement-block;

D KOTESWARARAO, 35

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

c) do- while loop

do {

Statement-block;

} while(expression);

6) Break statement

break;

7) Continue statement

continue;

8) goto statement
D KOTESWARARAO, 36 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

goto label; label:

label: . . goto label; .. ..

6) Array variable declaration:

data_type array_name[size];

7) Function definition:

return_type function_name(argument_list) {

Body of the function;

8) Function prototype:
D KOTESWARARAO, 37 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

return_type function_name(argument_list);

9) Structure:

struct tag_name { data_type member1; data_type member2; .. . data_type member1;

} structure_variables;

10) Union:

union tag_name {
D KOTESWARARAO, 38 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


data_type member1; data_type member2; .. . data_type member1;

`} union variables;

11) Pointer variable declaration:

data_type *variable_name;

D KOTESWARARAO, 39

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-1

D KOTESWARARAO, 40

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 1: Write a C program to find the sum of individual digits of a positive


integer. #include<stdio.h> #include<conio.h> void main() { int n,r,sum=0; clrscr(); printf("Enter n value "); scanf("%d",&n);
D KOTESWARARAO, 41 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


while(n!=0) { r=n%10; sum=sum+r; n=n/10; } printf("\nSum of Digits : %d",sum); getch(); }

OUTPUT: Enter n value 478 Sum of Digits : 19

PROGRAM NO 2: A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.

D KOTESWARARAO, 42

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<stdio.h> #include<conio.h> void main() { int n,i,f0=0,f1=1,f2; clrscr(); printf("Enter n value "); scanf("%d",&n); printf("\nFibonacci Sequence up to %d terms :\n\n\n",n); printf("%4d%4d",f0,f1); for(i=3;i<=n;i++) { f2=f0+f1; f0=f1; f1=f2; printf("%4d",f2); } getch(); } OUTPUT: Enter n value 15 Fibonacci Sequence up to 15 terms :
D KOTESWARARAO, 43 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

PROGRAM NO 3: Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user. #include<stdio.h> #include<conio.h> void main() { int n,i,j,c=0; clrscr(); printf("Enter n value "); scanf("%d",&n); printf("\nPrime Numbers between 1 to %d \n\n\n",n); for(i=2;i<=n;i++) { c=0; for(j=2;j<=i/2;j++) { if(i%j==0)
D KOTESWARARAO, 44 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ c++; break; } } if(c==0) printf("%4d",i); } getch(); }

OUTPUT: Enter n value 100

Prime Numbers between 1 to 100

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

D KOTESWARARAO, 45

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 46

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-2

D KOTESWARARAO, 47

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 4:Write a C program to calculate the following Sum:


Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! #include<conio.h> #include<stdio.h>
D KOTESWARARAO, 48 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<math.h> void main() { float x,term,sum=1; int i, j; clrscr(); printf("Enter x value\n"); scanf("%f",&x); for(j=1,i=2;i<=10;i+=2,j++) { term=term*(x*x)/(i*(i-1)); sum=sum+term*pow(-1,j); } printf("\nSum of series : %f",sum); getch(); }

OUTPUT: Enter x value 3

Sum of series : 1.000000

D KOTESWARARAO, 49

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 5: Write a C program to find the roots of a quadratic equation.


#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d,x1,x2,xr1,xr2,xi1,xi2; clrscr(); printf("enter a,b and c values"); scanf("%d%d%d",&a,&b,&c); d=b*b-4*a*c; if(d>0) { x1=(-b+sqrt(d))/2*a; x2=(-b-sqrt(d))/2*a; printf("roots are positive %d %d",x1,x2);
D KOTESWARARAO, 50 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} else if(d==0) { x1=x2=-b/2*a; printf(" roots are equals %d %d",x1,x2); } else { xr1=b/2*a; xr2=-b/2*a; xi1=sqrt(abs(d))/2*a; xi2=-sqrt(abs(d))/2*a; printf("roots are imaginary %d %d %d %d",xr1,xr2,xi1,xi2); } getch(); } OUTPUT: enter a,b and c values 1 -5 6 roots are positive 3 2

D KOTESWARARAO, 51

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-3

D KOTESWARARAO, 52

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 6: Write a C program to find the factorial of a given integer using non
recursive #include<stdio.h>
D KOTESWARARAO, 53 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value "); scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); } long int fact(long int n) { long int f=1; while(n!=0) { f=f*n; n--; } return f; }

OUTPUT:
D KOTESWARARAO, 54 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Enter n value 5

Factorial of 5 is 120

PROGRAM NO 7: Write a C program to find the factorial of a given integer using


recursive

#include<stdio.h> #include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value "); scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); }
D KOTESWARARAO, 55 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


long int fact(long int n) { if(n==0) return 1; else return (n*fact(n-1)); }

OUTPUT: Enter n value 6

Factorial of 6 is 720

PROGRAM NO 8: Write a C program to find GCD of Two Numbers using non-recursive


function

#include<conio.h> #include<stdio.h>
D KOTESWARARAO, 56 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


int gcd(int,int); void main() { int m,n; clrscr(); printf("Enter any Two Numbers"); scanf("%d%d",&m,&n); printf("GCD of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b) { int r; r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; }

D KOTESWARARAO, 57

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


OUTPUT: Enter any Two Numbers 136 48 GCD of the given two numbers 8

PROGRAM NO 9: Write a C program to find the GCD of two integers using recursive
functions

#include<conio.h> #include<stdio.h> int gcd(int,int); void main() { int m,n; clrscr(); printf("enter any two numbers"); scanf("%d%d",&m,&n); printf("gcd of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b)
D KOTESWARARAO, 58 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ int r; r=a%b; if(r==0) return b; r=gcd(b,r); }

OUTPUT: enter any two numbers 34 21 gcd of the given two numbers 1

PROGRAM NO 10: Write a C program to solve Towers of Hanoi problem

#include<stdio.h> #include<conio.h> void towers(int,char,char,char);


D KOTESWARARAO, 59 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void main() { int n; clrscr(); printf("enter n value"); scanf("%d",&n); towers(n,'A','B','C'); } void towers(int n,char frompeg,char topeg,char auxpeg) { if(n==1) { printf("\n%s%c%s%c","move disk 1 from peg ",frompeg," to peg ",topeg); return; } towers(n-1,frompeg,auxpeg,topeg); printf("\n%s%d%s%c%s%c","move disk ",n," from peg ",frompeg," to peg ",topeg); towers(n-1,auxpeg,topeg,frompeg); }

OUTPUT: enter n value4

D KOTESWARARAO, 60

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B move disk 3 from peg A to peg C move disk 1 from peg B to peg A move disk 2 from peg B to peg C move disk 1 from peg A to peg C move disk 4 from peg A to peg B move disk 1 from peg C to peg B move disk 2 from peg C to peg A move disk 1 from peg B to peg A move disk 3 from peg C to peg B move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B

D KOTESWARARAO, 61

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-4
D KOTESWARARAO, 62 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 63

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 11: The total distance travelled by vehicle in t seconds is given by


distance = ut+1/2at2 where u and a are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of u and a. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of u and a. #include <stdio.h> #include <math.h> void main() { int tim_intrval, counter,time; float accl, distance=0, velos; clrscr(); printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========>"); printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
D KOTESWARARAO, 64 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


scanf("%d",&tim_intrval);

for(counter = 1; counter <= tim_intrval; counter++) { printf("\n\t\t\tAT T%d TIME(sec) : ",counter); scanf("%d",&time); printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time); scanf("%f",&velos); printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time); scanf("%f",&accl); distance += (velos*time + (accl*pow(time,2))/2); }

printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance); getch(); } OUTPUT: <===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========> NO OF TIME INTERVALS : 3

AT T1 TIME(sec) : 45 VELOCITY AT 45 sec (m/sec) : 100


D KOTESWARARAO, 65 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


ACCLERATION AT 45 sec (m/sec^2): 25

AT T2 TIME(sec) : 50 VELOCITY AT 50 sec (m/sec) : 150 ACCLERATION AT 50 sec (m/sec^2): 50

AT T3 TIME(sec) : 60 VELOCITY AT 60 sec (m/sec) : 300 ACCLERATION AT 60 sec (m/sec^2): 70

TOTAL DISTANCE TRAVELLED BY VEHICLE IN 3 INTERVALS OF TIME : 243812.5000 00

PROGRAM NO 12: Write a C program, which takes two integer operands and one
operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)

D KOTESWARARAO, 66

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<stdio.h> #include<conio.h> void main() { int a,b; char ch; clrscr(); printf("enter a and b values"); scanf("%d%d",&a,&b); printf("enter the any operator(+,-,*,/)"); fflush(stdin); scanf("%c",&ch); switch(ch) { case '+': printf("Addition of two numbers : %d",a+b); break; case '-': printf("Subtraction of two numbers : %d",a-b); break; case '*': printf("Multiplication of two numbers : %d",a*b); break; case '/': printf("Division of two numbers :%f",(float)a/b); break; default: printf("invalid choice");
D KOTESWARARAO, 67 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} getch(); }

OUTPUT: enter a and b values15 3 enter the any operator(+,-,*,/)* Multiplication of two numbers : 45

D KOTESWARARAO, 68

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-5
D KOTESWARARAO, 69 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 70

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 13: Write a C program to find both the larges and smallest number in a
list of integers. #include<stdio.h> #include<conio.h> void main() { int a[20],i,n,max,min; clrscr(); printf("Enter n values "); scanf("%d",&n); printf("\nEnter %d elements ",n); for(i=0;i<n;i++) { printf("\nEnter %d position element : ",i); scanf("%d",&a[i]); }

D KOTESWARARAO, 71

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


max=min=a[0]; for(i=1;i<n;i++) { if(max<a[i]) max=a[i]; else if(min>a[i]) min=a[i]; } printf("\n Maximum element is :%d",max); printf("\n Minimum element is :%d",min); getch(); }

OUTPUT: Enter n values 6

Enter 6 elements Enter 0 position element : 23 Enter 1 position element : 67 Enter 2 position element : 45 Enter 3 position element : 34 Enter 4 position element : 13 Enter 5 position element : 87
D KOTESWARARAO, 72 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Maximum element is :87 Minimum element is :13

D KOTESWARARAO, 73

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 14: Write a Program to do Matrix Addition using Function
#include<stdio.h> #include<conio.h> void add(int [10][10],int [10][10],int,int); int i,j; void main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; clrscr(); printf("enter first matrix order\n"); scanf("%d%d",&r1,&c1); printf("enter second matrix order\n"); scanf("%d%d",&r2,&c2); printf("enter First Matrix elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&a[i][j]); printf("enter Second Matrix elements\n"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&b[i][j]); if(r1==r2&&c1==c2) {
D KOTESWARARAO, 74 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("Addition of two matrix is\n"); add(a,b,r1,c1); } else printf("\nMatrix addition is not possible"); getch(); } void add(int a[10][10],int b[10][10],int r,int c) { for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%4d",a[i][j]+b[i][j]); } printf("\n"); } }

OUTPUT: enter first matrix order 23 enter second matrix order


D KOTESWARARAO, 75 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


23 enter First Matrix elements 123 654 enter Second Matrix elements 678 325 Addition of two matrix is 7 9 11 9 7 9

PROGRAM NO 15: Write a program to perform matrix multiplication

D KOTESWARARAO, 76

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

void read1(int **,int,int); void print(int**,int,int); void mult(int**,int**,int**,int,int,int); int i,j,k; void main() { int **a,**b,**c,r1,c1,r2,c2; clrscr(); printf("Enter First Matrix Order\n"); scanf("%d%d",&r1,&c1); printf("Enter Second Matrix Order\n"); scanf("%d%d",&r2,&c2); a=(int**)malloc(r1*c1*2); b=(int**)malloc(r2*c2*2); c=(int**)malloc(r1*c2*2); printf("Enter First Matrix Elements\n"); read1(a,r1,c1); printf("Enter Second Matrix Elements\n"); read1(b,r2,c2); mult(a,b,c,r1,c1,c2);
D KOTESWARARAO, 77 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("First Matrix\n"); print(a,r1,c1); printf("Second Matrix\n"); print(b,r2,c2); printf("Result\n"); print(c,r1,c2); getch(); }

void read1(int **x,int r,int c) { for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",(*(x+i)+j)); }

void print(int **x,int r,int c) { for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%4d",*(*(x+i)+j)); printf("\n");


D KOTESWARARAO, 78 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} }

void mult(int **x,int **y,int **z,int r1,int c1,int c2) { for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { *(*(z+i)+j)=0; for(k=0;k<c1;k++) *(*(z+i)+j)=*(*(z+i)+j)+(*(*(x+i)+k))*(*(*(y+k)+j)); } } }

OUTPUT:

Enter First Matrix Order 22 Enter Second Matrix Order 22 Enter First Matrix Elements
D KOTESWARARAO, 79 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


22 22 Enter Second Matrix Elements 22 22 First Matrix 2 2 2 2 Second Matrix 2 2 2 2 Result 8 8 8 8

D KOTESWARARAO, 80

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 81

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-6
D KOTESWARARAO, 82 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 83

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 16: Write a C program that uses functions to perform the following
operations: i) To insert a sub-string in to given main string from a given position. #include<stdio.h> #include<conio.h> void insert(char[100],char[50],int); void main() { char s[100],ss[50]; int p; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",ss); printf("enter the position\n"); scanf("%d",&p); insert(s,ss,p); getch(); } void insert(char s[100],char ss[50],int p) { int i,j,k; char str[100]; for(i=0,j=0;i<p;i++,j++) str[j]=s[i]; for(i=0;ss[i]!='\0';i++,j++) str[j]=ss[i]; for(i=p;s[i]!='\0';i++,j++) str[j]=s[i];
D KOTESWARARAO, 84 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


str[j]='\0'; strcpy(s,str); printf("\nAfter insert main string is %s",s); }

OUTPUT:
enter the main string i you enter the sub string love enter the position 2 After insert main string is i love you ii) To delete n Characters from a given position in a given string. #include<stdio.h> #include<conio.h> void delet(char[100],int,int); void main() { char s[100]; int p,n; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the position\n"); scanf("%d",&p); printf("enter how many character we want delete\n"); scanf("%d",&n);
D KOTESWARARAO, 85 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


delet(s,p,n); getch(); } void delet(char s[100],int p,int n) { int i,j; for(i=p,j=p+n;s[i]!='\0';i++,j++) s[i]=s[j]; s[i]='\0'; printf("After delete main string is %s",s); }

OUTPUT:
enter the main string koteswararao enter the position 7 enter how many character we want delete 2 After delete main string is koteswarao

PROGRAM NO 17: Write a C program to determine if the given string is a palindrome or


not
D KOTESWARARAO, 86 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<conio.h> #include<stdio.h> #include<string.h> void main() { char str1[20],str2[20]; int d; clrscr(); printf("Enter string\n"); scanf("%s",str1); strcpy(str2,str1); strrev(str2); if(!strcmp(str1,str2)) printf("\n String is palindrome "); else printf("\n String is not palindrome "); getch(); }

OUTPUT:

Enter string MADAM


D KOTESWARARAO, 87 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

String is palindrome

D KOTESWARARAO, 88

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-7
D KOTESWARARAO, 89 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 90

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 18: Write a C program that displays the position or index in the string S
where the string T begins, or 1 if S doesnt contain T. #include<stdio.h> #include<conio.h> main() { char s[100],t[100],i,j,k; clrscr(); printf("enter the string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",t); for(i=0;i<strlen(s);i++) { for(j=0,k=i;j<strlen(t);j++,k++) { if(s[k]==t[j]) continue; else break; } if(j==strlen(t)) { printf("string is found at %d",index+1); exit(0); } } printf("sub string not found in string"); getch(); }
D KOTESWARARAO, 91 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


OUTPUT: enter the string COMPUTER PROGRAMMING AND DATA STRUCTURES enter the sub string DATA string is found at 26

PROGRAM NO 19: Write a C program to count the lines, words and characters in a given
text. #include<stdio.h> #include<conio.h> void main() { char text[81],ch; int nwords=0,nlines=0,nchars=0,i; clrscr(); printf("enter text\n"); while(1) { i=0; while((ch=getchar())!='\n') { text[i]=ch; nchars++;
D KOTESWARARAO, 92 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(ch==32) nwords++; i++; } nwords++; nlines++; text[i]='\0'; if(text[0]=='\0') break; } nwords--; nlines--; printf("\n no of characters : %d",nchars); printf("\n no of words printf("\n no of lines getch(); } : %d",nwords); : %d",nlines);

OUTPUT: enter text


D KOTESWARARAO, 93 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


KING IS GREAT WELCOME TO VVIT

no of characters : 28 no of words no of lines :6 :2

D KOTESWARARAO, 94

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 95

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-8

D KOTESWARARAO, 96

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 97

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 20: Write a C program to generate Pascals triangle.
#include<stdio.h> #include<conio.h> void main() { int i,j,n,k; static int p[10][10]; clrscr(); printf("\n Pascals Triangle size n="); scanf("%d",&n); printf("\n Pascal's triangle:\n"); for(i=0;i<n;i++) { for(k=0;k<=30-i*2;k++) printf(" "); for(j=0;j<=i;j++) { if(j==0||i==j) p[i][j]=1; else p[i][j]=p[i-1][j-1]+p[i-1][j]; printf("%4d",p[i][j]);

D KOTESWARARAO, 98

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} printf("\n"); } getch(); } OUTPUT:

Pascals Triangle size n=5

Pascal's triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

PROGRAM NO 21: Write a C program to construct a pyramid of numbers.


#include<stdio.h> #include<conio.h> void main() { int i,j,k,n; clrscr(); printf("Enter any number\n");
D KOTESWARARAO, 99 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<20-2*i;k++) printf(" "); for(j=1;j<=i;j++) printf("%4d",i); printf("\n"); } getch(); }

OUTPUT: Enter any number 6 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6

D KOTESWARARAO, 100

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-9

D KOTESWARARAO, 101

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 102

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 22: Write a C program to read in two numbers, x and n, and then compute
the sum of this geometric progression: 1+x+x2+x3+.+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125. Print x, n, the sum Perform error checking. For example, the formula does not make sense for negative exponents if n is less than 0. Have your program print an error message if n<0,> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int x,n,sum=1,i; clrscr(); printf("enter x and n values\n"); scanf("%d%d",&x,&n); while(n<0) { printf("Exponent value is negative please enter x and n values"); scanf("%d%d",&x,&n); } for(i=1;i<=n;i++) sum=sum+pow(x,i); printf("Sum=%d",sum); } getch(); } OUTPUT: enter x and n values 35 Sum=364

D KOTESWARARAO, 103

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 104

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-10

D KOTESWARARAO, 105

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 23: 2s complement of a number is obtained by scanning it from right to


left and complementing all the bits after the first appearance of a 1. Thus 2s complement of 11100 is 00100. Write a C program to find the 2s complement of a binary number.
D KOTESWARARAO, 106 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

#include<stdio.h> #include<conio.h> void main() { char b[100]; int i,l; clrscr(); printf("enter the binary number\n"); scanf("%s",b); for(i=0;b[i]!='\0';i++) if(b[i]=='0'||b[i]=='1') continue; else { printf("the given number is not a binary number"); exit(0); } l=strlen(b); for(i=l-1;i>=0;i--) if(b[i]=='0') continue; else break; for(i=i-1;i>=0;i--) if(b[i]=='0') b[i]='1'; else b[i]='0'; printf(" The 2's complement is %s",b); getch(); } OUTPUT: enter the binary number10111010 The 2's complement is 01000110

PROGRAM NO 24: Write a C program to convert a Roman numeral to its decimal


equivalent.
D KOTESWARARAO, 107 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int *a,len,i,j,k; char *rom; clrscr(); printf("Enter the Roman Numeral:"); scanf("%s",rom); len=strlen(rom); for(i=0;i<len;i++) { if(rom[i]=='I') a[i]=1; else if(rom[i]=='V') a[i]=5; else if(rom[i]=='X') a[i]=10; else if(rom[i]=='L') a[i]=50; else if(rom[i]=='C') a[i]=100; else if(rom[i]=='D') a[i]=500; else if(rom[i]=='M') a[i]=1000; else { printf("\nInvalid Value"); getch(); exit(0); } } k=a[len-1]; for(i=len-1;i>0;i--)
D KOTESWARARAO, 108 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ if(a[i]>a[i-1]) k=k-a[i-1]; else if(a[i]==a[i-1] || a[i]<a[i-1]) k=k+a[i-1]; } printf("\nIts Decimal Equivalent is:"); printf("%d",k); getch(); } OUTPUT: Enter the Roman Numeral:XII Its Decimal Equivalent is:12

D KOTESWARARAO, 109

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 110

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-11

D KOTESWARARAO, 111

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 25: Write a C program that uses functions to perform the following
operations: i) Reading a complex number ii) Writing a complex number iii) Addition of two complex numbers iv) Multiplication of two complex numbers
D KOTESWARARAO, 112 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


(Note: represent complex number using a structure.) #include<stdio.h> #include<conio.h> #include<math.h> typedef struct { float real,image; }complex;

void print(complex); complex add(complex,complex); complex sub(complex,complex); complex mul(complex,complex); complex div(complex,complex);

void main() { complex a,b,c; int ch; clrscr(); printf("enter first complex number\n"); scanf("%f%f",&a.real,&a.image);

D KOTESWARARAO, 113

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("enter second complex number\n"); scanf("%f%f",&b.real,&b.image); do { printf("\n 1.addition\n 2.subtraction\n 3.multiplication"); printf("\n 4.division\n 5.exit\n"); printf("enter your choice"); scanf("%d",&ch); printf("\nFirst Complex Number"); print(a); printf("Second Complex Number"); print(b); switch(ch) { case 1: printf("Addition of Two Complex Numbers"); c=add(a,b); break; case 2: printf("Subtraction of Two Complex Numbers"); c=sub(a,b); break; case 3: printf("Multiplication of Two Complex Numbers"); c=mul(a,b); break;
D KOTESWARARAO, 114 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case 4: printf("Division of Two Complex Numbers"); c=div(a,b); break; case 5: exit(0); } print(c); }while(ch<=5); getch(); }

void print(complex x) { char sign='+'; if(x.image<0) { sign='-'; x.image=abs(x.image); } printf(" %0.2f%ci%0.2f\n",x.real,sign,x.image); }

complex add(complex x,complex y) {


D KOTESWARARAO, 115 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


complex temp; temp.real=x.real+y.real; temp.image=x.image+y.image; return temp; }

complex sub(complex x,complex y) { complex temp; temp.real=x.real-y.real; temp.image=x.image-y.image; return temp; }

complex mul(complex x,complex y) { complex temp; temp.real=x.real*y.real-x.image*y.image; temp.image=x.real*y.image+x.image*y.real; return temp; }

complex div(complex x,complex y)


D KOTESWARARAO, 116 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ complex temp; temp.real=(x.real*y.real+x.image*y.image)/(x.real*x.real+x.image*x.image); temp.image=(x.real*y.image+x.image*y.real)/(x.real*x.real+x.image*x.image); return temp; }

OUTPUT: enter first complex number 34 enter second complex number 45

1.addition 2.subtraction 3.multiplication 4.division 5.exit enter your choice1

First Complex Number 3.00+i4.00 Second Complex Number 4.00+i5.00 Addition of Two Complex Numbers 7.00+i9.00
D KOTESWARARAO, 117 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

1.addition 2.subtraction 3.multiplication 4.division 5.exit enter your choice5

D KOTESWARARAO, 118

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-12

D KOTESWARARAO, 119

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 120

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 26: Write a C program which copies one file to another.
#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch; fp1=fopen("first.txt","r"); fp2=fopen("second.txt","w"); if(fp1==NULL) { printf("first.txt file unable to open"); exit(0); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }

D KOTESWARARAO, 121

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 27: Write a C program to reverse the first n characters in a file.


(Note: The file name and n are specified on the command line.)*/

#include <stdio.h> #include <conio.h> #include <string.h> #include <process.h>

void main(int argc, char *argv[])


D KOTESWARARAO, 122 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ char a[15]; char s[20]; char n; int k; int j=0; int i; int len; FILE *fp;

if(argc!=3) { puts("Improper number of arguments."); exit(0); } fp = fopen(argv[1],"r"); if(fp == NULL) { puts("File cannot be opened."); exit(0); }

k=*argv[2]-48; n = fread(a,1,k,fp); a[n]='\0';


D KOTESWARARAO, 123 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


len=strlen(a); for(i=len-1;i>=0;i--) { s[j]=a[i]; printf("%c",s[j]); j=j+1; } s[j+1]='\0'; getch(); }

WEEK-13
D KOTESWARARAO, 124 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 125

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 28: Write a C program that uses functions to perform the following
operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; typedef struct node *NODEPTR; NODEPTR p; NODEPTR getnode(); void freenode(NODEPTR p); void ins_start(NODEPTR p,int x); void ins_end(NODEPTR p,int x); void ins_middle(NODEPTR p,int x); int del_start(NODEPTR p); int del_middle(NODEPTR p); int del_end(NODEPTR p); void print(NODEPTR p); void main() { int ch,x;
D KOTESWARARAO, 126 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


p=getnode(); p->next=NULL; clrscr(); do { printf("\n1.ins_start\n2.ins_middle\n3.ins_end"); printf("\n4.del_start\n5.del_middle\n6.del_end"); printf("\n7.print\n8.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); ins_start(p,x); break; case 2: printf("enter inserted element"); scanf("%d",&x); ins_middle(p,x); break; case 3: printf("enter inserted element"); scanf("%d",&x); ins_end(p,x); break; case 4: x=del_start(p); printf("deleted element from list %d",x); break; case 5: x=del_middle(p); printf("deleted element from list %d",x); break; case 6: x=del_end(p); printf("deleted element from list %d",x); break; case 7: print(p); break; case 8: exit(0); } }while(ch<=8); getch(); } NODEPTR getnode() {
D KOTESWARARAO, 127 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); } void freenode(NODEPTR p) { free(p); } void ins_start(NODEPTR p,int x) { NODEPTR q; q=getnode(); q->info=x; if(p->next==NULL) q->next=NULL; else q->next=p->next; p->next=q; } void ins_middle(NODEPTR p,int x) { NODEPTR q; int s,i=1; printf("enter insert position"); scanf("%d",&s); while(p!=NULL && i<=s) { p=p->next; i++; } if(p->next==NULL) { printf("INSERTION IS NOT POSSIBLE"); exit(0); } q=getnode(); q->info=x; q->next=p->next; p->next=q;
D KOTESWARARAO, 128 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

} void ins_end(NODEPTR p,int x) { NODEPTR q; q=getnode(); q->info=x; if(p->next==NULL) q->next=NULL; else { while(p->next!=NULL) p=p->next; q->next=NULL; } p->next=q; } int del_start(NODEPTR p) { int x; NODEPTR q; if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; p->next=q->next; freenode(q); return x; } int del_middle(NODEPTR p) { int x,i=1,n; NODEPTR q; printf("enter position of the deleted element"); scanf("%d",&n); while(p!=NULL && i<n) { p=p->next; i++;
D KOTESWARARAO, 129 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; p->next=q->next; freenode(q); return x; } int del_end(NODEPTR p) { int x; NODEPTR q; if(p->next==NULL) { printf("deletion is not possible"); exit(0); } while((p->next)->next!=NULL) p=p->next; q=p->next; x=q->info; p->next=NULL; freenode(q); return x; } void print(NODEPTR p) { NODEPTR q; q=p; if(p->next==NULL) { printf("LIST IS EMPTY"); exit(0); } q=q->next; while(q!=NULL)
D KOTESWARARAO, 130 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("%2d->",q->info); q=q->next; } printf("NULL"); } OUTPUT: 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element12 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element23 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element45 1.ins_start 2.ins_middle
D KOTESWARARAO, 131 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->23->12->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice3 enter inserted element78 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->23->12->78->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice5 enter position of the deleted element2 deleted element from list 23 1.ins_start 2.ins_middle 3.ins_end
D KOTESWARARAO, 132 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->12->78->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice8

D KOTESWARARAO, 133

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-14
D KOTESWARARAO, 134 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 135

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 29: Write a C program that uses functions to perform the following
operations on doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways #include<stdio.h> #include<conio.h>

struct node { int info; struct node *next,*prev; };

typedef struct node *NODEPTR;

NODEPTR p;
D KOTESWARARAO, 136 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

NODEPTR getnode(); void freenode(NODEPTR p); void ins_start(NODEPTR p,int x); void ins_end(NODEPTR p,int x); void ins_middle(NODEPTR p,int x); int del_start(NODEPTR p); int del_middle(NODEPTR p); int del_end(NODEPTR p); void printforward(NODEPTR p); void printbackward(NODEPTR p); void main() { int ch,x; p=getnode(); p->next=NULL; p->prev=NULL; clrscr(); do { printf("\n1.ins_start\n2.ins_middle\n3.ins_end"); printf("\n4.del_start\n5.del_middle\n6.del_end"); printf("\n7.print_forward\n8.print_backward\n9.exit\n");
D KOTESWARARAO, 137 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); ins_start(p,x); break; case 2: printf("enter inserted element"); scanf("%d",&x); ins_middle(p,x); break; case 3: printf("enter inserted element"); scanf("%d",&x); ins_end(p,x); break; case 4: x=del_start(p); printf("deleted element from list %d",x); break; case 5: x=del_middle(p); printf("deleted element from list %d",x); break; case 6: x=del_end(p);
D KOTESWARARAO, 138 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("deleted element from list %d",x); break; case 7: printforward(p); break; case 8: printbackward(p); break; case 9: exit(0); } }while(ch<=8); getch(); }

NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); }

void freenode(NODEPTR p) { free(p); }


D KOTESWARARAO, 139 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

void ins_start(NODEPTR p,int x) { NODEPTR q,r; q=getnode(); q->info=x; if(p->next==NULL) { q->next=NULL; q->prev=p; p->prev=NULL; } else { r=p->next; r->prev=q; q->next=r; q->prev=p; } p->next=q; } void ins_middle(NODEPTR p,int x) {
D KOTESWARARAO, 140 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


NODEPTR q,r; int s,i=1; printf("enter insert position"); scanf("%d",&s); while(p!=NULL && i<=s) { p=p->next; i++; } if(p->next==NULL) { printf("INSERTION IS NOT POSSIBLE"); exit(0); } q=getnode(); q->info=x; r=p->next; q->next=r; q->prev=p; r->prev=q; p->next=q;

}
D KOTESWARARAO, 141 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void ins_end(NODEPTR p,int x) { NODEPTR q,r; q=getnode(); q->info=x; if(p->next==NULL) { q->prev=p; q->next=NULL; } else { while(p->next!=NULL) p=p->next; q->prev=p; } q->next=NULL; p->next=q; }

int del_start(NODEPTR p) { int x;


D KOTESWARARAO, 142 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


NODEPTR q,r; if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; r=q->next; r->prev=p; p->next=r; freenode(q); return x; } int del_middle(NODEPTR p) { int x,i=1,n; NODEPTR q,r; printf("enter position of the deleted element"); scanf("%d",&n); while(p!=NULL && i<n) { p=p->next;
D KOTESWARARAO, 143 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


i++; } if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; r=q->next; r->prev=p; p->next=r; freenode(q); return x; } int del_end(NODEPTR p) { int x; NODEPTR q; if(p->next==NULL) { printf("deletion is not possible"); exit(0);
D KOTESWARARAO, 144 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} while((p->next)->next!=NULL) p=p->next; q=p->next; x=q->info; p->next=NULL; freenode(q); return x; }

void printforward(NODEPTR p) { NODEPTR q; q=p->next; if(p->next==NULL) { printf("LIST IS EMPTY"); exit(0); } while(q!=NULL) { printf("%2d->",q->info); q=q->next;
D KOTESWARARAO, 145 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} printf("NULL"); } void printbackward(NODEPTR p) { NODEPTR q; while(p->next!=NULL) p=p->next; q=p; if(p->prev==NULL) { printf("LIST IS EMPTY"); exit(0); } while(q->prev!=NULL) { printf("%2d->",q->info); q=q->prev; } printf("NULL"); } OUTPUT:

D KOTESWARARAO, 146

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice1 enter inserted element20

1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice1 enter inserted element20

D KOTESWARARAO, 147

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice1 enter inserted element30 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice7 30->20->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice3 enter inserted element50 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward
D KOTESWARARAO, 148 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


9.exit enter your choice7 30->20->50->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice8 50->20->30->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice6 deleted element from list 50 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice7 30->20->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward
D KOTESWARARAO, 149 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


8.print_backward 9.exit enter your choice9

D KOTESWARARAO, 150

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-15

D KOTESWARARAO, 151

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 30: Write C programs that implement stack (its operations) using
i) Arrays

D KOTESWARARAO, 152

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<stdio.h> #include<conio.h> #define MAXSIZE 100 struct stack { int top; int item[MAXSIZE]; }; struct stack *s; void push(struct stack *s,int x); int pop(struct stack *s); void print(struct stack *s); int empty(struct stack *s); void main() { int op,x; s->top=-1; clrscr(); do { printf(" \n 1.push\n 2.pop \n 3.print \n 4.exit\n"); printf("enter your option"); scanf("%d",&op);
D KOTESWARARAO, 153 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


switch(op) { case 1: printf(" enter element\n"); scanf("%d",&x); push(s,x); break; case 2: if(!empty(s)) { x=pop(s); printf("\nthe retrived elemenet from stack is %d",x); } else printf("stack is underflow"); break; case 3: print(s); break; case 4: exit(0); } } while(op<=4); getch();
D KOTESWARARAO, 154 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

void push(struct stack *s,int n) { if(s->top==MAXSIZE-1) printf("stack is full"); else { s->top++; s->item[s->top]=n; } } int pop(struct stack *s) { return s->item[s->top--]; } void print(struct stack *s) { int i; if(!empty(s)) { printf("stack elements"); for(i=s->top;i>=0;i--)
D KOTESWARARAO, 155 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("\n ----\n"); printf("| %d |",s->item[i]); } printf("\n ----"); } else printf("stack is empty"); } int empty(struct stack *s) { if(s->top==-1) return 1; else return 0; } OUTPUT:

1.push 2.pop 3.print 4.exit enter your option1


D KOTESWARARAO, 156 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


enter element 15

1.push 2.pop 3.print 4.exit enter your option1 enter element 35

1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 35 | ---| 15 | ---1.push
D KOTESWARARAO, 157 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


2.pop 3.print 4.exit enter your option1 enter element 50

1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 50 | ---| 35 | ---| 15 | ---1.push 2.pop 3.print
D KOTESWARARAO, 158 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


4.exit enter your option2

the retrived elemenet from stack is 50 1.push 2.pop 3.print 4.exit enter your option4

PROGRAM NO 31: Write C programs that implement stack (its operations) using
ii) Pointers #include<stdio.h> #include<conio.h>

struct node { int info; struct node *next; };

typedef struct node *NODEPTR;

struct stack
D KOTESWARARAO, 159 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ NODEPTR top; };

typedef struct stack *stack;

stack s; NODEPTR getnode(); void freenode(NODEPTR p); void push(stack s,int x); int pop(stack s); int empty(stack s); void print(stack s);

void main() { int ch,x; s->top=NULL; clrscr(); do { printf("\n1.PUSH\n2.POP\n3.PRINT\n4.EXIT\n"); printf("enter your choice");
D KOTESWARARAO, 160 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); push(s,x); break; case 2: x=pop(s); printf("poped element from list %d",x); break; case 3: print(s); break; case 4: exit(0); } }while(ch<=4); getch(); }

NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p);


D KOTESWARARAO, 161 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

void freenode(NODEPTR p) { free(p); } int empty(stack s) { if(s->top==NULL) return 1; else return 0; } void push(stack s,int x) { NODEPTR q; q=getnode(); q->info=x; if(s->top==NULL) { q->next=NULL; s->top=q; }
D KOTESWARARAO, 162 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


else { q->next=s->top; s->top=q; }

int pop(stack s) { int x; NODEPTR q; if(empty(s)) { printf("stack is underflow"); exit(0); } q=s->top; x=q->info; s->top=q->next; freenode(q); return x;
D KOTESWARARAO, 163 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

void print(stack s) { NODEPTR q; q=s->top; if(s->top==NULL) { printf("STACK IS EMPTY"); exit(0); } printf(" ----\n"); printf("|NULL|\n"); while(q!=NULL) { printf(" ----\n"); printf("| %d |\n",q->info); q=q->next; } printf(" ----"); } OUTPUT:

D KOTESWARARAO, 164

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element20

1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element30

1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice3 ---|NULL| ---| 30 |


D KOTESWARARAO, 165 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


---| 20 | ---1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice2 poped element from list 30 1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice3 ---|NULL| ---| 20 | ---1.PUSH 2.POP 3.PRINT 4.EXIT
D KOTESWARARAO, 166 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


enter your choice4

D KOTESWARARAO, 167

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-16
D KOTESWARARAO, 168 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 169

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 32: Write C programs that implement Queue (its operations) using
i) Arrays #include<stdio.h> #include<conio.h> #define MAX 100 struct queue { int front,rear; int item[MAX]; }; struct queue *q; void insert(struct queue *q,int x); int delete(struct queue *q);
D KOTESWARARAO, 170 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void print(struct queue *q); int empty(struct queue *q); void main() { int ch,x; clrscr(); q->front=0; q->rear=-1; do { printf("\n 1.insert \n 2.remove \n 3.print \n 4.exit \n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter element\n"); scanf("%d",&x); insert(q,x); break; case 2: if(!empty(q)) { x=delete(q); printf("deleted element from queue is %d",x);
D KOTESWARARAO, 171 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} else { printf("queue is underflow\n"); getch(); } break; case 3: if(!empty(q)) print(q); else printf("queue is empty"); break; case 4: exit(0); default : printf("invalid choice"); getch();

} }while(ch<=4); } int empty(struct queue *q) { if(q->rear<q->front) return 1;


D KOTESWARARAO, 172 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


else return 0; }

void insert(struct queue *q,int x) { if(q->rear==MAX-1) { printf("queue is overflow\n"); getch(); } else { q->rear++; q->item[q->rear]=x; } }

int delete(struct queue *q) { return q->item[q->front++]; }

D KOTESWARARAO, 173

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void print (struct queue *q) { int i; for(i=q->front;i<=q->rear;i++) printf("%4d",q->item[i]); } OUTPUT:

1.insert 2.remove 3.print 4.exit enter your choice1 enter element 35

1.insert 2.remove 3.print 4.exit enter your choice1 enter element 67


D KOTESWARARAO, 174 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

1.insert 2.remove 3.print 4.exit enter your choice1 enter element 12

1.insert 2.remove 3.print 4.exit enter your choice3 35 67 12 1.insert 2.remove 3.print 4.exit enter your choice2 deleted element from queue is 35 1.insert 2.remove
D KOTESWARARAO, 175 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


3.print 4.exit enter your choice3 67 12 1.insert 2.remove 3.print 4.exit enter your choice1 enter element 45

1.insert 2.remove 3.print 4.exit enter your choice3 67 12 45 1.insert 2.remove 3.print 4.exit enter your choice4
D KOTESWARARAO, 176 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 33: Write C programs that implement Queue (its operations) using
ii) Pointers #include<stdio.h> #include<conio.h>

struct node { int info; struct node *next; };

typedef struct node *NODEPTR;

struct queue { NODEPTR front,rear; };

struct queue *pq;

NODEPTR getnode(); void freenode(NODEPTR p); void insert(struct queue *pq,int x); int delete(struct queue *pq);
D KOTESWARARAO, 177 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


int empty(struct queue *pq); void print(struct queue *pq);

void main() { int ch,x; pq->rear=pq->front=NULL; clrscr(); do { printf("\n1.insert\n2.delete\n3.print\n4.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); insert(pq,x); break; case 2: x=delete(pq); printf("deleted element from list %d",x); break; case 3: print(pq);
D KOTESWARARAO, 178 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


break; case 4: exit(0); } }while(ch<=4); getch(); }

NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); }

void freenode(NODEPTR p) { free(p); }

void insert(struct queue *pq,int x) { NODEPTR p;


D KOTESWARARAO, 179 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

p=getnode(); p->info=x; p->next=NULL; if(pq->rear==NULL) pq->front=p; else (pq->rear)->next=p; pq->rear=p; }

int delete(struct queue *pq) { int x; NODEPTR p; if(empty(pq)) { printf("queue is underflow"); exit(0); } p=pq->front; x=p->info; pq->front=p->next;
D KOTESWARARAO, 180 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(pq->front==NULL) pq->rear=NULL; freenode(p); return x; } int empty(struct queue *pq) { if(pq->front==NULL) return 1; else return 0; }

void print(struct queue *pq) { NODEPTR q; q=pq->front; if(pq->front==NULL) { printf("QUEUE IS EMPTY"); exit(0); }
D KOTESWARARAO, 181 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

while(q!=NULL) { printf("%2d->",q->info); q=q->next; } printf("NULL"); }

OUTPUT:

1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element10

1.insert 2.delete 3.print 4.exit enter your choice1


D KOTESWARARAO, 182 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


enter inserted element20 1.insert 2.delete 3.print 4.exit enter your choice3 10->20->NULL 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element45 1.insert 2.delete 3.print 4.exit enter your choice3 10->20->45->NULL 1.insert 2.delete 3.print 4.exit enter your choice2 deleted element from list 10 1.insert 2.delete 3.print 4.exit enter your choice3 20->45->NULL 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element50 1.insert 2.delete
D KOTESWARARAO, 183 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


3.print 4.exit enter your choice3 20->45->50->NULL 1.insert 2.delete 3.print 4.exit enter your choice4

D KOTESWARARAO, 184

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-17
D KOTESWARARAO, 185 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 186

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 34: Write a C program that uses Stack operations to perform the
following: i) Converting infix expression into postfix expression #include<stdio.h> #include<conio.h> #include<ctype.h>

#define MAX 100

struct stack { int top; int item[MAX]; };

D KOTESWARARAO, 187

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


struct stack *s; char postfix[MAX]; void push(struct stack *s,char a); int pop(struct stack *s); int prec(char op); void intopost(char infix[MAX]);

void main() { char infix[MAX]; s->top=-1; clrscr(); printf("enter the infix expression"); gets(infix); intopost(infix); printf("the postfix expression is %s",postfix); getch(); }

void intopost(char infix[MAX]) { int i=0,j=0; while(infix[i]!='\0')


D KOTESWARARAO, 188 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ if(isalpha(infix[i])) postfix[j++]=infix[i]; else { switch(infix[i]) { case '$': case '*': case '/': case '+': case '-': while((s->top!=-1) &&prec(s->item[s->top])>=prec(infix[i])) postfix[j++]=pop(s); push(s,infix[i]); break; case '(':push(s,'('); break; case ')': while(s->item[s->top]!='(') postfix[j++]=pop(s); pop(s); break;
D KOTESWARARAO, 189 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


default : printf("invalid expression"); getch(); exit(0); } } i++; } while(s->top!=-1) postfix[j++]=pop(s); }

int prec(char op) { switch(op) { case '+': case '-': return (10); case '*': case '/': return (20); case '$': return (30);
D KOTESWARARAO, 190 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case '(': return (2); } }

void push(struct stack *s,char a) { if(s->top==MAX-1) printf("stack is full"); else s->item[++s->top]=a; } int pop(struct stack *s) { if(s->top==-1) { printf("stack is underflow"); exit(0); } else return (s->item[s->top--]); } OUTPUT:
D KOTESWARARAO, 191 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


enter the infix expressionA*(B+C)-D the postfix expression is ABC+*D-

PROGRAM NO 35: Write a C program that uses Stack operations to perform the
following: ii) Evaluating the postfix expression #include<stdio.h> #include<conio.h> #include<math.h> #define MAXCOLS 100 struct stack { int top; int item[MAXCOLS]; }; double eval(char[]); double pop(struct stack *s); void push(struct stack *s,double n); int empty(struct stack *s); int isdigit(char);
D KOTESWARARAO, 192 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


double oper(int,double,double); void main() { char expr[MAXCOLS]; int position=0; clrscr(); printf("enter postfix expression\n"); while((expr[position++]=getchar())!='\n'); expr[--position]='\0'; printf("\nthe original postfix expression ] %s\n",expr); printf("the result of postfix expression %f",eval(expr)); getch(); }

double eval(char expr[]) { int c,position; double opnd1,opnd2,value; struct stack *opndstk; opndstk=(struct stack *)malloc(sizeof(struct stack)); opndstk->top=-1; for(position=0;(c=expr[position])!='\0';position++) if(isdigit(c))
D KOTESWARARAO, 193 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


push(opndstk,(double)(c-'0')); else { opnd2=pop(opndstk); opnd1=pop(opndstk); value=oper(c,opnd1,opnd2); push(opndstk,value); } return (pop(opndstk)); }

isdigit(char symb) { return (symb>='0' && symb<='9'); }

double oper(int symb,double op1,double op2) { switch(symb) { case '+': return(op1+op2); case '-': return(op1-op2); case '*': return(op1*op2);
D KOTESWARARAO, 194 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case '/': return(op1/op2); case '$': return(pow(op1,op2)); default: printf("\nillegal operation "); exit(0); } }

void push(struct stack *s,double n) { if(s->top==MAXCOLS-1) printf("stack is overflow"); else { s->top++; s->item[s->top]=n; } }

double pop(struct stack *s) { if(!empty(s)) return (s->item[s->top--]); else


D KOTESWARARAO, 195 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("etack is underflow"); exit(0); } }

int empty(struct stack *s) { if(s->top==-1) return 1; else return 0; } OUTPUT: enter postfix expression 345+*

the original postfix expression 345+* the result of postfix expression 27.000000

D KOTESWARARAO, 196

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-18

D KOTESWARARAO, 197

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 198

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 36: Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers ii) Traversing the above binary tree in preorder, inorder and postorder. #include<stdio.h> #include <stdlib.h> #include<conio.h> struct treenode { int ele; struct treenode *l_child, *r_child; }; struct treenode *insert_node(struct treenode *t,int a); void TraverseInorder(struct treenode *t); void TraversePreorder(struct treenode *t); void TraversePostorder(struct treenode *t); /*main function*/ void main() { struct treenode *root_node = NULL; int num,value; int choice; clrscr(); printf("----------------------------------------------------\n"); printf("\t\t\tMENU\n"); printf("-----------------------------------------------------\n"); printf("[1] Create a Binary Tree and Use Inorder Traversal\n"); printf("[2] Create a Binary Tree and Use Preorder Traversal\n"); printf("[3] Create a Binary Tree and Use Postorder Traversal\n"); printf("-----------------------------------------------------\n"); printf("Enter your choice:"); scanf("%d",&choice); if(choice>0 & choice<=3)
D KOTESWARARAO, 199 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("\nEnter the number of nodes:"); scanf("%d",&num); while(num-- > 0) { printf("\n\nEnter the data value:"); scanf("%d",&value); root_node = insert_node(root_node,value); } switch(choice) { case 1: printf("\n\nBinary tree using Inorder Traversal : "); TraverseInorder(root_node); getch(); break; case 2: printf("\n\nBinary tree using Preorder Traversal : "); TraversePreorder(root_node); getch(); break; case 3: printf("\n\nBinary tree using Postorder Traversal : "); TraversePostorder(root_node); getch(); break; default: printf("Invalid Choice"); break; } } } /*end main*/ /* Function to create a Binary Tree of integers data */ struct treenode *insert_node(struct treenode *t,int a) { struct treenode *temp_node1,*temp_node2;
D KOTESWARARAO, 200 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(t == NULL) { t = (struct treenode *) malloc(sizeof(struct treenode)); if(t == NULL) { printf("Value cannot be allocated.\n"); exit(0); } t->ele = a; t->l_child=t->r_child=NULL; } else { temp_node1 = t; while(temp_node1 != NULL) { temp_node2 = temp_node1; if( temp_node1 ->ele > a) temp_node1 = temp_node1->l_child; else temp_node1 = temp_node1->r_child; } if( temp_node2->ele > a) { temp_node2->l_child = (struct treenode*)malloc(sizeof(struct treenode)); temp_node2 = temp_node2->l_child; if(temp_node2 == NULL) { printf("Value cannot be allocated.\n"); exit(0); } temp_node2->ele = a; temp_node2->l_child=temp_node2->r_child = NULL; } else { temp_node2->r_child = (struct treenode*)malloc(sizeof(struct treenode)); temp_node2 = temp_node2->r_child; if(temp_node2 == NULL) { printf("Value cannot be allocated.\n");
D KOTESWARARAO, 201 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


exit(0); } temp_node2->ele = a; temp_node2->l_child=temp_node2->r_child = NULL; } } return(t); } /* Function for Traversing the binary tree in inorder. */ void TraverseInorder(struct treenode *t) { if(t != NULL) { TraverseInorder(t->l_child); printf("%d\t",t->ele); TraverseInorder(t->r_child); } } /* Function for Traversing the binary tree in preorder. */ void TraversePreorder(struct treenode *t) { if(t != NULL) { printf("%d\t",t->ele); TraversePreorder(t->l_child); TraversePreorder(t->r_child); } } /* Function for Traversing the binary tree in postorder. */ void TraversePostorder(struct treenode *t) { if(t != NULL) { TraversePostorder(t->l_child); TraversePostorder(t->r_child); printf("%d\t",t->ele); } } OUTPUT: ---------------------------------------------------D KOTESWARARAO, 202 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


MENU ----------------------------------------------------[1] Create a Binary Tree and Use Inorder Traversal [2] Create a Binary Tree and Use Preorder Traversal [3] Create a Binary Tree and Use Postorder Traversal ----------------------------------------------------Enter your choice:1 Enter the number of nodes:4 Enter the data value:12 Enter the data value:2 Enter the data value:45 Enter the data value:23 Binary tree using Inorder Traversal : 2 12 23 45

D KOTESWARARAO, 203

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 204

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-19

D KOTESWARARAO, 205

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 206

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 37: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers : i) Linear search #include<stdio.h> #include<conio.h> int lsearch(int[],int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=lsearch(a,n,s); if(key!=-1) printf("Element found at %d position ",key+1); else
D KOTESWARARAO, 207 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("Element not found "); getch(); } int lsearch(int x[10],int n,int s) { int i; for(i=0;i<n;i++) if(s==x[i]) return i; return -1; } OUTPUT: Enter the n value 5 Enter 5 elements 23 56 73 21 34 Enter search element73 Element found at 3 position

PROGRAM NO 38: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers : ii) Binary search /* Binary Search using Non-recursive */ #include<stdio.h> #include<conio.h> int bsearch(int[],int,int); void main()
D KOTESWARARAO, 208 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("enter %d elements in sorted order",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter search element"); scanf("%d",&s); key=bsearch(a,n,s); if(key!=-1) printf("element found at %d position ",key+1); else printf("element not found "); getch(); }

int bsearch(int x[10],int n,int s) { int low,high,mid; low=0; high=n-1;


D KOTESWARARAO, 209 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


while(low<=high) { mid=(low+high)/2; if(s==x[mid]) return mid; else if(s<x[mid]) high=mid-1; else low=mid+1; } return -1; }

OUTPUT:

Enter the n value 5 enter 5 elements in sorted order 34 45 68 69 76 enter search element69 element found at 4 position

D KOTESWARARAO, 210

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 39: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers :

D KOTESWARARAO, 211

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


ii) Binary search /* Binary Search using recursive */

#include<stdio.h> #include<conio.h> int brsearch(int[],int,int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements in sorting order ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=bsearch(a,s,0,n-1); if(key!=-1) printf("Element found at %d position ",key+1); else printf("Element not found "); getch();
D KOTESWARARAO, 212 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

int bsearch(int x[10],int s,int low,int high) { int mid; if(low>high) return -1; mid=(low+high)/2; if(s==x[mid]) return mid; else if(s<x[mid]) bsearch(x,s,low,mid-1); else bsearch(x,s,mid+1,high);

OUTPUT:

Enter the n value 6 Enter 6 elements in sorting order 12 23 34 45 56 67 Enter search element52 Element not found
D KOTESWARARAO, 213 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 214

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 215

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-20

D KOTESWARARAO, 216

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 40: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: i) Bubble sort #include<stdio.h> #include<conio.h> void bubble(int[],int); void main() {
D KOTESWARARAO, 217 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


int a[10],i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); bubble(a,n); getch(); }

void bubble(int x[10],int n) { int temp,flag=1,i,j; for(i=0;i<n-1&&flag==1;i++) { flag=0; for(j=0;j<n-i-1;j++) { if(x[j]>x[j+1]) { flag=1; temp=x[j];
D KOTESWARARAO, 218 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


x[j]=x[j+1]; x[j+1]=temp; } } } printf("sorted elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); }

OUTPUT:

Enter the n value 7 Enter 7 elements 34 89 76 21 10 54 41 sorted elements 10 21 34 41 54 76 89

D KOTESWARARAO, 219

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 41: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: ii) Quick sort
D KOTESWARARAO, 220 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

#include<stdio.h> #include<conio.h> void quick(int a[100],int lb,int ub); int partition(int a[100],int lb,int ub); void main() { int x[100],i,n; clrscr(); printf("Enter how many elements we want"); scanf("%d",&n); printf("Enter %d elements\n ",n); for(i=0;i<n;i++) scanf("%d",&x[i]); clrscr(); printf("Given elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); quick(x,0,n-1); printf("\n Sorted Elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); getch();
D KOTESWARARAO, 221 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

void quick(int a[100],int lb,int ub) { int i,j; if(lb>=ub) return; j=partition(a,lb,ub); quick(a,lb,j-1); quick(a,j+1,ub); } int partition(int a[100],int lb,int ub) { int s,down,temp,up; s=a[lb]; up=ub; down=lb; while(down<up) { while(a[down]<=s && down<ub) down++; while(a[up]>s) up--;
D KOTESWARARAO, 222 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(down<up) { temp=a[down]; a[down]=a[up]; a[up]=temp; } } a[lb]=a[up]; a[up]=s; return up; }

OUTPUT:

Given elements 12 34 45 10 5 Sorted Elements 5 10 12 34 45

D KOTESWARARAO, 223

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-21

D KOTESWARARAO, 224

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 225

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


PROGRAM NO 42: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: i) Insertion sort #include<stdio.h> #include<conio.h> void insertsort(int[],int); void main() { int a[10],i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); insertsort(a,n); printf("Sorted elements \n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); } void insertsort(int x[10],int n) { int i,k,y;
D KOTESWARARAO, 226 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(k=1;k<n;k++) { y=x[k]; for(i=k-1;i>=0&&y<x[i];i--) x[i+1]=x[i]; x[i+1]=y; } } OUTPUT: Enter the n value 5 Enter 5 elements 67 43 48 26 13 Sorted elements 13 26 43 48 67

PROGRAM NO 43: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: ii) Merge sort #include<stdio.h> #include<conio.h> void mergesort(int,int); void merge(int,int,int); int a[10],b[10]; void main() {
D KOTESWARARAO, 227 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


int i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(0,n-1); printf("\n Sorted Elements\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); }

void mergesort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; mergesort(low,mid); mergesort(mid+1,high); merge(low,mid,high);
D KOTESWARARAO, 228 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} } void merge(int low,int mid,int high) { int h,i,j,k; h=low;i=low;j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; j++; } i++; } if(h>mid) for(k=j;k<=high;k++,i++) b[i]=a[k];
D KOTESWARARAO, 229 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


else for(k=h;k<=mid;k++,i++) b[i]=a[k]; for(k=low;k<=high;k++) a[k]=b[k]; }

OUTPUT: Enter the n value 6 Enter 6 elements 45 23 87 65 34 12 Sorted Elements 12 23 34 45 65 87

D KOTESWARARAO, 230

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 231

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-22

D KOTESWARARAO, 232

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 44: Write C programs to implement the Lagrange interpolation


#include<stdio.h> #include<conio.h> #define MaxN 90 void main() { float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0; int i, j, n; clrscr(); printf("Enter the value of n: \n"); scanf("%d", &n); printf("Enter the values of x and y: \n"); for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated: "); scanf("%f", &x); for (i=0; i<=n; i++)
D KOTESWARARAO, 233 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ numerator=1; denominator=1; for (j=0; j<=n; j++) if(j!=i) { numerator *= x-arr_x[j]; denominator *= arr_x[i]-arr_x[j]; } y+=(numerator/denominator)*arr_y[i]; } printf("When x=%4.1f y=%7.1f\n",x,y); getch(); } OUTPUT: Enter the value of n: 4 Enter the values of x and y: 23 45 65 37 89 Enter the value of x at which value of y is to be calculated: 3 When x= 3.0 y= 7.0 PROGRAM NO 45: Write C programs to implement the Newton- Gregory forward interpolation. #include<stdio.h> #include<conio.h> #define MaxN 100 #define Order_of_diff 4 void main () { float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1]; int i,j,n,k; clrscr(); printf("Enter the value of n \n"); scanf("%d",&n); printf("Enter the values of x and y");
D KOTESWARARAO, 234 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated"); scanf("%f", &x); h=arr_x[1]-arr_x[0]; for(i=0; i<=n-1; i++) diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/ for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/ for(i=0; i<=n-j; i++) diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1]; i=0; while(!(arr_x[i]>x)) /* Finding x0 */ i++; i--; p=(x-arr_x[i])/h; y=arr_y[i]; for (k=1; k<=Order_of_diff; k++) { numerator *=p-k+1; denominator *=k; y +=(numerator/denominator)*diff_table[i][k]; } printf("When x=%6.1f, y=%6.2f\n",x, y); getch(); } OUTPUT: Enter the value of n 4 Enter the values of x and y 12 23 34 45 56 Enter the value of x at which value of y is to be calculated2 When x= 2.0, y= 3.00
D KOTESWARARAO, 235 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 236

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-23
D KOTESWARARAO, 237 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 238

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 46: Write C programs to implement the linear regression and polynomial
regression algorithms. #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n);
D KOTESWARARAO, 239 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("Enter the values of x and y:\n"); for(i=0;i<n;i++) scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i<n;i++) sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy); printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1) { reg_coff_xy=corr_coff*(sx/sy); printf("\nThe value of linear regression coefficient is %f",reg_coff_xy); } else if(strcmp(type_coff,"y on x")==1) { reg_coff_yx=corr_coff*(sy/sx); printf("\nThe value of linear regression coefficient is %f",reg_coff_yx); } else printf("\nEnter the correct type of regression coefficient."); getch(); } float mean(float *a, int n) { float sum=0, i=0; for(i=0;i<n;i++) sum=sum+a[i]; sum=sum/n; return (sum); } void deviation(float *a, float mean, int n, float *d, float *s) { float sum=0,t=0; int i=0;
D KOTESWARARAO, 240 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(i=0;i<n;i++) { d[i]=a[i]-mean; t=d[i]*d[i]; sum=sum+t; } sum=sum/n; *s=sqrt(sum); } OUTPUT: Enter the value of n: 3 Enter the values of x and y: 24 56 14 Enter the type of regression coefficient as 'x on y' or 'y on x': y on x The value of linear regression coefficient is 1.750000

WEEK-24
D KOTESWARARAO, 241 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 242

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

PROGRAM NO 47: Write C program to implement Simpson method.


#include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2, e3; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of sub-intervals: "); scanf("%f%f%d", &x0, &xn, &n);
D KOTESWARARAO, 243 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); e3=4*eval(postfix, x0+h); s=log(e1)+log(e2)+log(e3); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } printf("The value of integral is %6.3f\n",(h/3)*s); return(0); } /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; } /*Removing the operands from a stack. */ float pop()
D KOTESWARARAO, 244 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ float item; if(top==-1) { printf("\n\tThe stack is empty\n\t"); getch(); } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top1++; stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("\n\tThe stack1 is empty\n\t"); getch(); } item=stack1[top1]; top1--; return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k;
D KOTESWARARAO, 245 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) {
D KOTESWARARAO, 246 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


ch=pop1(); postfix[j]=ch; j++; } postfix[j]='\0'; } /*Determining the priority of elements that are placed inside the stack. */ int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); } return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Invalid expression"); } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) {
D KOTESWARARAO, 247 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); }
D KOTESWARARAO, 248 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


return 0; } OUTPUT: Enter an expression: 3+6*5 Enter x0, xn and number of sub-intervals: 1 5 3 The value of integral is 88.000

PROGRAM NO 48: Write C program to implement Trapezoidal method.


#include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2;
D KOTESWARARAO, 249 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of subintervals"); scanf("%f%f%d", &x0, &xn, &n); h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); s=log(e1)+log(e2); for (i=1;i<=n-1;i++) s+=2*log(eval(postfix,x0+i*h)); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn); for (i=1;i<=n-1;i++) s+=2*eval(postfix,x0+i*h); } printf("Value of the integral is %6.3f\n",(h/2)*s); return(0); } /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++;
D KOTESWARARAO, 250 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


stack[top]=item; } return; } /*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("\n\tThe stack is empty\n\t"); getch(); } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top1++; stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("\n\tThe stack1 is empty\n\t"); getch(); } item=stack1[top1]; top1--;
D KOTESWARARAO, 251 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); /*Assigning the popped element into the postfix array. */ postfix[j]=ch;
D KOTESWARARAO, 252 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='\0'; } int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); break; } return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8);
D KOTESWARARAO, 253 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case '\0':return (0); default: printf("Invalid expression"); break; } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); break; } push(k); } i++; }
D KOTESWARARAO, 254 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); } return 0; } OUTPUT: Enter an expression: 3+6*5 Enter x0, xn and number of sub-intervals: 1 5 3 The value of integral is 88.000

D KOTESWARARAO, 255

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

C - Language Viva Questions

D KOTESWARARAO, 256

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 257

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

C - Language Viva
1) What is the computer? Ans: A computer is an electronic device, processing the data according to set of instructions, and produce the results, may be store in the system.

2) What is the CPU? Ans: CPU stands for Central Processing Unit, Responsible for controlling all activities of the computer system.

3) What are the input devices? Ans: keyboard, mouse, joysticketc

4) What are the output devices? Ans: printer, monitor, speaker etc.

5) What is the ALU? Ans: ALU stands for Arithmetic and Logic unit. To perform the Arithmetic and Logic operations.

6) What is the CU? D KOTESWARARAO, 258 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: CU stands for Control unit, to control the all functionality of the system.

7) What is the program? Ans: program is a set of instruction.

8) What is the algorithm? Ans: an algorithm is a step by step procedure to solve the problem.

9) What is the Flowchart? Ans: A graphical representation of the algorithm is called a flowchart.

10) What are the program development steps? Ans: a) problem definition b) Problem design c) Coding d) Debugging e) Maintenance

11) What is the C? Ans: C is a general purpose programming language.

12) Who developed C language? Ans: Dennis Ritchie in 1972.

13) What are the features of C language? Ans: a) C is a robust Associate Professor, CSE Dept.

D KOTESWARARAO, 259

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


b) C is a middle level language c) C is a Structured Programming language d) C is a portable e) C is easily extendable f) C program is efficient and fast g) C have only 32 keywords.

14) What are the applications of C language? Ans: System Software like Compiles, interpreter, spread sheets, database ets Application software and Games.

15) What is the identifier? Ans: identifiers refers to name of the variables, functions, and arrays. These are user defined names.

16) What is the keyword? Ans: keywords have special meaning that is define by the system.

17) What is the variable? Ans: variable is date name may be stores the data values, it refers to the memory location.

18) What are the rules to create the variables? Ans : a) Variable name must begin with a letter or underscore. b) Variable consists only letters, digits, underscore. c) The variable should not be a keyword. d) White spaces is not allowed. D KOTESWARARAO, 260 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

19) What is the Data Type? Ans: Data type specifies to the system which type of values to be stores and how much memory to be allocated to the variable.

20) What are the basic data types? Ans: int, char, float, double.

21) What is the size of the each basic data types? Ans: int 2 Bytes, char 1 Bytes, float 4 Bytes, double 8 Bytes.

22) What is the constant? Ans: Constant is fixed value does not change during the execution of the program.

23) What is the integer constant? Ans: An integer constant refers to a sequence of digits.

24) What is the character constant? Ans: A single character constant contains a single character enclosed within pair of single quote marks. 25) What is the real constant? A number followed by a decimal point and fractional part.

26) What is the string constant? Ans: A string constant is a sequence of character enclosed in double quotes.

27) What is the expression? D KOTESWARARAO, 261 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: An expression is a combination of variables, constants, and operators arranged as per the syntax of the language.

28) What is the operator? Ans: Operator is symbol that tells to the computer to perform certain operation on the operand.

29) What is the operand? Ans: Operand is variable or constant.

30) What is the sizeof()? Ans: The operator is used to find out the how many bytes allocated for variable, constant or data type.

32) What is the pre increment and post increment? Ans: preincrement means first perform he increment after then perform the operation. postincrement means first perform he operations after then perform the increment.

33) What is the pre decrement and post decrement? Ans: predecrement means first perform he decrement after then perform the operation. postdecrement means first perform he operations after then perform the decrement.

34) What is the header file? Ans: header file contains several built in functions.

35) What is the stdio.h? D KOTESWARARAO, 262 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: Standard input output header file. It provides standard input and output functions.

36) What is the conio.h? Ans: console input and output header file.

37) What is the standard input and output function? Ans: scanf(), printf().

38) What meant by formatted input and output functions? Ans: formatted input output functions are read or write (print) any type of data values.

39) What are the formatted input and output functions? Ans: scanf(), printf().

40) What are unformatted input and output functions? Ans: unformatted input output functions are used to read or write only characters. Ex: getchar(),getch(),gets(),putchar(),puts().. etc.

41) What is the Block? Ans: a block is a group of statement into a single unit.

42) What are the conditional statements? Ans: a) simple if, b) if-else, c) nested if else d) else if ladder e) switch case.

43) What is the difference between else-if ladder and switch case? D KOTESWARARAO, 263 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: switch case reduce the complexity of the program.

44) What is the loop? Ans: loop is a block of statements which are repeatedly executed for certain number of times.

45) How many loops available in C language? Ans: 3, while, do-while, for.

46) What is the difference between while and do-while? Ans: while is the pretest condition, do-while is the posttest condition. In do-while the statements will be executed at least once.

47) What is the break statement? Ans: exit from the block.

48) What is continue statement? Ans: when execute the continue statement skip the next statement and control goes to next iteration.

49) What is the need of the exit? Ans: exit from the program.

50) What is the array? Ans: Array is a group of related data items that share a common name. (or) array is a collection of homogenous elemements.

D KOTESWARARAO, 264

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


51) What is the difference between array and ordinary variable? Ans: ordinary variable can store one value at a time but array variable can stores many values at a time.

52) What is the index? ans: index a variable. it can be used to access the element in the array.

53) What is the range of the index in general? Ans: 0 to n-1.

54) What is the string? Ans: a sequence of characters is called string.

54) What is the function? Ans: function is a self contained block, it perform the particular task.

55) What are the advantages of the function? Ans: a) reduce the length of the program. b) easy to understand c) easy to debug d) easy to maintain e) reusability.

56) What is meant by actual arguments?? Ans: which are the arguments passed in the calling function is called actual arguments.

D KOTESWARARAO, 265

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


57) What is meant formal arguments? Ans: which are the arguments declared in the called function is called actual arguments.

58) What is the prototype? Ans: function prototypes tells the compiler the number and type of arguments that are to be passed to function and the type of value that is to be returned by the function.

59) How to call the function? Ans: using function name.

60) What meant by storage classes? Ans: storage classes specifies the scope and life time of the variable.

61) How many storage classes available in C language? ans: 4, auto, extern, static, register.

62) What is the difference between auto and extern? Ans: auto means the variables access within that function, extern means the variable access throughout the program.

63) What is the recursive function? Ans: A function call itself is called a recursive function.

64) What is the Pointer? Ans: Pointer is a variable which stores the address of the another variable.

D KOTESWARARAO, 266

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


65) What are the advantages of the Pointer? Ans: a) pointers are more efficient in handling the data tables. b) pointers reduce the length and complexity of the program c) Increasing the execution speed of the program d) dynamic memory allocations e) easily handling the data structures

66) How to allocate dynamic memory for the variable? Ans: malloc()

67) How to delete dynamic memory for the variable? Ans: free()

68) What is the Structure? Ans: A Structure is a collection of one or more variables, possibly of different data types.

69) What is the difference between array and structure? Ans: Array is a collection of similar data items but structure is a collection of different data items.

70) How to access the structure variable? Ans: structure variable can be accessing using . or -> operator.

71) What is the union? Ans: Union is a collection of one or more variables, possibly of different data types.

D KOTESWARARAO, 267

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


72) What is the difference between structure and union? Ans: main difference between structure and union is allocation of the memory. In union all members of a union use the same memory location but the structure using different locations.

73) What is the preprocessor? Ans: preprocessor is program to process the data before pass to the compiler.

74) What is the need of the preprocessor? Ans: fast execution of the program.

76) What is the file? Ans: A file is collection of records. The main purpose of the file is to store the data permanently.

77) Which function to be used to open the file? Ans: fopen()

78) What are the modes to be used in the files? Ans: r read, w write , a append.

79) What is the compiler? Ans: compiler is program it translates the source program into object program.

80) What interpreter? Ans: Interpreter is program it translates the source program into object program. D KOTESWARARAO, 268 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

81) What is the difference between compiler and interpreter? Ans: compiler translate the source program at a time but the interpreter translate line by line.

82) What is the bit? Ans: bit is a binary digit. Example: o and 1.

83) What is the byte? Ans: group of 8 bits is called Byte.

84) What is the operating system? Ans: The system software that controls the computer.

85) What is the software? Ans: Software is the set of programs and applications that tell the computer what to do.

86) What is the hardware? Ans: Hardware is physical component of a system, basically anything that you can touch with your fingers.

87) What is the data? Ans: Information processed by a computer.

88) What is a Stack? Ans: Stack is a collection of ordered items in which insertion and deletion can be performed at one end is called as top of the stack. D KOTESWARARAO, 269 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

89) What is a Queue? Ans: Queue is a collection of ordered items in which insertion can be performed at one end is called as rear and deletion can be performed other end is called as front.

90) What are the operations performed on Stacks? Ans: push and pop

91) what are the operations performed on Queue? Ans: insertion and Deletion.

92) What is Push()? Ans: Insert the element at the top of the stack

93) What is Pop()? Ans:Delete the top element at the top of the stack.

94) what are the applications of Stack? Ans: a) Infix to Postfix conversion b) Postfix Evaluation c) Paranthesis matching d) Function calls e)recurssion

95) What are the applciations of Queue? ans: simulation b) CPU scheduling c) Client/server systems d) page replacement algorithms.

D KOTESWARARAO, 270

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


96) What is a Data structures? Ans: Data structures specifies way of organizes the data in memory.

97) What ia an Abstract Data Type ? Ans: abstract data type is a mathematical model of data structures.

98) what is Linked list? Ans: Linked list is a collection of nodes each node having link with another node(s).

99) What is Single Linked list? Ans: Single linked list is a collection of nodes, each node having two fields one is info, another one is next field.

100) What is Double Linked List? Ans:Double linked list is a collection of nodes, each node having three fields one is info, another two are next and prev fields.

101) what is the drawback of Single Linked list? Ans: only forward accessing, it is not possible to accessing backward.

102) What is the difference between array and linked list? Ans: array is a static, linked list is a dynamic

103) what are the types of Data structures? Ans: 1.Linear Data Structures 2. Non-Linear Data Structures.

104) What is mean by searching? D KOTESWARARAO, 271 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: searchin means to find the element is present or not in the list

105) what is meant by sorting? Ans: sorting means arrange the elements in particular order.

106) What is Space complexity? Ans: It is defined as the amount of memory need to run to completion by an program. 107) what is Time complexity? Ans: It is defined as the amount of Computer time need to run to completion by an program. 108) what is Time complexity of Linear search? Ans: Best Case: O(1) Average Case: O(n) Worst Case: O(n)

109) what is Time complexity of Binary search? Ans: Best Case:O(1) Average Case:O(logn) Worst Case:O(logn)

110) what is Time complexity of Quick sort? Ans: Best Case:O(nlogn) Average Case:O(lnogn) Worst Case:O(n2)

111) what is Time complexity of Merge sort? D KOTESWARARAO, 272 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Ans: Best Case: O(lnogn) Average Case: O(lnogn) Worst Case: O(lnogn)

112) what is Time complexity of Bubble sort? Ans: O(n2)

113) what is Time Complexity of Insertion sort? Ans: O(n2)

114) what is Infix expression? Ans: The operator between two operands.

115) what is Postfix expression? Ans: The operator after two operands

116) what is Time complexy of selection sort?

117) What is Prefix expression? Ans: The operator precending two operands

DOS - acronym for Disk Operating System - used in IBM PCs.


RAM - acronym for Random-Access Memory

ROM - acronym for Read Only Memory; memory that can only be read from and not written to.
UPS - acronym for Uninterruptible Power Source

D KOTESWARARAO, 273

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Objectives: To make the student learn a programming language. To teach the student to write programs in C solve the problems To Introduce the student to simple linear and non linear data structures such as lists, stacks, queues, trees and graphs. Text Books 1. C programming and Data Structures, P. Padmanabham, Third Edition, BS Publications 2. Data Structures: A pseudo code approach with C, second edition R.F. Gilberg and B.A. Forouzan 3. Programming in C, P.Dey & M. Ghosh, Oxford Univ.Press. 4. C and Data Structures, E Balaguruswamy, TMH publications

D KOTESWARARAO, 274

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

FOR ANY QUERIES PLEASE CONTACT

D KOTESWARARAO
Phone No:

Associate Professor, Dept. of CSE, VVIT.

9441544277
Email

Id:d_koteswararao@yahoo.co.in

ThankQ
D KOTESWARARAO, 275 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

***************************

D KOTESWARARAO, 276

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

You might also like