Sie sind auf Seite 1von 258

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. week 1.a) 33 34 c) Prime Numers between 1 to N week 2. a) Evaluate the expression 39 b) 40 week 3.a) 43 b) 44 45 d) 46 GCD of Two numbers using Non-Recursive Factorial of Given Number using Non-Recursive Factorial of Given Number using Recursive Roots of Quadratic equations 35 NAME OF THE PROGRAM Sum of Digits of Given Number PAGE No.

b) Fibonacci Series

c)GCD of Two numbers using Non-Recursive

D KOTESWARARAO, 4

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


e) 47 week 4.a) 51 b) 53 week 5. a) 57 b) 59 c) 61 week 6. a) Insert sub-string in main String 67 b) Delete sub-string in main String 68 c) Given String is Palindrome or not 69 week 7 a) To Display portion of string if string in Main String 73 b)To count no. of characters, words and lines in a Text 74 week 8 a) 79 b) 80 week 9 83 Geometric Regression Pascals Tr iangle Matrix Multiplication Largest and Smallest number in a list of Integer Matrix Addition Total Distance Travelled by Vehicle Arithmetic Operations using Switch case Towers of Hanoi Problem

Pyrmid of Numbers

week 10 a) To Find 2s Complement of Given Number 87

D KOTESWARARAO, 5

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


b) To Convert Roman To decimal Number 88 week 11 Complex Numbers 93 week 12 a) To Copy one File into another File 99 b) 100 week 13 a) To Display contents of a file 103 b) To Merge two files into a third file 104 week 14 Single Linked List 107 week 15 a) 117 121 week 16 a) Queue implements using Arrays 129 b) Queue implements using Pointers 133 week 17 a) Infix to Postfix Conversion 141 b) Postfix Evaluation 144 week 18 a) Bubble sort 148 b) Selection sort 150
D KOTESWARARAO, 6 Associate Professor, CSE Dept.

To Reverse the first n Characters in a File

Stack implements using Arrays

b) Stack implements using Pointers

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


week 19 a) Linear search using non recursive 155 b) Linear search using recursive 156 c) Binary search using Non-Recursive 157 d) Binary search using Recursive 159 week 20 163 week 21 167 Quick Sort Merge Sort

week22 a) To implement the Lagrange Interpolation 171 b) To implement the Newton-Gregory Forward Interpolation 172 week 23 To implement the Linear Regression 177 week 24 a) To implement Simpson Method 181 b) To implement Tr apezoidal Method 187

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 Link Section


D KOTESWARARAO, 23 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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:

D KOTESWARARAO, 28

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


(a) (b) (c) (d) (e) if statement if-else statement nested if else else if ladder 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) {
D KOTESWARARAO, 30 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(expression2) { 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;
D KOTESWARARAO, 31 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Statement-2 }

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)
D KOTESWARARAO, 33 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Break; default: 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) {

D KOTESWARARAO, 35

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Statement-block;

c) do- while loop

do {

Statement-block;

} while(expression);

6) Break statement

break;

7) Continue statement

continue; D KOTESWARARAO, 36 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

8) goto statement

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;

}
D KOTESWARARAO, 37 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

8) Function prototype:

return_type function_name(argument_list);

9) Structure:

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

} structure_variables;

10) Union:

D KOTESWARARAO, 38

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


union tag_name { 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

a) 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;
D KOTESWARARAO, 41 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


clrscr(); printf("Enter n value "); scanf("%d",&n); 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

D KOTESWARARAO, 42

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


b) 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. #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:
D KOTESWARARAO, 43 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Enter n value 15 Fibonacci Sequence up to 15 terms : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

c) 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;

D KOTESWARARAO, 44

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(j=2;j<=i/2;j++) { if(i%j==0) { 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

a) Write a C program to calculate the following Sum:


D KOTESWARARAO, 48 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! #include<conio.h> #include<stdio.h> #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


D KOTESWARARAO, 49 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Sum of series : 1.000000

b) 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) {
D KOTESWARARAO, 50 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


x1=(-b+sqrt(d))/2*a; x2=(-b-sqrt(d))/2*a; printf("roots are positive %d %d",x1,x2); } 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

D KOTESWARARAO, 53

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


a) Write a C program to find the factorial of a given integer using non 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(); } long int fact(long int n) { long int f=1; while(n!=0) { f=f*n; n--; } return f; }
D KOTESWARARAO, 54 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

OUTPUT:

Enter n value 5

Factorial of 5 is 120

b) 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);
D KOTESWARARAO, 55 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); } 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

D KOTESWARARAO, 56

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


c) Write a C program to find GCD of Two Numbers using non-recursive function

#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) { int r; r=a%b; while(r!=0) { a=b; b=r; r=a%b;
D KOTESWARARAO, 57 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} return b; }

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

d) 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);
D KOTESWARARAO, 58 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf("gcd of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b) { 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

D KOTESWARARAO, 59

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


e) Write a C program to solve Towers of Hanoi problem

#include<stdio.h> #include<conio.h> void towers(int,char,char,char); 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);
D KOTESWARARAO, 60 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

OUTPUT: enter n value4

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

D KOTESWARARAO, 62

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-4

D KOTESWARARAO, 63

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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() {
D KOTESWARARAO, 64 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 : "); 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:


D KOTESWARARAO, 65 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


<===========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 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

D KOTESWARARAO, 66

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) 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) #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;
D KOTESWARARAO, 67 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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"); } 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

D KOTESWARARAO, 69

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-5

D KOTESWARARAO, 70

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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);
D KOTESWARARAO, 71 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(i=0;i<n;i++) { printf("\nEnter %d position element : ",i); scanf("%d",&a[i]); } 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


D KOTESWARARAO, 72 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

Maximum element is :87 Minimum element is :13

D KOTESWARARAO, 73

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) 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");
D KOTESWARARAO, 74 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&b[i][j]); if(r1==r2&&c1==c2) { 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"); } }
D KOTESWARARAO, 75 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

OUTPUT: enter first matrix order 23 enter second matrix order 23 enter First Matrix elements 123 654 enter Second Matrix elements 678 325 Addition of two matrix is 7 9 11 9 7 9

D KOTESWARARAO, 76

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write a program to perform matrix multiplication

#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);
D KOTESWARARAO, 77 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); 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) {


D KOTESWARARAO, 78 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

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:

D KOTESWARARAO, 79

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Enter First Matrix Order 22 Enter Second Matrix Order 22 Enter First Matrix Elements 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

D KOTESWARARAO, 82

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-6

D KOTESWARARAO, 83

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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(); }
D KOTESWARARAO, 84 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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]; 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 b) Write a C program that uses functions to perform the following operations: 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();
D KOTESWARARAO, 85 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); 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
D KOTESWARARAO, 86 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


2 After delete main string is koteswarao

c) Write a C program to determine if the given string is a palindrome or not #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();
D KOTESWARARAO, 87 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

OUTPUT:

Enter string MADAM

String is palindrome

D KOTESWARARAO, 88

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 89

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-7

D KOTESWARARAO, 90

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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;
D KOTESWARARAO, 91 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


else break; } if(j==strlen(t)) { printf("string is found at %d",index+1); exit(0); } } printf("sub string not found in string"); getch(); } OUTPUT: enter the string COMPUTER PROGRAMMING AND DATA STRUCTURES enter the sub string DATA string is found at 26

b) 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");
D KOTESWARARAO, 92 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


while(1) { i=0; while((ch=getchar())!='\n') { text[i]=ch; nchars++; 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 KOTESWARARAO, 93 Associate Professor, CSE Dept.

: %d",nwords); : %d",nlines);

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} OUTPUT: enter text 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

a) 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++) {
D KOTESWARARAO, 98 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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]); } 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 b) Write a C program to construct a pyramid of numbers. #include<stdio.h> #include<conio.h>


D KOTESWARARAO, 99 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void main() { int i,j,k,n; clrscr(); printf("Enter any number\n"); 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

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

D KOTESWARARAO, 106

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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. #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:
D KOTESWARARAO, 107 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


enter the binary number10111010 The 2's complement is 01000110

b) Write a C program to convert a Roman numeral to its decimal equivalent. #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
D KOTESWARARAO, 108 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("\nInvalid Value"); getch(); exit(0); } } k=a[len-1]; for(i=len-1;i>0;i--) { 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

D KOTESWARARAO, 112

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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 (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() {

D KOTESWARARAO, 113

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


complex a,b,c; int ch; clrscr(); printf("enter first complex number\n"); scanf("%f%f",&a.real,&a.image); 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");
D KOTESWARARAO, 114 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


c=sub(a,b); break; case 3: printf("Multiplication of Two Complex Numbers"); c=mul(a,b); break; 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); }


D KOTESWARARAO, 115 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


printf(" %0.2f%ci%0.2f\n",x.real,sign,x.image); }

complex add(complex x,complex y) { 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;


D KOTESWARARAO, 116 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

complex div(complex x,complex y) { 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


D KOTESWARARAO, 117 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

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

a) 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

b) 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>


D KOTESWARARAO, 122 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

void main(int argc, char *argv[]) { 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); }
D KOTESWARARAO, 123 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

k=*argv[2]-48; n = fread(a,1,k,fp); a[n]='\0'; 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(); }

D KOTESWARARAO, 124

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-13

D KOTESWARARAO, 125

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a c Programme to display the contents of a file


#include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp=fopen("sample.txt","r"); if(fp==NULL) { printf("first.txt file unable to open"); exit(0); } while((ch=getc(fp))!=EOF) putchar(ch); fclose(fp); getch(); }
D KOTESWARARAO, 126 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a c Programme to merge two files into a third file(i.e. the contents of the first file
followed by those of the second are put in the third file) #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2,*fp3; char ch; fp1=fopen("first.txt","r"); fp3=fopen("third.txt","w"); while((ch=getc(fp1))!=EOF) putc(ch,fp3); fclose(fp1); fclose(fp3); fp2=fopen("second.txt","r"); fp3=fopen("third.txt","a"); while((ch=getc(fp2))!=EOF) putc(ch,fp3); fclose(fp2); fclose(fp3); }
D KOTESWARARAO, 127 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 128

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-14

D KOTESWARARAO, 129

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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);
D KOTESWARARAO, 130 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; 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;
D KOTESWARARAO, 131 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case 7: print(p); break; case 8: exit(0); } }while(ch<=8); getch(); } NODEPTR getnode() { 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++;
D KOTESWARARAO, 132 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} if(p->next==NULL) { printf("INSERTION IS NOT POSSIBLE"); exit(0); } q=getnode(); q->info=x; q->next=p->next; p->next=q; } 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; }
D KOTESWARARAO, 133 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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++; } 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)
D KOTESWARARAO, 134 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ NODEPTR q; q=p; if(p->next==NULL) { printf("LIST IS EMPTY"); exit(0); } q=q->next; while(q!=NULL) { 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
D KOTESWARARAO, 135 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 choice1 enter inserted element45 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->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
D KOTESWARARAO, 136 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 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, 137

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 138

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-15

D KOTESWARARAO, 139

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C programs that implement stack (its operations) using i) Arrays

#include<stdio.h> #include<conio.h> #define MAXSIZE 100 struct stack {


D KOTESWARARAO, 140 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); switch(op) { case 1: printf(" enter element\n"); scanf("%d",&x); push(s,x);
D KOTESWARARAO, 141 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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(); }

void push(struct stack *s,int n) { if(s->top==MAXSIZE-1)


D KOTESWARARAO, 142 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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--) { printf("\n ----\n"); printf("| %d |",s->item[i]); } printf("\n ----");
D KOTESWARARAO, 143 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} 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 enter element 15

1.push 2.pop
D KOTESWARARAO, 144 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 2.pop 3.print 4.exit enter your option1 enter element
D KOTESWARARAO, 145 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


50

1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 50 | ---| 35 | ---| 15 | ---1.push 2.pop 3.print 4.exit enter your option2

the retrived elemenet from stack is 50 1.push


D KOTESWARARAO, 146 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


2.pop 3.print 4.exit enter your option4

b) 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 { NODEPTR top; };

D KOTESWARARAO, 147

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element");
D KOTESWARARAO, 148 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); }

void freenode(NODEPTR p) {
D KOTESWARARAO, 149 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; } else { q->next=s->top; s->top=q;
D KOTESWARARAO, 150 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

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; }

void print(stack s) {
D KOTESWARARAO, 151 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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:

1.PUSH 2.POP 3.PRINT 4.EXIT


D KOTESWARARAO, 152 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 | ---| 20 | ---1.PUSH
D KOTESWARARAO, 153 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 enter your choice4

D KOTESWARARAO, 154

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 155

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 157

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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); void print(struct queue *q); int empty(struct queue *q); void main() {
D KOTESWARARAO, 158 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); } else { printf("queue is underflow\n");
D KOTESWARARAO, 159 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; else return 0; }

D KOTESWARARAO, 160

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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++]; }

void print (struct queue *q) { int i; for(i=q->front;i<=q->rear;i++)


D KOTESWARARAO, 161 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

1.insert 2.remove 3.print


D KOTESWARARAO, 162 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 3.print 4.exit enter your choice3 67 12
D KOTESWARARAO, 163 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 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 b) Write C programs that implement Queue (its operations) using ii) Pointers #include<stdio.h> #include<conio.h>

D KOTESWARARAO, 164

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); int empty(struct queue *pq); void print(struct queue *pq);

void main()
D KOTESWARARAO, 165 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ 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); break; case 4: exit(0); } }while(ch<=4);
D KOTESWARARAO, 166 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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;

p=getnode(); p->info=x; p->next=NULL;


D KOTESWARARAO, 167 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; if(pq->front==NULL) pq->rear=NULL; freenode(p); return x;
D KOTESWARARAO, 168 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} 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); }

while(q!=NULL) { printf("%2d->",q->info);
D KOTESWARARAO, 169 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 enter inserted element20 1.insert 2.delete 3.print 4.exit
D KOTESWARARAO, 170 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 3.print 4.exit enter your choice3 20->45->50->NULL 1.insert 2.delete 3.print
D KOTESWARARAO, 171 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


4.exit enter your choice4

D KOTESWARARAO, 172

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 174

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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]; };

struct stack *s; char postfix[MAX]; void push(struct stack *s,char a);
D KOTESWARARAO, 175 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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') { if(isalpha(infix[i])) postfix[j++]=infix[i];


D KOTESWARARAO, 176 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; default : printf("invalid expression"); getch(); exit(0);
D KOTESWARARAO, 177 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} } 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); case '(': return (2); }
D KOTESWARARAO, 178 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


}

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: enter the infix expressionA*(B+C)-D the postfix expression is ABC+*D-

D KOTESWARARAO, 179

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) 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); double oper(int,double,double); void main() {

D KOTESWARARAO, 180

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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)) push(opndstk,(double)(c-'0')); else {
D KOTESWARARAO, 181 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); case '/': return(op1/op2); case '$': return(pow(op1,op2)); default: printf("\nillegal operation ");
D KOTESWARARAO, 182 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 { printf("etack is underflow"); exit(0);
D KOTESWARARAO, 183 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} }

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, 184

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-18

D KOTESWARARAO, 185

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


a) write a c program that implements 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() { 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++) {

D KOTESWARARAO, 186

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


flag=0; for(j=0;j<n-i-1;j++) { if(x[j]>x[j+1]) { flag=1; temp=x[j]; 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, 187 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) write a c program that implements the following sorting methods to sort a given list of integers in ascending order. ii) Selection sort
#include<stdio.h> #include<conio.h> void selection(int[],int);

D KOTESWARARAO, 188

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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]); selection(a,n); printf("sorted elements\n"); for(i=0;i<n;i++) printf("%4d",a[i]);

getch(); }

void selection(int x[10],int n) { int i,index,j,large; for(i=n-1;i>0;i--) { large=x[0]; index=0; D KOTESWARARAO, 189 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


for(j=1;j<=i;j++) { if(x[j]>large) { large=x[j]; index=j; } } x[index]=x[i]; x[i]=large; } }

OUTPUT: Enter the n value 5 Enter 5 elements 43 98 67 21 54 sorted elements 21 43 54 67 98

D KOTESWARARAO, 190

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 191

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 192

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-19

D KOTESWARARAO, 193

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C program to implement linear search using non recursive function.


#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);
D KOTESWARARAO, 194 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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 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
D KOTESWARARAO, 195 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


Element found at 3 position

b) Write C program to implement linear search using recursive function.


#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, 196

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) { if(n==-1) return -1; else if(x[n]==s) return n; else lsearch(x,n-1,s); } OUTPUT: Enter the n value 5 Enter 5 elements 23 56 73 21 34 Enter search element73 Element found at 3 position

c) Write C program to implement Binary Search using Non-recursive function


#include<stdio.h> #include<conio.h> int bsearch(int[],int,int);
D KOTESWARARAO, 197 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


void main() { 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;


D KOTESWARARAO, 198 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


high=n-1; 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, 199

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 200

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

d) Write C program to implement Binary Search using recursive function


#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, 201 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 element 45 Element found at 4 position
D KOTESWARARAO, 202 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 203

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-20

D KOTESWARARAO, 204

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Quick sort #include<stdio.h> #include<conio.h> void quick(int a[100],int lb,int ub);
D KOTESWARARAO, 205 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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(); }

void quick(int a[100],int lb,int ub) {


D KOTESWARARAO, 206 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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--; if(down<up) { temp=a[down]; a[down]=a[up];
D KOTESWARARAO, 207 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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, 208

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-21

D KOTESWARARAO, 209

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Merge sort #include<stdio.h>
D KOTESWARARAO, 210 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#include<conio.h> void mergesort(int,int); void merge(int,int,int); int a[10],b[10]; void main() { 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;


D KOTESWARARAO, 211 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


if(low<high) { mid=(low+high)/2; mergesort(low,mid); mergesort(mid+1,high); merge(low,mid,high); } } 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++;
D KOTESWARARAO, 212 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} i++; } if(h>mid) for(k=j;k<=high;k++,i++) b[i]=a[k]; 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, 213

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-22

D KOTESWARARAO, 214

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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;
D KOTESWARARAO, 215 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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++) { 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 b) Write C programs to implement the Newton- Gregory forward interpolation. #include<stdio.h> #include<conio.h> #define MaxN 100 #define Order_of_diff 4
D KOTESWARARAO, 216 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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"); 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:
D KOTESWARARAO, 217 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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, 218

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 219

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-23

D KOTESWARARAO, 220

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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);
D KOTESWARARAO, 221 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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); 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(); }
D KOTESWARARAO, 222 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; 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

D KOTESWARARAO, 223

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-24

D KOTESWARARAO, 224

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 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];
D KOTESWARARAO, 225 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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); 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)
D KOTESWARARAO, 226 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; 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() {
D KOTESWARARAO, 227 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; 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
D KOTESWARARAO, 228 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


{ while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { 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) {
D KOTESWARARAO, 229 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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) { 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"); }
D KOTESWARARAO, 230 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


push(k); } i++; } 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

b) Write C program to implement Trapezoidal method. #include<stdio.h> #include<conio.h>


D KOTESWARARAO, 231 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


#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; 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);
D KOTESWARARAO, 232 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


} /*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() { 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; }
D KOTESWARARAO, 233 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; 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();
D KOTESWARARAO, 234 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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; 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; }
D KOTESWARARAO, 235 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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"); 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;
D KOTESWARARAO, 236 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); break; } push(k); } i++; } 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, 237

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

C - Language Viva Questions

D KOTESWARARAO, 238

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, 239

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? D KOTESWARARAO, 240 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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? 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 D KOTESWARARAO, 241 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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 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.

D KOTESWARARAO, 242

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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? D KOTESWARARAO, 243 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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? 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.

D KOTESWARARAO, 244

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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? 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. D KOTESWARARAO, 245 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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? 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. D KOTESWARARAO, 246 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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.

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 D KOTESWARARAO, 247 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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?

D KOTESWARARAO, 248

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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? D KOTESWARARAO, 249 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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? D KOTESWARARAO, 250 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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? D KOTESWARARAO, 251 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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.

D KOTESWARARAO, 252

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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.

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. D KOTESWARARAO, 253 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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? 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) D KOTESWARARAO, 254 Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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? 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?

D KOTESWARARAO, 255

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL


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

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, 256

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

FOR ANY QUERIES PLEASE CONTACT

D KOTESWARARAO
D KOTESWARARAO, 257

Associate Professor, Dept. of CSE, VVIT.

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Phone No:

9441544277
Email

Id:d_koteswararao@yahoo.co.in

ThankQ ***************************

D KOTESWARARAO, 258

Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY