Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C&DS

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 101

Program 1(a):Aim: A Program to find sum of individual digits of a given number. #include<stdio.h> #include<conio.

h> void main( ) { int n,s=0,r; clrscr( ); printf(enter any number); scanf(%d,&n); while(n>0) { r=n%10; s=s+r; n/=10; } printf(sum=%d,s); getch( ); }

Input: enter any number 234 Output: 9

COMPUTER PROGRAMMING LAB MANUAL

Program 1(b):Aim: A Program to print Fibonacci series. #include<stdio.h> #include<conio.h> void main( ) { int a,b,c,n; clrscr( ); printf("enter the value of n: \t"); scanf("%d",&n); a=0; b=1; printf("%d\n%d\n",a,b); c=a+b; do { printf("%d ",c); a=b; b=c; c=a+b; } while(c<=n); getch( ); }

Input: enter the value of n 8 Output:0 1 1 2 3 5 8 13

COMPUTER PROGRAMMING LAB MANUAL

Program 1(c):Aim: A Program to print prime numbers between 1 and n. void main( ) { int i,j,n,; clrscr( ); printf(enter any number); scanf(%d,&n); for(i=1;i<=n;i++) { for(j=2;j<i;j++) { if(i%j==0) count++; } if(count==0) printf(%d\t,i); } getch( ); }

Input: enter any number 4 Output: 2 3

COMPUTER PROGRAMMING LAB MANUAL

Program 2(a):Aim: A Program to calculate the following sum: 1-x2/2! + x4/4! - ---- n terms. #include<stdio.h> #include<conio.h> #include<math.h> void main( ) { int i,n,fact,x; float sum=1; clrscr( ); printf("Enter the Value\n"); scanf("%d",&n); printf("Enter the value of x:\n"); scanf("%d",&x); for(i=2;i<=2*(n-1);i+=2) { fact*=i*(i-1); sum*(-1)i=pow(x,i)/(fact,i); } printf("Result= %f",sum); getch( ); }

Input: Enter the value 2 Enter the value of x 1


COMPUTER PROGRAMMING LAB MANUAL

Output: Result=1.5

Program 2(b):Aim: A Program to find roots of a quadratic equation. #include<stdio.h> #include<conio.h> void main( ) { int a,b,c,d; float r1,r2; clrscr( ); printf(enter a,b,c); scanf(%d%d%d,&a,&b,&c); d=b*b-4*a*c; if(d>0) { r1=((-b+sqrt(d))/(2*a)); r2=((-b-sqrt(d))/(2*a)); printf(roots are %f,%f,r1,r2); } else if (d==0) { r1=r2=(-b/(2*a)); printf(roots are %f,%f,r1,r2); } else { printf(roots are imaginary); } getch( ); }
COMPUTER PROGRAMMING LAB MANUAL

Input: enter a,b,c 5 6 8 Output: roots are imaginary

Program 3(a):Aim: A Program to find factorial of a given number using recursion. #include<stdio.h> #include<conio.h> int factorial(int); void main( ) { int n,k; printf(enter any number\n); scanf(%d,&n); k=factorial(n); printf(factorial of %d is %d,n,k); getch( ); } int factorial(int x) { int fact; if(x==0||x==1) return(1); else { fact=(x*factorial(x-1)); return(fact); } }

COMPUTER PROGRAMMING LAB MANUAL

Input: enter any number 4 Output: factorial of 4 is 24

Program 3(b):Aim: A Program to find factorial without using recursion. #include<stdio.h> #include<conio.h> void main( ) { int i,n,fact=1; clrscr( ); printf(enter any number\n); scanf(%d,&n); for(i=1;i<=n;i++) { fact=fact*i; } printf(factorial of %d is %d,n,fact); getch( ); } Input: enter any number 4 Output:factorial of 4 is 24

COMPUTER PROGRAMMING LAB MANUAL

Program 4(a):Aim: A Program to find gcd of two numbers. #include<stdio.h> #include<conio.h> void main( ) { int l,m,n; clrscr( ); printf("enter any two values"); scanf("%d%d",&m,&n); do { l=n%m; n=m; m=l; } while(l!=0); printf("gcd is %d",n); getch( ); }

Input: enter any two values


COMPUTER PROGRAMMING LAB MANUAL

2 3 Output: gcd is 1

Program 4(b):Aim: A Program to find gcd of two numbers using recursion. #include<stdio.h> #include<conio.h> int gcd(int a,int b); void main( ) { int a,b; clrscr( ); printf("enter any two values"); scanf("%d%d",&a,&b); printf("gcd is %d",gcd(a,b)); getch( ); } int gcd(int a,int b) { int x,y; x=a; y=b; if(y!=0) { x=gcd(y,x%y); } return(x);
COMPUTER PROGRAMMING LAB MANUAL

Input: enter any two values 6 24 Output: gcd is 6

Program 5(a): Aim: A Program to implement towers of hanoi. #include <stdio.h> #include <conio.h> #include <dos.h> typedef unsigned int uint; #define NUM_ELEMENTOS 3 int torres[NUM_ELEMENTOS][3]; void mostraTela() { int j, k, posx, posy; int i; clrscr( ); for(j = 0; j < 3; j++) { for(i = NUM_ELEMENTOS-1; i >= 0; i--) { posy = 20 - i; posx = 20 + (j * 20); for(k = 1; k <= torres[i][j]; k++) { gotoxy(posx-k, posy); putch('-');
COMPUTER PROGRAMMING LAB MANUAL

10

gotoxy(posx+k, posy); putch('-'); } gotoxy(posx, posy); if(torres[i][j]) printf("%d", torres[i][j]); putch('|'); } }

else

} void move(uint src, uint dst) { int i, is, id; for(i = NUM_ELEMENTOS-1; i >= 0; i--) { if(torres[i][src]) { is = i; break; } } for(i = 0; i < NUM_ELEMENTOS; i++) if(!torres[i][dst]) id = i; break; } torres[id][dst] = torres[is][src]; torres[is][src] = 0; mostraTela( ); gotoxy(1,1); printf("Moveu peca %u da torre %u para a torre %u.", torres[id][dst], src, dst); getch( ); } int podemover(uint peca, uint src, uint dst) {
COMPUTER PROGRAMMING LAB MANUAL

{ {

11

int i, is, id; for(i = NUM_ELEMENTOS-1; i >= 0; i--) if(torres[i][src]) is = i; break; } for(i = NUM_ELEMENTOS-1; i >= 0; i--) if(torres[i][dst]) { id = i; break; } } return (torres[is][src] == peca) && ((torres[0][dst] == 0) torres[is][src]); } }

{ {

|| torres[id][dst] >

uint ondeesta(uint peca) { int i, j; for(j = 0; j < 3; j++) { for(i = NUM_ELEMENTOS-1; i >= 0; i--) { if(torres[i][j] == peca) { return j; } } } return -1; } void ranoi(uint n, uint src, uint dst) { uint i, j, jog = 0, stemp, dtemp, peca,auxtemp; int sentido = 1;
COMPUTER PROGRAMMING LAB MANUAL

12

if(src > 2 || dst > 2 || src == dst || n == 0) return; for(i = 0; i < n; i++) { } if((n % 2) == 0) sentido = -1; peca = 1; for(i = 0; i < jog; i++) { stemp = ondeesta(peca); if(peca % 2) dtemp = ((3 + stemp) + sentido) % 3; jog = (2 * jog) + 1;

} else {

if(podemover(peca, stemp, (stemp + 1)%3)) dtemp = (stemp + 1)%3; else dtemp = (stemp + 2)%3; } move(stemp, dtemp); peca = (peca % n) + 1; stemp = ondeesta(peca); while(!podemover(peca, stemp, (stemp + 1)%3) && ! podemover(peca, stemp, (stemp + 2)%3)) { peca = (peca % n) + 1; stemp = ondeesta(peca); } } } int main( ) { uint i, j, src = 0, dst = 1;
COMPUTER PROGRAMMING LAB MANUAL

13

for(j = 0; j < 3; j++) { for(i = 0; i < NUM_ELEMENTOS; i++) { if(j == src) torres[i][j] = NUM_ELEMENTOS - i; else torres[i][j] = 0;

} mostraTela( ); gotoxy(1,1); getch( ); ranoi(NUM_ELEMENTOS, src, dst); gotoxy(1,1); getch( ); return 0; }

Output: -1--2----3--| --2----3--| | ---3--| | ---3--| | | | | | | | -1| | -1| | | | | ---3--| | | | | | | | --2 | -1--2 | -1--2-14

COMPUTER PROGRAMMING LAB MANUAL

| | -1| | -1| | |

| | ---3--| --2----3---1--2----3---

| | --2 | | | | | |

Program 5(b):Aim: A Program to solve Towers of Hanoi using recursion #include<stdio.h> #include<stdlib.h> #define N 3 int A[N], B[N], C[N]; void Hanoi(int,int*,int*,int*); void PrintAll(void) { int i; printf("A: "); for(i=0;i<N;i++)printf(" %d ",A[i]); printf("\n"); printf("B: ");
COMPUTER PROGRAMMING LAB MANUAL

15

for(i=0;i<N;i++)printf(" %d ",B[i]); printf("\n"); printf("C: "); for(i=0;i<N;i++)printf(" %d ",C[i]); printf("\n"); printf("-----------------------------------------\n"); return; } int Move(int *source, int *dest) { int i=0,j=0; while((*(source + i)==0)&&(i<N))i++; while((*(dest + j)==0)&&(j<N))j++;

*(dest+j-1) = *(source+i); *(source + i) = 0; PrintAll( ); return *(dest+j-1); } void Hanoi(int n,int *source, int *dest, int *spare) { int i; if(n==1){ Move(source,dest); return; } Hanoi(n-1,source,spare,dest); Move(source,dest); Hanoi(n-1,spare,dest,source); return;

} int main( ) {

int i; for(i=0;i<N;i++)A[i]=i+1;
COMPUTER PROGRAMMING LAB MANUAL

16

with

for(i=0;i<N;i++)B[i]=0; for(i=0;i<N;i++)C[i]=0; printf("Solution of Tower of Hanoi Problem %d Disks\n\n",N); printf("Starting state:\n"); PrintAll( ); printf("\n\nSubsequent states:\n\n"); Hanoi(N,A,B,C); return 0; } Output: Solution of Tower of Hanoi Problem with 3 Disks Starting state: A: 1 2 3 B: 0 0 0 C: 0 0 0 -----------------------------------------Subsequent states: A: 0 2 3 B: 0 0 1 C: 0 0 0 -----------------------------------------A: 0 0 3 B: 0 0 1 C: 0 0 2 -----------------------------------------A: 0 0 3 B: 0 0 0 C: 0 1 2 -----------------------------------------A: 0 0 0 B: 0 0 3 C: 0 1 2 -----------------------------------------A: 0 0 1 B: 0 0 3
COMPUTER PROGRAMMING LAB MANUAL

17

C: 0 0 2 -----------------------------------------A: 0 0 1 B: 0 2 3 C: 0 0 0 -----------------------------------------A: 0 0 0 B: 1 2 3 C: 0 0 0 ------------------------------------------

Program 6(a):Aim: The total distance traveled by vehicle in t seconds is given by Distance=ut+1/2a*a where u and a are the intial velocity (m/sec) and accleration (m/sec2).write a C program to find the distance traveled 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>
COMPUTER PROGRAMMING LAB MANUAL

18

#include<conio.h> void main() { int u,a,t,i; float d; clrscr(); printf("Enter the value of u,a \n"); scanf("%d%d",&u,&a); for(i=0;i<=50;i++) { t=i; d=(u*t)+(0.5*a*t*t); } printf("%g",d); getch(); } Input: Enter the value of u,a 2 3 Output: 3850

Program 6(b):Aim: A Program to perform arithmetic operation using switch-case. #include<stdio.h> #include<conio.h> void main() { int a,b,c; char ch; clrscr();
COMPUTER PROGRAMMING LAB MANUAL

19

printf("Enter the value of a\t\n"); scanf("%d",&a); printf("Enter the value of b \t \n"); scanf("%d",&b); printf("***********"); printf("\n MENU \n"); printf("***********"); printf("\n 1.Addition \n"); printf("\n 2.Subtraction \n"); printf("\n 3.Multiplication \n"); printf("\n 4.Division \n"); while(ch!='n') { printf("\n Enter u r choice\t\n"); scanf("%c",&ch); switch(ch) { case '+': c=a+b; printf("The sum is: %d",c); break; case '-': c=a-b; printf("The difference is: %d",c); break; case '*': c=a*b; printf("The Product is: %d",c); break;

case '/':

%d",c);

c=a/b; printf("The quotient is: break;

COMPUTER PROGRAMMING LAB MANUAL

20

getch();

Input: Enter the value of a 4 Enter the value of b 5 *********** MENU 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter u r choice 3 Output:The Product is:20

Program 7(a):Aim: of A Program to print the maximum and minimum elements in a list elements.

COMPUTER PROGRAMMING LAB MANUAL

21

#include<stdio.h> #include<conio.h> void main( ) { int i,n,a[10],max,min; clrscr( ); printf(enter size of the array); scanf(%d,&n); printf(enter elements); for(i=0;i<n;i++) scanf(%d,&a[i]); max=a[0]; min=a[0]; for(i=1;i<n;i++) { if(max<a[i]) max=a[i]; if(min>a[i]) min=a[i]; } printf(the maximum element is %d,minimum element is %d,max,min); getch( ); } Input: enter size of the array 3 enter elements 1 4 3 Output: the maximum element is 4, minimum element is 1

Program 7(b):Aim: A Program to perform addition and multiplication of two matrices


COMPUTER PROGRAMMING LAB MANUAL

22

using functions. #include<stdio.h> #include<conio.h> void add( ); void mul( ); void main( ) { clrscr( ); add( ); mul( ); getch( ); } void add( ) { int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10]; clrscr( ); printf(enter order of first matrix); scanf(%d%d,&m,&n); printf(enter order of second matrix); scanf(%d%d,&p,&q); if(m==p&&n==q) { printf(enter elements of first matrix); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&a[i][j]); printf(enter elements of second matrix); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&b[i][j]); for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } } printf(the addition matrix is\n); for(i=0;i<m;i++)
COMPUTER PROGRAMMING LAB MANUAL

23

for(j=0;j<n;j++) { printf(%d\t,c[i][j]); } printf(\n); }

} void mul( ) { int i,j,k,m,n,p,q,a[10][10],b[10][10],c[10][10]; clrscr( ); printf(enter order of first matrix); scanf(%d%d,&m,&n); printf(enter order of second matrix); scanf(%d%d,&p,&q); if(n==p) { printf(enter elements of first matrix); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&a[i][j]); printf(enter elements of second matrix); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&b[i][j]); for(i=0;i<m;i++) { for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<p;k++) { c[i][j]+=a[i][k]*b[k][j]; } }
COMPUTER PROGRAMMING LAB MANUAL

} else printf(addition is not possible);

24

} printf(the resultant matrix is\n); for(i=0;i<m;i++) { for(j=0;j<q;j++) {

} printf(\n);

printf(%d\t,c[i][j]);

} Input: enter order of first matrix 2 2 enter order of second matrix 2 2 enter elements of first matrix 5 6 3 4 enter elements of second matrix 4 5 9 4 enter order of first matrix 2 2 enter order of second matrix 2 2 enter elements of first matrix 3 2 5 6 enter elements of second matrix 2 5 9 3 Output: the addition matrix is 9 11 12 8 the resultant matrix is
COMPUTER PROGRAMMING LAB MANUAL

} else printf(multiplication is not possible);

25

24 64

21 43

Program 8:Aim: A Program that uses functions to perform the following operations: To insert a sub string into given main string from a given position To delete n characters from a given position in a given string. #include<stdio.h> #include<conio.h> #include<string.h> void insert( ); void delet( ); void main( ) { clrscr( ); insert( ); delet( ); getch( ); } void insert( ) { char str1[10],str2[10],str3[10]; int i,j,k,pos; printf("enter main string"); scanf("%s",str1); printf("enter position"); scanf("%d",&pos); printf("enter sub string"); scanf("%s",str2); for(i=0;i<pos;i++) { str3[i]=str1[i]; } for(j=0;str2[j]!='\0';i++,j++) {
COMPUTER PROGRAMMING LAB MANUAL

(i) (ii)

26

} for(k=pos;str1[k]!='\0';i++,k++) { str3[i]=str1[k]; } str3[i]='\0'; printf("the resultant string is\n"); printf("%s",str3); } void delet( ) { char str1[30],str2[10]; int i,j,pos,n,m; printf("enter string"); scanf("%s",str1); m=strlen(str1); printf("enter position and no.of characters to be deleted"); scanf("%d%d",&pos,&n); for(j=pos-1;j<(pos+n-1);j++) { str1[j]='\0'; } for(j=pos-1,i=(pos+n-1);i<m;i++,j++) { str1[j]=str1[i]; } str1[j]='\0'; printf("resultant string is\n"); printf("%s",str1); } Input: enter main string: abcdef enter position: 3 enter sub string: gg Output: the resultant string is abcggdef Input: enter sring: abcde enter position and no. of characters to be deleted: 3 2
COMPUTER PROGRAMMING LAB MANUAL

str3[i]=str2[j];

27

Output: resultant string is: abe

Program 9:Aim: not. A Program to check whether the given string is palindrome or

#include<stdio.h> #include<conio.h> #include<string.h> void main( ) { char s1[10],s2[10]; clrscr( ); printf(enter any string); scanf(%s,s1); strcpy(s2,s1); strrev(s2); if(strcmp(s1,s2)==0) printf(given string is a palindrome); else printf(not palindrome); getch( ); }

Input: enter any string mam Output:given string is a palindrome

COMPUTER PROGRAMMING LAB MANUAL

28

Program 10(a):Aim: A Program to display the position in string S where the string T begins or -1 if S doesnt contain T. #include<stdio.h> #include<conio.h> #include<string.h> void main( ) { char s[20],t[20],*p; int i,j,k; clrscr( ); printf("enter main string"); scanf("%s",s); printf("enter the string to be found"); scanf("%s",t); p=strstr(s,t); if(p!=NULL) printf("the string is found at position %d",p-s); else printf("%d",-1); getch( ); } Input: enter main string abcde enter the string to be found cd Output: the string is found at position 2
COMPUTER PROGRAMMING LAB MANUAL

29

Program 10(b):Aim: A Program to count the lines, words and characters in the given text. #include <stdio.h> #include <conio.h> void main( ) { char c,choice; char str[10]; int nc=0,nw=1,nl=0,count=0,i; clrscr( ); printf("ENTER STRING:- "); while ((c=getchar())!=EOF) { if(c==' '||(c>=65&&c<=90)||(c>=97&&c<=122)) nc++; else if(c=='\n') nl++; if(c==' '||c=='\n') nw++; } printf("no. of characters is %d",nc); printf("no. of words is %d",nw); printf("no. of lines is %d",nl); fflush(stdin); printf ("Do you want to continue?(y/n):- ");
COMPUTER PROGRAMMING LAB MANUAL

30

scanf("%c",&choice); getch( ); } Input: ENTER STRING:abc def ghi Output: no. of characters is 10 no. of words is 3 no. of lines is 2

Program 11(a):Aim: A Program to print Pascal triangle #include<stdio.h> #include<conio.h> void main( ) { int a[10][10]; int i,j,c,n; clrscr( ); printf("Enter how many lines do you want"); scanf("%d",&n); a[1][1]=1; printf("%5d",a[1][1]); a[2][1]=1;a[2][2]=2;a[2][3]=1; printf("%d %d %d",a[2][1],a[2][2],a[2][3]); for(i=3;i<=n;i++) { a[i][1]=1; printf("%d",a[i][1]); j=2;c=3; while(j<=i) { a[i][j]=a[i-1][c-1]+a[i-1][c-2]; printf("%5d",a[i][j]);
COMPUTER PROGRAMMING LAB MANUAL

31

c=c+1; j=j+1; } a[i][j]=1; printf("%d",a[i][j]);

} Input: Enter how many lines do you want 3 Output: 1 1 2 1 1 3 3 1

} getch( );

Program 11(b):Aim: A Program to print the sequence 4 7 #include<stdio.h> #include<conio.h> void main( ) { int i,j,n,k,l=1; clrscr( ); printf(enter number of rows); scanf(%d,&n); for(i=1;i<=n;i++) { for(j=1;j<=(n-i);j++) printf( ); for(k=1;k<(2*i-1);k++) { if(k%2==0) printf( ); else
COMPUTER PROGRAMMING LAB MANUAL

1 2 8 5 3 9 6 10

32

printf(%d\t,l++);

} getch( ); } Input:enter number of rows 4 Output: 1 2 4 5 7 8

3 9 6 10

Program 12:Aim: A Program to read two numbers x and n and then compute the sum of this geometrix progression. 1+x+x2+x3++xn. #include<stdio.h> #include<conio.h> #include<math.h> void main( ) { int i,sum=0,x,n; printf("enter the values of x and n"); scanf("%d%d",&x,&n); for(i=0;i<=n;i++) { sum=sum+pow(x,i); } printf("result of the expression is %d",sum); getch( ); }
COMPUTER PROGRAMMING LAB MANUAL

33

Input: enter the values of x and n 1 3 Output: result of the expression is 3

Program 13:Aim: A Program to find 2s complement of a binary number. #include<stdio.h> #include<conio.h> void main( ) { int a[10],b[10],i,j,n; clrscr( ); printf("enter no. of digits"); scanf("%d",&n); printf("enter the binary number"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=n-1,j=0;i>=0;i--,j++) { if(a[i]!=1) { b[j]=a[i]; }
COMPUTER PROGRAMMING LAB MANUAL

34

else { b[j]=a[i]; --i; ++j; for(;i>=0;i--,j++) { if(a[i]==0) b[j]=1; else b[j]=0; } } printf("the 2's complement is "); for(j=0;j<n;j++) printf("%d",b[j]); getch( ); } }

Input:enter no. of digits 4 enter the binary number 1 0 0 1 Output: the 2's complement is 1110

COMPUTER PROGRAMMING LAB MANUAL

35

Program 14:Aim: A Program to convert a Roman numeral to its decimal equivalent. 36

COMPUTER PROGRAMMING LAB MANUAL

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main( ) { clrscr( ); int *a,l,i,j,k; char *s; printf("Enter The Roman Number"); scanf("%s",s); l=strlen(s); for(i=0;i<l;i++) { if(s[i]=='I') a[i]=1; else if(s[i]=='V') a[i]=5; else if(s[i]=='X') a[i]=10; else if(s[i]=='L') a[i]=50; else if(s[i]=='C') a[i]=100; else if(s[i]=='D') a[i]=500; else if(s[i]=='M') a[i]=1000; else { printf("Wrong Input"); getch(); exit(0); } }

k=a[l-1];
COMPUTER PROGRAMMING LAB MANUAL

37

for(i=l-1;i>0;i--) { if(a[i]>a[i-1])

} printf("%d",k); getch( ); }

k=k-a[i-1]; else if(a[i]==a[i-1] || a[i]<a[i-1]) k=k+a[i-1];

Input: Enter The Roman Number VI Output: 6

COMPUTER PROGRAMMING LAB MANUAL

38

Program 15:Aim: A Program to add and multiply two complex numbers (x+iy) and (a+ib). #include<stdio.h> #include<conio.h> struct complex add(struct complex,struct complex); struct complex mul(struct complex,struct complex); struct complex { int real,imag; }; void main( ) { struct complex c1,c2,c3,c4; printf("enter first number"); scanf("%d%d",&c1.real,&c1.imag); printf("enter second number"); scanf("%d%d",&c2.real,&c2.imag); c3=add(c1,c2); c4=mul(c1,c2); printf("addition is %d+i%d\n",c3.real,c3.imag); printf("subtraction is %d+i%d",c4.real,c4.imag); getch(); } struct complex add(struct complex c1,struct complex c2) { struct complex c3; c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return(c3); } struct complex mul(struct complex c1,struct complex c2) { struct complex c4; c4.real=(c1.real*c2.real)-(c1.imag*c2.imag);
COMPUTER PROGRAMMING LAB MANUAL

39

c4.imag=(c1.imag*c2.real)+(c1.real*c2.imag); return(c4); }

Input: enter first number 2 3 enter second number 2 3 Output: addition is 4+i6 multiplication is 5+i12

COMPUTER PROGRAMMING LAB MANUAL

40

Program 16(a):Aim: A Program to copy contents of one file to another. #include<stdio.h> #include<conio.h> void main( ) { FILE *fs,*ft; char ch; fs=fopen(file1.txt,r); if(fs==NULL) { printf(cannot open file); exit(0); } ft=fopen(file2.txt,w); if(ft==NULL) { printf(cannot open file); exit(0); } while((ch=getc(fs))!=EOF) { putc(ch,ft); } fclose(ft); printf( the copied contents are \n); ft=fopen(file2.txt, r); while((ch=getc(ft))!=EOF) { putchar(ch); } fclose(fs); fclose(ft); getch( );
COMPUTER PROGRAMMING LAB MANUAL

41

} Output:the copied contents are Qis College of engineering and technology.

Program 16(b):Aim: A Program to reverse the first n characters in a file. #include<stdio.h> #include<conio.h> void main( ) { FILE *fp; char ch,str[10]; int i,n; clrscr( ); fp=fopen(file1.txt,r); if(fp==NULL) { printf(file cannot be opened); exit(0); } printf(enter n); scanf(%d,&n); for(i=0;i<n;i++) { str[i]=getc(fp); } for(i=n-1;i>=0;i--) { putchar(str[i]); } while((ch=getc(fp))!=EOF) { putchar(ch); } getch( ); }
COMPUTER PROGRAMMING LAB MANUAL

42

Output:enter n 4 college of engineering and technology

Program 17:Aim: A Program that uses functions to perform the following operations on a singly linked list. (a) Creation (b) Insertion (c) Deletion (d) traversal #include<stdio.h> #include<conio.h> void create(int); void insatend(int); void insatpos(int,int); void delatbeg( ); void delatend( ); void delatpos(int); void traversal( ); struct node { int ele; struct node *next; }; struct node *start=NULL; void main( ) { int ch,ele; printf(1.create 2.insatend 3.insertion at position 4.delatbeg 5.delatend 6.deletion at position 7.traversal); do { printf(enter ur choice); scanf(%d,&ch); switch(ch) { case 1: printf(enter element to be inserted);
COMPUTER PROGRAMMING LAB MANUAL

43

scanf(%d,&ele); create(ele); break; case 2: printf(enter element to be inserted); scanf(%d,&ele); insatend(ele); break;

case 3: printf(enter element and position to be inserted); scanf(%d%d,&ele,&pos); insatpos(ele,pos); break; case 4: delatbeg(); break; case 5: delatend(); break; case 6: printf(enter position to be deleted); scanf(%d,&pos); delatpos(pos); break; case 7: traversal(); break; } while(ch>=1&&ch<=7); getch( ); } void create(int ele) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { start=temp; start->next=NULL; } else
COMPUTER PROGRAMMING LAB MANUAL

44

temp->next=start; start=temp;

} void insatend(int ele) { struct node *temp,*p; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { start=temp; start->next=NULL; } else { p=start; while(p->next!=NULL) p=p->next; p->next=temp; temp->next=NULL; } } void insatpos(int ele,int pos) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { if(pos==1) { start=temp; start->next=NULL; } else printf(insertion is not possible at this position); } else if (start->next==NULL)
COMPUTER PROGRAMMING LAB MANUAL

45

if(pos==1) { temp->next=start; start->next=NULL; start=temp; } else if(pos==2) { start->next=temp; temp->next=NULL; } else printf(insertion is not possible at this position);

} else {

int count=0; struct node *p; p=start; while(p!=NULL) { p=p->next; count++; } if(pos<=count+1) { if(pos==1) { temp->next=start; start=temp; } else { p=start; for(i=1;i<pos-1;i++) p=p->next; temp->next=p->next; p->next=temp; } 46

COMPUTER PROGRAMMING LAB MANUAL

} else printf(insertion is not possible at this position);

} void delatbeg() { struct node *temp; if(start==NULL) printf(list is empty); else if(start->next==NULL) { temp=start; start=NULL; free(temp); } else { temp=start; start=start->next; free(temp); } } void delatend() { struct node *temp; if(start==NULL) printf(list is empty); else if(start->next==NULL) { temp=start; start=NULL; free(temp); } else { struct node *p; p=start; while(p->next->next!=NULL) p=p->next;
COMPUTER PROGRAMMING LAB MANUAL

47

temp=p->next; p->next=NULL; free(temp);

} void delatpos(int pos) { struct node *temp,*p; int count=0; if(start==NULL) printf(list is empty); else if (start->next==NULL) { if(pos==1) { temp=start; start=NULL; free(temp); } else printf(deletion is not possible at this position); } else { p=start; while(p!=NULL) { p=p->next; count++; } if(pos<=count) { if(pos==1) { temp=start; start=start->next; free(temp); } else {
COMPUTER PROGRAMMING LAB MANUAL

48

for(i=1;i<pos-1;i++) p=p->next; temp=p->next; p->next=p->next->next; free(temp);

} else printf(deletion is not possible at this position);

} void traversal() { struct node*p; if(start==NULL) printf(list is empty); else { p=start; while(p!=NULL) { printf(%d\t,p->ele); p=p->next; } } }

Output:1.create 2.insatend 3.insertion at position 4.delatbeg 5.delatend 6.deletion at position 7.traversal enter your choice 1 enter element to be inserted 11 enter your choice 3 enter element and position to be inserted 23 3 enter your choice 1
COMPUTER PROGRAMMING LAB MANUAL

49

Program 18:Aim: A Program that uses functions to perform the following operations on a doubly linked list. (a) Creation (b) Insertion (c) Deletion (d) traversal in both ways #include<stdio.h> #include<conio.h> void create(int); void insatend(int); void insatpos(int,int); void delatbeg(); void delatend(); void delatpos(int); void traversal(); void traversalback(); struct node { int ele;
COMPUTER PROGRAMMING LAB MANUAL

50

struct node *prev; struct node *next; }; struct node *start=NULL; void main() { int ch,ele; printf(1.create 2.insatend 3.insertion at position 4.delatbeg 5.delatend 6.deletion at position 7.traversal 8.traversalback); do { printf(enter ur choice); scanf(%d,&ch); switch(ch) { case 1: printf(enter element to be inserted); scanf(%d,&ele); create(ele); break; case 2: printf(enter element to be inserted); scanf(%d,&ele); insatend(ele); break; case 3: printf(enter element and position to inserted); scanf(%d%d,&ele,&pos); insatpos(ele,pos); break; delatbeg(); break; delatend(); break; printf(enter position to be deleted); scanf(%d,&pos); delatpos(pos); break; traversal(); break; 51

be

case 4: case 5: case 6:

case 7:

COMPUTER PROGRAMMING LAB MANUAL

case 8: traversalback(); break; } while(ch>=1&&ch<=8); }

} void create(int ele) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { start=temp; start->prev=NULL; start->next=NULL; } else { temp->next=start; temp->prev=NULL; start=temp; } } void insatend(int ele) { struct node *temp,*p; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { start=temp; start->prev=NULL; start->next=NULL; } else { p=start; while(p->next!=NULL) p=p->next;
COMPUTER PROGRAMMING LAB MANUAL

52

temp->prev=p; p->next=temp; temp->next=NULL;

} void insatpos(int ele,int pos) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp->ele=ele; if(start==NULL) { if(pos==1) { start=temp; start->prev=NULL; start->next=NULL; } else printf(insertion is not possible at this position); } else if (start->next==NULL) { if(pos==1) { temp->next=start; start->prev=temp; start=temp; start->prev=NULL; } else if(pos==2) { start->next=temp; temp->prev=start; temp->next=NULL; } else printf(insertion is not possible at this position); } else
COMPUTER PROGRAMMING LAB MANUAL

53

int count=0; struct node *p; p=start; while(p!=NULL) { p=p->next; count++; } if(pos<=count+1) { if(pos==1) { temp->next=start; start->prev=temp; start=temp; start->prev=NULL; } else { p=start; for(i=1;i<pos-1;i++) p=p->next; temp->next=p->next; p->next->prev=temp; p->next=temp; temp->prev=p; } } else printf(insertion is not possible at this position);

} void delatbeg() { struct node *temp; if(start==NULL) printf(list is empty); else if(start->next==NULL) {
COMPUTER PROGRAMMING LAB MANUAL

54

} else {

temp=start; start=NULL; free(temp);

temp=start; start=start->next; start->prev=NULL; free(temp); } void delatend() { struct node *temp; if(start==NULL) printf(list is empty); else if(start->next==NULL) { temp=start; start=NULL; free(temp); } else { struct node *p; p=start; while(p->next->next!=NULL) p=p->next; temp=p->next; p->next=NULL; free(temp); } } void delatpos(int pos) { struct node *temp,*p; int count=0; if(start==NULL) printf(list is empty);
COMPUTER PROGRAMMING LAB MANUAL

55

else if (start->next==NULL) { if(pos==1) { temp=start; start=NULL; free(temp); } else printf(deletion is not possible at this position); } else { p=start; while(p!=NULL) { p=p->next; count++; } if(pos<=count) { if(pos==1) { temp=start; start=start->next; start->prev=NULL; free(temp); } else { for(i=1;i<pos-1;i++) p=p->next; temp=p->next; temp->next->prev=p; p->next=p->next->next; free(temp); } } else printf(deletion is not possible at this position);
COMPUTER PROGRAMMING LAB MANUAL

56

} void traversal() { struct node*p; if(start==NULL) printf(list is empty); else { p=start; while(p!=NULL) { printf(%d\t,p->ele); p=p->next; } } } void traversalback() { struct node *p; if(start==NULL) printf(list is empty); else { p=start; while(p->next!=NULL) p=p->next; while(p!=NULL) { printf(%d\t,p->ele); p=p->prev; } } }

Output:1.create 2.insatend 3.insertion at position 4.delatbeg


COMPUTER PROGRAMMING LAB MANUAL

57

5.delatend 6.deletion at position 7.traversal 8.traversalback enter your choice 1 enter element to be inserted 11 enter your choice 3 enter element and position to be inserted 23 3 enter your choice 1

Program 19:Aim: A Program to implement stack using arrays. #define max 20


COMPUTER PROGRAMMING LAB MANUAL

58

void push(int); int pop(); void display(); int stack[max],top=-1; void main() { int ch,ele; printf(1.Push\n2.Pop\n3.Display); do { printf(Enter ur choice); scanf(%d,&ch); switch(ch) { case 1: printf(Enter element to be inserted); scanf(%d,&ele); push(ele); break; case 2: ele=pop(); if(ele!=-1) printf(Deleted element is %d,ele); break; case 3: display(); break; } }while(ch>=1&&ch<=3); getch(); } void push(int ele) { if(top>=max-1) printf(stack is full); else { top++; stack[top]=ele;

} int pop() {

COMPUTER PROGRAMMING LAB MANUAL

59

int ele; if(top==-1) { printf(Stack is empty); return(-1); } else { ele=stack[top]; top--; return(ele); } } void display() { if(top==-1) printf(Stack is empty); else { for(i=0;i<=top;i++) printf(%d stack[i]); } } Output: 1.Push 2.Pop 3.Display Enter ur choice 1 Enter element to be inserted 11 Enter ur choice 2 Deleted element is 11 Enter ur choice 3 Stack is empty

COMPUTER PROGRAMMING LAB MANUAL

60

Program 20:Aim: A Program to implement stack using pointers. #include<stdio.h> #include<conio.h> void push( ); void pop( ); void display( ); struct node { int ele; struct node *next; }*start; void main( ) { int ch; start=NULL; do { printf(1.Push\n 2.Pop\n 3.Display\n); printf(Enter ur choice); scanf(%d,&ch); switch(ch) { case 1: push() ; break; case 2: pop( ); break; case 3: display( ); break; default: printf(Enter choice); break; } } while(ch>=1&&ch<=3); } void push( )
COMPUTER PROGRAMMING LAB MANUAL

61

} void pop( ) { struct node *temp; if(start==NULL) { printf(list is empty); else { temp=start; printf(Deleted element is %d,temp->ele); start=start->next; free(temp); } } void display( ) { struct node *p; p=start; if(start==NULL) printf(List is empty); else { while(p!=NULL) { printf(%d,p->ele); p=p->next; } } } Output: 1.Push 2.Pop
COMPUTER PROGRAMMING LAB MANUAL

struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); printf(Enter element to be inserted); scanf(%d,&temp->ele); temp->next=start; start=temp;

62

3.Display Enter ur choice 1 Enter element to be inserted 11 Enter ur choice 2 Deleted element is 11 Enter ur choice 3 List is empty

COMPUTER PROGRAMMING LAB MANUAL

63

Program 21:Aim: A Program to implement queue using arrays. #define max 20 void enqueue(int); int dequeue(); void display(); int queue[max],f=0,r=-1; void main() { int ele, ch; do { printf(1.Enqueue\n 2.Dequeue \n3.Display\n); printf(Enter ur choice); scanf(%d, &ch); switch(ch) { case 1: printf(Enter element to be inserted); scanf(%d,&ele); enqueue(ele); break; case 2: ele=dequeue(); if(ele!=-1) printf(Deleted element is %d,ele); break; case 3: display(); break; } } while(ch>=1&&ch<=3); }
COMPUTER PROGRAMMING LAB MANUAL

64

void enqueue(int ele) { r++; if(r>=max-1) printf(queue is full); else { queue[r]=ele;

} int dequeue() { int ele; if(f>r) { printf(queue is empty); } else {

ele=queue[f]; f++; return(ele);

} void display() { int i; if(f>r) { printf(queue is empty); } else { for(i=f;i<=r;i++) printf(%d ,queue[i]); } } Output:1.Enqueue
COMPUTER PROGRAMMING LAB MANUAL

65

2.Dequeue 3.Display Enter ur choice 1 Enter element to be inserted 11 Enter ur choice 2 Deleted element is 11

Program 22:Aim: A Program to implement queue using pointers. #include<stdio.h> #include<conio.h> void enqueue( ); void dequeue( ); void display( ); struct node { int ele; struct node *next; }*start; void main() { int ch; start=NULL; do { printf(1.Enqueue 2.Dequeue 3.Display\n); printf(enter ur choice); scanf(%d,&ch); switch(ch) { case 1: enqueue( ); break; case 2: dequeue( );
COMPUTER PROGRAMMING LAB MANUAL

66

break; case 3: display( ); break; default: printf(Enter choice between 1 and 3); break;

} void enqueue() { struct node *temp,*p; temp=(struct node*)malloc(sizeof(struct node)); printf(Enter element to be inserted); scanf(%d,&temp->ele); if(start==NULL) { start=temp; start->next=NULL; } else { p=start; while(p!=NULL) { p=p->next; } p->next=temp; temp->next=NULL; } } void dequeue() { struct node *temp; if(start==NULL) { printf(queue is empty); } else
COMPUTER PROGRAMMING LAB MANUAL

} while(ch>=1&&ch<=3); getch( );

67

temp=start; printf(Deleted element is %d,temp->ele); start=start->next; free(temp);

} void display() { struct node *p; p=start; if(start==NULL) { printf(queue is empty); } else { while(p!=NULL) { printf(%d,p->ele); p=p->next; } } }

Output: Enqueue Dequeue Display Enter ur choice 1 Enter element to be inserted 11 Enter ur choice 2 Deleted element is 11

COMPUTER PROGRAMMING LAB MANUAL

68

Program 23:Aim: A Program to convert the given infix expression to postfix expression. #include<stdio.h> #include<conio.h> #include<ctype.h> void push(char); char pop( ); int opera(char); int prio(char); char stack[20]; int top=-1; void main( ) { char inf[10],ch; int i; printf(enter infix expression); scanf(%s,inf); for(i=0;inf[i]!=\0;i++) { if(isalnum(inf[i]))
COMPUTER PROGRAMMING LAB MANUAL

69

printf(%c,inf[i]); else if(opera(inf[i]) { while(prio(inf[i])<=prio(stack[top])) { printf(%c,pop()); } push(inf[i]); } else if(inf[i]==() push(inf[i]); else if(inf[i]==)) { while((ch=pop())!=() { printf(%c,ch); } } } while(top!=-1) printf(%c,pop()); getch( ); } int opera(char ch) { if(ch==+||ch==-||ch==*||ch==/||ch==%) return(1); else return(0); } int prio(char ch) { switch(ch) { case +: case -: return(1); case *: case /: case %:return(2); default: return(0);
COMPUTER PROGRAMMING LAB MANUAL

70

} void push(char ch) { if(top>=max-1) { printf(stack is full); } else { top++; stack[top]=ch; } } char pop( ) { char ch; if(top==-1) { printf(stack is empty); exit(0); } else { ch=stack[top]; top--; return(ch); } } Input: enter any infix expression a+b-c Output: ab+c-

COMPUTER PROGRAMMING LAB MANUAL

71

Program 24:Aim: A Program to evaluate the given postfix expression. void push(int); int pop(); int opera(char); int stack[10],top=-1; void main() { int i,x,y;
COMPUTER PROGRAMMING LAB MANUAL

72

} void push(int ele) { if(top>=max-1) { printf(stack is full); }


COMPUTER PROGRAMMING LAB MANUAL

char post[10]; printf(enter any postfix expression); scanf(%s,post); for(i=0;post[i]!=\0;i++) { if(isdigit(post[i]) { push(post[i]-48); } else if(opera(post[i]) { x=pop(); y=pop(); switch(post[i]) { case +: push(y+x); break; case -: push(y-x); break; case *: push(y*x); break; case /: push(y/x); break; case %: push(y%x); break; } } } if(top==0) { printf(result=%d,pop()); } getch();

73

else { top++; stack[top]=ele; } int pop() { int ele; if(top==-1) { printf(stack is empty); return(-1); } else { ele=stack[top]; top--; return(ele); } } int opera(char ch) { if(ch==+||ch==-||ch==*||ch==/||ch==%) return(1); else return(0); } Input: enter any infix expression 12+3Output: 0 }

Program 25:Aim: A Program that uses functions to perform the following: (i)Creating a Binary Tree of integers. (ii)Traversing the above tree in preorder,inorder and postorder.

COMPUTER PROGRAMMING LAB MANUAL

74

#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct bt { struct bt *lc; int d; struct bt *rc; }node; void void void void void { insert(node **,int); inorder(node *); postorder(node *); preorder(node *); main( ) node *root=NULL; int e,n,i; clrscr( ); printf("\n Enter the Number of Nodes \n"); scanf("%d",&n); printf("\n Enter the Elements \n"); for(i=1;i<=n;i++) { scanf("%d",&e); insert(&root,e); } inorder(root); postorder(root); preorder(root); } void insert(node **r,int e) {

if(*r==NULL) { *r=(node *)malloc(sizeof(node)); (*r)->lc=(*r)->rc=NULL ;


COMPUTER PROGRAMMING LAB MANUAL

75

(*r)->d=e; return; } else {

} }

if(e<(*r)->d) insert(&((*r)->lc),e); else insert(&((*r)->rc),e);

void inorder(node *r) { if(r!=NULL) { inorder(r->lc); printf("Inorder elements are: %d\n",r->d); inorder(r->rc); } else return; } void postorder(node *r) { if(r!=NULL) { postorder(r->lc); postorder(r->rc); printf("Postorder elements are: %d\n",r->d); } else return; } void preorder(node *r) { if(r!=NULL) { printf("Preorder elements are: %d\n",r->d);
COMPUTER PROGRAMMING LAB MANUAL

76

preorder(r->lc); preorder(r->rc); } else return;

Input: Enter the Number of Nodes 4 Enter the Elements 9 10 11 12 Output:Inorder elements are: 9 Inorder elements are: 10 Inorder elements are: 11 Inorder elements are: 12 Postorder elements are: 12 Postorder elements are: 11 Postorder elements are: 10 Postorder elements are: 9 Preorder elements are: 9 Preorder elements are: 10 Preorder elements are: 11 Preorder elements are: 12

Program 26:COMPUTER PROGRAMMING LAB MANUAL

77

Aim: To write a program to implement linear search. #include<stdio.h> #include<conio.h> void main( ) { int a[10],i,n,search; printf(Enter no of elements \n); scanf(%d,&n); printf(Enter elements \n); for(i=0;i<n;i++) scanf(%d,&a[i]); printf(Enter element to be searched\n); scanf(%d,&search); for(i=0;i<n;i++) { if(a[i]==search) { printf( %d is found at %d position,search,i+1); exit(); } } printf(element %d is not found, search); getch( ); } Input: Enter no. of elements 4 Enter elements 5 9 3 7 Enter element to be searched 7 Output: 7 is found at 4 position

Program 27:COMPUTER PROGRAMMING LAB MANUAL

78

Aim: A program to implement binary search. int bin_search(int a[ ], int n, int ele); void main( ) { int a[10],i,n,ele,pos; printf(enter the size of an array \n); scanf(%d,&n); printf(enter elements :\n); for(i=0;i<n;i++) scanf(%d,&a[i]); printf(Enter element to be searched\n); scanf(%d,&ele); pos=bin-search(a,n,ele); if(pos>=0) { printf(Elements %d is found at %d position, ele,pos+1); exit(0); } else printf(Element is not found); getch(); } int bin-search(int a[ ],int n,int ele) { int low,high,mid; low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(ele==a[mid]) high=mid-1; else low=mid+1; } return(-1); }
COMPUTER PROGRAMMING LAB MANUAL

79

Input: Enter size of the array 4 Enter elements 56 90 34 76 Enter element to be searched 76 Output: 76 is found at 4 position

COMPUTER PROGRAMMING LAB MANUAL

80

Program 28:Aim: A program to implement Bubble sort. #include<stdio.h> #include<conio.h> void main( ) { int a[10],i,j,n,temp; printf(enter the size of an array \n); scanf(%d,&n); printf(enter elements :\n); for(i=0;i<n;i++) scanf(%d,&a[i]); for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf(Sorted order is); for(i=0;i<n;i++) printf(%d\n,a[i]); getch( ); } Input: Enter size of an array 3 Enter elements 5 9 4 Output: Sorted order is 4 5 9
COMPUTER PROGRAMMING LAB MANUAL

81

Program 29:Aim: A program to implement Quick sort. #include<stdio.h> #include<conio.h> int partition(int m,int n); void quick_sort(int p,int q); int a[10]; void main( ) { int i,size,p=0,q; printf(Enter size of array\n); scanf(%d,&size); q=size-1; printf(Enter elements:); for(i=0;i,size;i++) scanf(%d,&a[i]); quick_sort(p,q); printf(After sorting ); for(i=0;i,size;i++) scanf(%d ,&a[i]); getch( ); } void quick_sort(int p,int q) { int j; if(p<q) { j=partition(p,q+1); quick_sort(p,j-1); quick_sort(j+1,q); } } int(partition(int m,int n) {
COMPUTER PROGRAMMING LAB MANUAL

82

int key,l,r,temp; key=a[m]; l=m; r=n; do {

do {

l++;

} while(l<r); a[m]=a[r]; a[r]=key; return( r); }

} while(a[l]<key); do { r--; } while(a[r]>key); if(l<r) { temp=a[l]; a[l]=a[r]; a[r]=temp; }

Input: Enter size of an array 5 Enter elements 7 2 9 1 3 Output: After sorting 1 2 3 7 9

COMPUTER PROGRAMMING LAB MANUAL

83

Program 30:Aim: To write a program to implement Insertion sort.

#include<stdio.h> #include<conio.h> void main( ) { int a[10],i,j,n,small; clrscr( ); printf(enter the size of an array \n); scanf(%d,&n); printf(enter elements :\n); for(i=0;i<n;i++) scanf(%d,&a[i]); for(i=0;i<n;i++) { small=a[i]; j=i; while((j>0)&&a[j-i]>small) { a[j]=a[j-i]; j--; } a[j]=small; } printf(Sorted order is\n); for(i=0;i<n;i++) printf(%d ,a[i]); getch(); } Input: Enter size of an array
COMPUTER PROGRAMMING LAB MANUAL

84

3 Enter elements 2 7 3 Output: Sorted array is 2 3 7

Program 31:Aim: A program to implement merge sort. #include<stdio.h> #include<conio.h> void merge_sort(int a[ ],int b[ ],int c[ ], int n1, int n2,int *n); void main( ) { int a[10],b[10],c[20],n1,n2,n,i; clrscr( ); printf(Enter size of first array:); scanf(%d,&n1); printf(Enter elements of first array in sorted order \n); for(i=0;i<n;i++) scanf(%d,&a[i]); printf(Enter size of second array:); scanf(%d,&n2); printf(Enter elements of second array in sorted order \n); for(i=0;i<n2;i++) scanf(%d,&b[i]); merge_sort(a,b,c,n1,n2,&n); printf(After sorting\n); for(i=0;i<n;i++) printf(%d ,c[i]); for(i=0;i<n;i++) printf(%d ,c[i]); getch( ); } void merge_sort(int a[],int b[],int c[], int n1, int n2,int *n)
COMPUTER PROGRAMMING LAB MANUAL

85

int i=0;j=0;k=0; while((i<n1)&&(j<n2)) { if(a[i]<b[j]) { c[k]=a[i]; i++; } else

c[k]=b[j]; j++; } k++;

} while(i<n1) { c[k]=a[i]; i++; K++; } while(j<n2) { c[k]=b[j]; j++; k++; } *n=k; }

Input: Enter size of first array 3 Enter elements of first array in sorted order 1 2 3 Enter size of second array
COMPUTER PROGRAMMING LAB MANUAL

86

4 Enter elements of second array in sorted order 4 5 6 7 Output: after sorting 1 2 3 4 5 6 7

Program 32:Aim: A Program to implement Lagrange Interpolation. #include<stdio.h> #include<conio.h> void main( ) { int n,i,m,j; float x[10],y[10],t,sum,tk,k; printf("enter number of values"); scanf("%d",&n); printf("enter values for x and y"); for(i=0;i<n;i++) { scanf("%f%f",&x[i],&y[i]); } printf("enter the required interpolation value"); scanf("%f",&t); sum=0; for(k=0;k<n;k++) { tk=1; for(j=0;j<n;j++) { if(j!=k)
COMPUTER PROGRAMMING LAB MANUAL

87

{ }

tk=tk*((t-x[j])/(x[k]-x[j]));

} printf("x=%f,y=%f",t,sum); getch( ); } Output:enter number of values3 enter values for x and y1 1 2 4 5 10 enter the required interpolation value3 x=3.000000,y=6.500000

} sum=sum+(tk*y[k]);

Program 33:Aim: A Program to implement Newton-Gregory forward interpolation method. #include<stdio.h> #include<conio.h> void main( ) { int n,i,k,j; float x[20],y[20],deltay[20] [20],a,h,p,pvalue,factvalue,term,sumy=0; printf("enter number of pairs\n"); scanf("%d",&n); printf("enter values for x\n"); for(i=0;i<n;i++) scanf("%f",&x[i]); printf("enter values for y\n"); for(i=0;i<n;i++) scanf("%f",&y[i]); printf("enter the x value required\n"); scanf("%f",&a); h=x[1]-x[0]; p=(a-x[0])/h;
COMPUTER PROGRAMMING LAB MANUAL

88

for(i=0;i<n;i++) deltay[i][0]=y[i]; for(i=0;i<n;i++) deltay[i][1]=deltay[i+1][0]-deltay[i][0]; k=n-2; for(i=2;i<n;i++) { k=k-1; for(j=0;j<=k;j++) deltay[j][i]=deltay[j+1][i-1]-deltay[j][i-1]; } pvalue=1; factvalue=1; sumy=y[0]; for(i=1;i<n;i++) { pvalue=pvalue*(p-i+1); factvalue=factvalue*i; term=(pvalue*deltay[0][i]); sumy=sumy+term; } printf("the interpolated value of %f is %f",a,sumy); getch( );

Output: enter number of pairs 4 enter values for x 0.1 0.2 0.3 0.4 enter values for y 1.005 1.020 1.045 1.081 enter the x value required 0.16 the interpolated value of 0.160000 is 1.011936
COMPUTER PROGRAMMING LAB MANUAL

89

Program 34:Aim: A Program for linear regression. #include<stdio.h> #include<conio.h> void main( ) { int n,i,j; float x[20],y[20],sx=0,sy=0,sxy=0,a0,a1,sx2=0,mx,my; clrscr( ); printf("how many sets of data you want to enter"); scanf("%d",&n); printf("enter x,y values in sets\n"); for(i=0;i<n;i++) scanf("%f%f",&x[i],&y[i]); for(i=0;i<n;i++) { sx=sx+x[i]; sy=sy+y[i];
COMPUTER PROGRAMMING LAB MANUAL

90

sxy=sxy+(x[i]*y[i]); sx2=sx2+(x[i]*x[i]); } a0=(sy*sx2-sx*sxy)/(n*sx2-sx*sx); a1=(n*sxy-sx*sy)/(n*sx2-sx*sx); printf("equation is y=%f+%fx",a0,a1); printf("enter for which vlaue u want to know y"); scanf("%f",&mx); my=a0+a1*mx; printf("%f",my); getch( );

Output: how many sets of data you want to enter5 enter x, y values in sets 13 34 46 67 78 equation is y=2.026316+0.850877x enter for which value u want to know y4 5.429824

Program 35:Aim: A Program for polynomial regression. #include<stdio.h> #include<conio.h> #include<math.h> void main( ) { int n,i,j,k,m; float x[20],y[20],c[20][20],u=0,a[20]; clrscr( ); printf("enter n,m\n"); scanf("%d%d",&n,&m); printf("enter the data\n"); for(i=1;i<=n;i++) scanf("%f%f",&x[i],&y[i]); for(j=1;j<=m+1;j++)
COMPUTER PROGRAMMING LAB MANUAL

91

} for(j=1;j<=m+1;j++) { c[j][m+2]=0; for(i=1;i<=n;i++) { c[j][m+2]+=(y[i]*pow(x[i],(j-1))); } } for(k=1;k<=m+1;k++) { for(i=1;i<=m+1;i++) { if(i!=k) { u=c[i][k]/c[k][k]; for(j=k;j<=m+2;j++) c[i][j]-=(u*c[k][j]); } } } for(i=1;i<=m+1;i++) a[i]=c[i][m+2]/c[i][i]; for(i=1;i<=m+1;i++) printf("a[%d]=%f\n",i-1,a[i]); getch( );

for(k=1;k<=m+1;k++) { c[j][k]=0; for(i=1;i<=n;i++) { c[j][k]=c[j][k]+pow(x[i],(j+k-2)); } }

Output: enter n,m 42 enter the data


COMPUTER PROGRAMMING LAB MANUAL

92

24 3.5 3.8 5.2 5 66 a[0]=5.970579 a[1]=-1.486140 a[2]=0.248912

Program 36(a):Aim: A Program for trapezoidal method. #include<stdio.h> #include<conio.h> #include<math.h> #define f(x) (1/(1+(x*x))) void main( ) { float a,b,sum=0,x,value,h; int n,i; clrscr( ); printf("enter a,b,n values\n");
COMPUTER PROGRAMMING LAB MANUAL

93

scanf("%f%f%d",&a,&b,&n); h=(b-a)/n; x=a; sum=f(a); for(i=1;i<n;i++) { x=x+h; sum+=(2+f(x)); } sum+=f(b); value=(sum*h)/2; printf("the value is %f",value); getch( );

Output: enter a,b,n values 0 1 10 the value is 1.329991

Program 36(b):Aim: A Program to implement Simpson method. #include <stdio.h> #include<conio.h> #include <math.h> #define f(x) (1/(1+x)) void main( ) { int i,n; float a,b,sum=0,x,value,h;
COMPUTER PROGRAMMING LAB MANUAL

94

int n,i; clrscr( ); printf("enter a,b,n value\n"); scanf("%f%f%d",&a,&b,&n); h=(b-a)/n; x=a; sum=f(a); for(i=1;i<n;i++) { x=x+h; if(i%2==0) sum+=(2*f(x)); else sum+=(4*f(x)); } sum+=f(b); value=sum+(h/3); printf("the value is %f",value); getch( );

Output: enter a,b,n values 0 1 10 value=0.693150

COMPUTER PROGRAMMING LAB MANUAL

95

ADDITIONAL EXPERIMENTS

Program 1:Aim: A Program to evaluate the expression (a*x+b)/(a*x-b) #include<stdio.h> #include<conio.h> void main( ) {
COMPUTER PROGRAMMING LAB MANUAL

96

int a,x,b; float res; printf(enter values of a,x,b\n); scanf(%d%d%d,&a,&x,&b); res=(float)(a*x+b)/(a*x-b); printf(result=%f,res); getch( ); } Input: enter values of a,x,b 234 Output: 5

Program 2:Aim: A Program to check whether a number is Armstrong or not. #include<stdio.h> #include<conio.h>
COMPUTER PROGRAMMING LAB MANUAL

97

void main( ) { int r,n,sum=0,tn; printf(enter any number\n); scanf(%d,&n); while(n>0) { r=n%10; sum =sum+r*r*r; n/=10; } if(tn==sum) printf(%d is armstrong,sum); else printf(%d is not Armstrong,sum) getch( );

Input:enter any number 153 Output:153 is Armstrong

Program 3:Aim: A Program to print values of an array in ascending order.

COMPUTER PROGRAMMING LAB MANUAL

98

#include<stdio.h> #include<conio.h> void main( ) { int i,a[10],n,temp; printf(enter size of the array\n); scanf(%d,&n); printf(enter elements\n); for(i=0;i<n;i++) scanf(%d,&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf(Ascending order is\n); for(i=0;i<n;i++) printf(%d ,a[i]); getch( );

} Input:enter size of the array 4 enter elements 4321 Output:Ascending order is 1234

Program 4:Aim: A program to implement Selection sort.


COMPUTER PROGRAMMING LAB MANUAL

99

#include<stdio.h> #include<conio.h> void sel_sort(int x[ ], int n); void main( ) { int x[10],i,j,n; printf(Enter the size of an array \n); scanf(%d,&n); printf(Enter elements :\n); for(i=0;i<n;i++) scanf(%d,&x[i]); sel_sort(x,n); printf(Sorted order is \n); for(i=0;i<n;i++) printf(%d ,x[i]); getch( ); } void sel_sort(int x[ ], int n) { int i,j,index,small; for(i=0;i<n;i++) { small=x[i]; index=i; for(j=i+1;j<n;j++) { if(x[j]<small) { small=x[j]; index=j; } } x[index]=x[i]; x[i]=small; } }

COMPUTER PROGRAMMING LAB MANUAL

100

Input: Enter size of an array 5 Enter elements 2 7 3 9 1 Output: Sorted 0rder is 1 2 3 7 9

COMPUTER PROGRAMMING LAB MANUAL

101

You might also like