Vous êtes sur la page 1sur 112

Aim:To write a C program to find the sum of individual digits of a positive integer. Source code:#include<stdio.h> #include<conio.

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

Output: Enter n1234 sum=10

Aim:To write a program for Fibonacci Sequence, which 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. Source code:#include <stdio.h> void main() { int f1=0, f2=1,n,i=1,f3; clrscr(); printf("Enter how many terms"); scanf("%d",&n); printf("FIBONACCI SERIES"); printf("\n%d\n\n%d",f1,f2); while(i<=n-2) { f3=f1 + f2; printf("\n\n%d",f3); f1=f2; f2=f3; i=i+1; } getch(); } Output:Enter how many terms5 FIBONACCI SERIES 0 1 1 2 3

Aim:To write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. Source code:#include <stdio.h> void main() { int n,i,j,c; clrscr(); printf("Enter the range"); scanf("%d",&n); for(j=2;j<=n;j++) { c=0; for(i=1;i<=j;i++) { if(j%i==0) c=c+1; } if(c==2) printf("%d\t",j); } getch(); } Output:INPUT THE VALUE OF N: 10

THE PRIME NO. SERIES B/W 1 TO 10 : 1 2 3 5 7

Aim: To write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! Source code:#include <stdio.h> #include <math.h> void main() { int n,x,i,j,f; float s=0,t; clrscr(); printf("Enter n,x"); scanf("%d%d",&n,&x); for(i=0;i<=n;i++) { t=pow(x,2*i); f=1; for(j=1;j<=2*i;j++) f=f*j; s=s+pow(-1,i)*(t/f); } printf("Sum=%f",s); getch(); } Output:-

EQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10! ENTER VALUE OF X : 4 SUM : -0.685785

Aim:To Write a C program to find the roots of a quadratic equation. Source code:#include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,root1,root2; clrscr(); printf("\n Enter values of a,b,c for finding roots of a quadratic eq:\n"); scanf("%f%f%f",&a,&b,&c); if(b*b>=4*a*c) { root1=(-b+sqrt((b*b)-4*a*c))/(2*a); root2=(-b-sqrt((b*b)-4*a*c))/(2*a); printf("\n*****ROOTS ARE*****\n"); printf("\n root1=%f\n root2=%f",root1,root2); } else printf("\n Imaginary Roots."); getch(); } Output:-

Enter values of a,b,c for finding roots of a quadratic eq: 1 -2 1 *****ROOTS ARE***** root1=1.000000 root2=1.000000

Aim:To write a C programs that use both recursive and non-recursive functions to find the factorial of a given integer. Source code:#include<stdio.h> #include<conio.h> void main() { int n,i,f=1,ch; clrscr(); while(1) { printf("Enter n value"); scanf("%d",&n); clrscr(); printf("1.Recursive\n2.Non-Recursive\n3.Exit"); printf("\nEnter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: f=rfact(n); printf("%d",f); break; case 2: fact(n); break; case 3: exit(1); default: printf("Invalid choice"); break; } } }

fact(int n) { int f=1; while(n>0) { f=f*n; n=n-1; } printf("%d",f); } int rfact(int n) { if(n==1) return 1; else return n*rfact(n-1); } Output:-

Enter n value 5 1.Recursive 2.Non-Recursive 3.Exit Enter your choice 1 120

Aim:To write C programs that use both recursive and non-recursive functions to find the GCD (greatest common divisor) of two given integers. Source code:#include<stdio.h> #include<conio.h>

void main() { int m,n,g,ch; clrscr(); while(1) { printf("Enter m,n value"); scanf("%d%d",&m,&n); clrscr(); printf("1.Recursive\n2.Non-Recursive\n3.Exit"); printf("\nEnter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: g=rgcd(m,n); printf("%d",g); break; case 2: fgcd(m,n); break; case 3: exit(1); default: printf("Invalid choice"); break; } } } fgcd(int m,int n) {

int i,g; for(i=1;i<=m;i++) { if(m%i==0&&n%i==0) g=i; } printf("%d",g); } int rgcd(int m,int n) { if(m%n==0) return n; else return rgcd(n,m%n); } Output:-

Enter the two numbers whose GCD is to be found: 10 5 GCD of 10 and 5 Using Recursive Function is 5 GCD of 10 and 5 Using Non-Recursive Function is 5

Aim:To write C programs that use both recursive and non-recursive functions to solve Towers of Hanoi problem. Source code:include<conio.h> #include<stdio.h> void hanoi( int n,char sour, char aug, char dest) { if(n!=0) { hanoi( n - 1,sour, dest, aug ); printf( "Move top disk from needle %c to needle %c.\n", sour, aug ); hanoi( n- 1,dest, aug, sour ); } } void main() { int n; clrscr(); printf("Enter the no. of disks to be transferred: "); scanf("%d",&n); hanoi(n,'A','B','C'); getch(); } Output:Enter the no. of disks to be transferred: 2 Move top disk from needle A to needle C. Move top disk from needle A to needle B. Move top disk from needle C to needle B.

Aim:The total distance travelled by vehicle in t seconds is given by distance = ut+1/2at 2 2 where u and a are the initial velocity (m/sec.) and acceleration (m/sec ). 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. Source code:#include <stdio.h> #include <math.h> void main() { int tim_intrval, counter,time; float accl, distance=0, velos; clrscr(); printf("PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL"); printf("\n\n\n\tNO OF TIME INTERVALS : "); scanf("%d",&tim_intrval); for(counter = 1; counter <= tim_intrval; counter++) { printf("\n\tAT T%d TIME(sec) : ",counter); scanf("%d",&time); printf("\tVELOCITY AT %d sec (m/sec) : ",time); scanf("%f",&velos); printf("\tACCLERATION AT %d sec (m/sec^2): ",time); scanf("%f",&accl); distance = (velos*time + (accl*pow(time,2))/2); } printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance); getch(); }

Output:PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL NO OF TIME INTERVALS : 2 AT T1 TIME(sec) : 1 VELOCITY AT 1 sec (m/sec) : 1 ACCLERATION AT 1 sec (m/sec^2): 1 AT T2 TIME(sec) : 1 VELOCITY AT 1 sec (m/sec) : 1 ACCLERATION AT 1 sec (m/sec^2): 1 TOTAL DISTANCE TRAVELLED BY VEHICLE IN 2 INTERVALS OF TIME : 1.500000

Aim:To 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) Source code:#include<stdio.h> #include<conio.h> void main() { int a,b,res,ch; clrscr(); printf("\n\tMENU\n"); printf("\n\t(1)ADDITION"); printf("\n\t(2)SUBTRACTION"); printf("\n\t(3)MULTIPLICATION"); printf("\n\t(4)DIVISION"); printf("\n\t(5)REMAINDER"); printf("\n\t(0)EXIT"); printf("\n\n\tEnter your choice:"); scanf("%d",&ch); if(ch<=5 & ch>0) { printf("Enter two numbers:\n"); scanf("%d%d",&a,&b); } switch(ch) { case 1: res=a+b; printf("\n Addition:%d",res); break; case 2: res=a-b; printf("\n Subtraction:%d",res); break; case 3: res=a*b; printf("\n Multiplication:%d",res);

break; case 4: res=a/b; printf("\n Division:%d",res); break; case 5: res=a%b; printf("\n Remainder:%d",res); break; case 0: printf("\n Choice Terminated"); exit(); break; default: printf("\n Invalid Choice"); } getch(); }

Output:MENU (1)ADDITION (2)SUBTRACTION (3)MULTIPLICATION (4)DIVISION (5)REMAINDER (0)EXIT Enter your choice:1 Enter two numbers: 12 21 Addition:33

Aim:To write a C program to find both the larges and smallest number in a list of integers. Source code:#include<stdio.h> main( ) { int a[30],n,min,max,i; clrscr(); printf("Enter n"); scanf("%d",&n); printf("Enter array elements"); for(i=0;i<=n-1;i++) scanf("%d",&a[i]); min=a[0]; max=a[0]; for(i=1;i<=n-1;i++) { if(min>a[i]) min=a[i]; if(max<a[i]) max=a[i]; } printf("Min=%d,Max=%d",min,max); getch(); } Output:Enter n 5 Enter array elements 5 3 4 8 9 Min=3, Max=9

Aim:To write a C program that uses functions to perform the following: i) Addition of Two Matrices ii) Multiplication of Two Matrices Source code:#include<stdio.h> void main() { int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10]; clrscr(); printf("\n\t\tMENU"); printf("\n[1]ADDITION OF TWO MATRICES"); printf("\n[2]MULTIPLICATION OF TWO MATRICES"); printf("\n[0]EXIT"); printf("\n\tEnter your choice:\n"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Valid Choice\n"); } switch(ch) { case 1: printf("Input rows and columns of A & B Matrix:"); scanf("%d%d",&r1,&c1); printf("Enter elements of matrix A:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter elements of matrix B:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++)

scanf("%d",&b[i][j]); } printf("\n =====Matrix Addition=====\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%5d",a[i][j]+b[i][j]); printf("\n"); } break; case 2: printf("Input rows and columns of A matrix:"); scanf("%d%d",&m,&n); printf("Input rows and columns of B matrix:"); scanf("%d%d",&p,&q); if(n==p) { printf("matrices can be multiplied\n"); printf("resultant matrix is %d*%d\n",m,q); printf("Input A matrix\n"); read_matrix(a,m,n); printf("Input B matrix\n"); /*Function call to read the matrix*/ read_matrix(b,p,q); /*Function for Multiplication of two matrices*/ printf("\n =====Matrix Multiplication=====\n"); for(i=0;i<m;++i) for(j=0;j<q;++j) { c[i][j]=0; for(k=0;k<n;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Resultant of two matrices:\n"); write_matrix(c,m,q); } /*end if*/ else

{ printf("Matrices cannot be multiplied."); } /*end else*/ break; case 0: printf("\n Choice Terminated"); exit(); break; default: printf("\n Invalid Choice"); } getch(); } /*Function read matrix*/ int read_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); return 0; } /*Function to write the matrix*/ int write_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",a[i][j]); printf("\n"); } return 0; }

Output:MENU [1]ADDITION OF TWO MATRICES [2]MULTIPLICATION OF TWO MATRICES [0]EXIT Enter your choice: 1 Valid Choice Input rows and columns of A & B Matrix:3 3 Enter elements of matrix A: 1 2 3 4 5 6 7 8 9 Enter elements of matrix B: 1 2 3 4 5 6 7 8 9 =====Matrix Addition===== 2 4 6 8 10 12 14 16 18

Aim:To write a c program to insert a sub-string in to given main string from a given position Source code:#include <stdio.h> #include <conio.h> #include <string.h> void main() { char a[10],b[10],c[10]; int p=0,r=0,i=0; int t=0; int x,g,s,n,o; clrscr(); puts("Enter First String:"); gets(a); puts("Enter Second String:"); gets(b); printf("Enter the position where the item has to be inserted: "); scanf("%d",&p); r = strlen(a); n = strlen(b); i=0; // Copying the input string into another array while(i <= r) { c[i]=a[i]; i++; } s = n+r; o = p+n; // Adding the sub-string for(i=p;i<s;i++) { x = c[i]; if(t<n) { a[i] = b[t]; t=t+1; }

a[o]=x; o=o+1; } printf("%s", a); getch(); }

Output:Enter First String: hello Enter Second String: hi Enter the position where the item has to be inserted: 0 Hihello

Aim:To delete n Characters from a given position in a given string. Source code:#include <stdio.h> #include <conio.h> #include <string.h> void delchar(char *x,int a, int b); void main() { char string[10]; int n,pos,p; clrscr(); puts("Enter the string"); gets(string); printf("Enter the position from where to delete"); scanf("%d",&pos); printf("Enter the number of characters to be deleted"); scanf("%d",&n); delchar(string, n,pos); getch(); } void delchar(char *x,int a, int b) { if ((a+b-1) <= strlen(x)) { strcpy(&x[b-1],&x[a+b-1]); puts(x); } } Output:Enter the string heloo Enter the position from where to delete1 Enter the number of characters to be deleted2 Loo

Aim:To write a C program to determine if the given string is a palindrome or not Source code:#include<stdio.h> #include<string.h> enum Boolean{false,true}; enum Boolean IsPalindrome(char string[]) { int left,right,len=strlen(string); enum Boolean matched=true; if(len==0) return 0; left=0; right=len-1; while(left<right&&matched) { if(string[left]!=string[right]) matched=false; else { left++; right--; } } return matched; }

int main() { char string[40]; clrscr(); printf("****Program to test if the given string is a palindrome****\n"); printf("Enter a string:"); scanf("%s",string); if(IsPalindrome(string)) printf("The given string %s is a palindrome\n",string);

else printf("The given string %s is not a palindrome\n",string); getch(); return 0; }

Output:-

****Program to test if the given string is a palindrome**** Enter a string:MADAM The given string MADAM is a palindrome

Aim:To 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. Source code:#include<stdio.h> #include<string.h> #include<conio.h> void main() { char s[30], t[20]; char *found; clrscr(); puts("Enter the first string: "); gets(s); puts("Enter the string to be searched: "); gets(t); found=strstr(s,t); if(found) printf("Second String is found in the First String at %d position.\n",found-s); else printf("-1"); getch(); } Output:Enter the first string: GODAVARI INSTITUTE OF ENGINEERING AND TECHNOLOGY Enter the string to be searched: OF Second String is found in the First String at 19 position.

Aim:To write a C program to count the lines, words and characters in a given text. Source code:#include <stdio.h> main() { char line[81], ctr; int i,c, end = 0, characters = 0, words = 0, lines = 0; printf("KEY IN THE TEXT.\n"); printf("GIVE ONE SPACE AFTER EACH WORD.\n"); printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");

while( end == 0) { /* Reading a line of text */ c = 0; while((ctr=getchar()) != '\n') line[c++] = ctr; line[c] = '\0'; /* counting the words in a line */ if(line[0] == '\0') break ; else { words++; for(i=0; line[i] != '\0';i++) if(line[i] == ' ' || line[i] == '\t') words++; } /* counting lines and characters */ lines = lines +1; characters = characters + strlen(line); }

printf ("\n"); printf("Number of lines = %d\n", lines); printf("Number of words = %d\n", words); printf("Number of characters = %d\n", characters); } Output:KEY IN THE TEXT. GIVE ONE SPACE AFTER EACH WORD. WHEN COMPLETED, PRESS 'RETURN'. GODAVARI INSTITUTE OF ENGINEERING & TECHNOLOGY

Number of lines = 1 Number of words = 7 Number of characters = 47

Aim:To write a C program to generate Pascals triangle. Source code:#include<stdio.h> #include<conio.h> void main() { int bin,p,q,r,x; clrscr(); bin=1; q=0; printf("Rows you want to input:"); scanf("%d",&r); printf("\nPascal's Triangle:\n"); while(q<r) { for(p=40-3*q;p>0;--p) printf(" "); for(x=0;x<=q;++x) { if((x==0)||(q==0)) bin=1; else bin=(bin*(q-x+1))/x; printf("%6d",bin); } printf("\n"); ++q; } getch(); }

Output:Rows you want to input:5 Pascal's Triangle: 1 1 1 1 1 4 3 6 2 3 4 1 1 1 1

Aim:To write a C program to construct a pyramid of numbers. Source code:#include<stdio.h> #include<conio.h> void main() { int num,i,y,x=35; clrscr(); printf("\nEnter the number to generate the pyramid:\n"); scanf("%d",&num); for(y=0;y<=num;y++) { /*(x-coordinate,y-coordinate)*/ gotoxy(x,y+1); /*for displaying digits towards the left and right of zero*/ for(i=0-y;i<=y;i++) printf("%3d",abs(i)); x=x-3; } getch(); } Output:-

Enter the number to generate th 5 0 1 0 1: 2 1 0 1 2 3 2 1 0 1 2 3 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5

Aim:To 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 Source code:#include<stdio.h> #include<conio.h> #include<math.h> void main() { int s_sum,i,x,n; clrscr(); printf("Enter the values for x and n:"); scanf("%d %d",&x,&n); if(n<=0 || x<=0) { printf("Value is not valid\n"); } else { printf("Value is valid\n"); s_sum=1; for(i=1;i<=n;i++) { s_sum=s_sum+pow(x,i); } printf("Sum of series=%d\n",s_sum); } getch(); } Output:Enter the values for x and n:5 3 Value is valid Sum of series=156

Aim:To write a program for 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. Source code:#include <stdio.h> #include<conio.h> void complement (char *a); void main() { char a[16]; int i; clrscr(); printf("Enter the binary number"); gets(a); for(i=0;a[i]!='\0'; i++) { if (a[i]!='0' && a[i]!='1') { printf("The number entered is not a binary number. Enter the correct number"); exit(0); } } complement(a); getch(); } void complement (char *a) { int l, i, c=0; char b[16]; l=strlen(a); for (i=l-1; i>=0; i--) { if (a[i]=='0') b[i]='1'; else b[i]='0';

} for(i=l-1; i>=0; i--) { if(i==l-1) { if (b[i]=='0') b[i]='1'; else { b[i]='0'; c=1; } } else { if(c==1 && b[i]=='0') { b[i]='1'; c=0; } else if (c==1 && b[i]=='1') { b[i]='0'; c=1; } } } b[l]='\0'; printf("The 2's complement is %s", b); } Output:Enter the binary number 111111 The 2's complement is 000001

Aim:To write a C program to convert a Roman numeral to its decimal equivalent. Source code:#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int *a,len,i,j,k; char *rom; clrscr(); printf("Enter the Roman Numeral:"); scanf("%s",rom); len=strlen(rom); for(i=0;i<len;i++) { if(rom[i]=='I') a[i]=1; else if(rom[i]=='V') a[i]=5; else if(rom[i]=='X') a[i]=10; else if(rom[i]=='L') a[i]=50; else if(rom[i]=='C') a[i]=100; else if(rom[i]=='D') a[i]=500; else if(rom[i]=='M') a[i]=1000; else {

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

Aim:To 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 Source code:#include<stdio.h> #include<math.h> void arithmetic(int opern); struct comp { double realpart; double imgpart; }; void main() { int opern; clrscr(); printf("\n\n \t\t\t***** MAIN MENU *****"); printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b"); scanf("%d",&opern); switch(opern) { case 0: exit(0); case 1: case 2: arithmetic(opern); default: main(); } } void arithmetic(int opern) { struct comp w1, w2, w;

printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:"); scanf("%lf",&w1.realpart); printf("\n Imaginary Part of First Number:"); scanf("%lf",&w1.imgpart); printf("\n Real Part of Second Number:"); scanf("%lf",&w2.realpart); printf("\n Imaginary Part of Second Number:"); scanf("%lf",&w2.imgpart); switch(opern) { case 1: w.realpart = w1.realpart+w2.realpart; w.imgpart = w1.imgpart+w2.imgpart; break; case 2: w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart); w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart); break; } if (w.imgpart>0) printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart); else printf("\n Answer = %lf%lfi",w.realpart,w.imgpart); getch(); main(); } Output:***** MAIN MENU ***** Select your option: 1 : ADD 2 : MULTIPLY 0 : EXIT Enter your Option [ 1] Enter two Complex Numbers (x+iy): Real Part of First Number:2 Imaginary Part of First Number:2 Real Part of Second Number:3 Imaginary Part of Second Number:3 Answer = 5.000000+5.000000i

Aim:To write a C program which copies one file to another. Source code:#include <stdio.h> #include <conio.h> #include <ctype.h> main() { FILE *f1,*f2; char ch; f1=fopen("roman.c","r"); f2=fopen("file.c","w"); if(f1==NULL) printf("file not found"); else while((ch==fgetc(f1))!=EOF) { fprintf(f2,"%c",ch); } fclose(f2); fclose(f1); getch(); } Output:I have already created file hai.c. The content of hai.c is Hai this is C&DS record After executing above program the content of hai.c is copied into the file hello.c and there is no any Output display on console window. After executing program see the file hello.c then you will find the content on hai.c is copied into hello.c

Aim:To write a C program to reverse the first n characters in a file. Source code:#include <stdio.h> #include <conio.h> #include <string.h> #include <process.h> void main(int argc, char *argv[]) { char a[15],s[20],n; int k, j=0,i,len; FILE *fp; if(argc!=3) { puts("Improper number of arguments."); exit(0); } fp = fopen(argv[1],"r"); if(fp == NULL) { puts("File cannot be opened."); exit(0); } k=*argv[2]-48; n = fread(a,1,k,fp); a[n]='\0'; 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(); } Output:sad.c GIET College C:\TURBOC2>filerev sad.c das.c egelloc teiG

Aim:To write a C program that uses functions to perform the following operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal Source code:#include<stdio.h> struct node { int data; struct node *next; }; struct node *start=NULL,*end=NULL,*present,*new; main() { int ch; do { clrscr(); printf("\n1. Create\n2. Insert\n3. Delete\n4. Display"); printf("\n5. Exit.\n\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: create();break; case 2: insert(); break; case 3: delete();printf("\nPress a key..");getch(); break; case 4: display();printf("\nPress a key..");getch(); break; case 5: exit(0); } }while(1); } create() { char ch; do { clrscr();

new=(struct node *)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->next=NULL; if(start==NULL) start=end=new; else { end->next=new; end=new; } printf("\nDo you want to continue (y/n): "); fflush(stdin); scanf("%c",&ch); }while(ch=='y'); } insert() { int ch,pos,i=1; clrscr(); printf("\n\tInsert\n"); printf("\n1. At Begining\n2. At any Position\n3. At End."); printf("\n\nYour Choice: "); scanf("%d",&ch); new=(struct node *)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->next=NULL; switch(ch) { case 1: new->next=start; start=new; break; case 2: printf("\nPosition: "); scanf("%d",&pos); present=start; while(i<pos-1 && present!=NULL) { present=present->next; i++;

} new->next=present->next; present->next=new; break; case 3: end->next=new; end=new; break; } } delete() { int ch,pos,i=1; struct node *t; clrscr(); printf("\n\tDelete\n"); printf("\n1. At Begining\n2. At any Position\n3. At End."); printf("\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nDeleted Item: %d",start->data); start=start->next; break; case 2: printf("\nPosition: "); scanf("%d",&pos); present=start; while(i<pos-1 && present!=NULL) { present=present->next; i++; } t=present->next; printf("\nDeleted Item: %d",t->data); present->next=t->next; break; case 3: present=start; while(present->next!=end)

{ present=present->next; } printf("\nDeleted Item: %d",end->data); present->next=NULL; end=present; break; } } display() { present=start; if(start==NULL) printf("\nList is empty."); else { while(present!=NULL) { printf(" %d ",present->data); present=present->next; } } } Output:-

1. Create 2. Insert 3. Delete 4. Display 5. Exit. Your Choice:1

Element: 5 Do you want to continue (y/n):y

Insert 1. At Begining 2. At any Position 3. At End. Your Choice:1 6 1. Create 2. Insert 3. Delete 4. Display 5. Exit.

Your Choice: 4 65

Aim:To write a C program that uses functions to perform the following operations on doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways Source code:#include<stdio.h> struct node { int data; struct node *prev; struct node *next; }; struct node *start=NULL,*end=NULL; struct node *present,*new; main() { int ch; do { clrscr(); printf("\n1. Create\n2. Insert"); printf("\n3. Delete\n4. Display"); printf("\n5. Exit.\n\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1:create();break; case 2:insert(); break; case 3:delete(); printf("\nPress a key."); getch(); break; case 4:display(); printf("\nPress a key."); getch(); break; case 5: exit(0); } }while(1); }

create() { char ch; do { clrscr(); new=(struct node*)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->prev=NULL; new->next=NULL; if(start==NULL) start=end=new; else { end->next=new; new->prev=end; end=new; } printf("\nContinue (y/n): "); fflush(stdin); scanf("%c",&ch); }while(ch=='y'); } insert() { int ch,pos,i=1; char ch1; struct node *t; clrscr(); printf("\n\tInsert\n"); printf("\n1. At Begining\n2. At any Position"); printf("\n3. At End."); printf("\n\nYour Choice: "); scanf("%d",&ch); new=(struct node*)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->prev=NULL; new->next=NULL;

switch(ch) { case 1: new->next=start; start->prev=new; start=new; break; case 2: printf("\nPosition: "); scanf("%d",&pos); printf("\n\nFrom:\n\tf. forward. \n\tr. reverse."); printf("\nYour Choice: "); fflush(stdin); scanf("%c",&ch1); if(ch1=='f') { present=start; while(i<pos-1 && present!=NULL) { present=present->next; i++; } new->prev=present; new->next=present->next; t=present->next; t->prev=new; present->next=new; } else if(ch1=='r') { present=end; while(i<pos-1 && present!=NULL) { present=present->prev; i++; } new->next=present; new->prev=present->prev; t=present->prev; t->next=new; present->prev=new;

} else printf("\nInvalid Option."); getch(); break;

case 3: end->next=new; new->prev=end; end=new; break; } } delete() { int ch,pos,i=1; char ch1; struct node *t,*t1; clrscr(); printf("\n\tDelete\n"); printf("\n1. At Begining\n2. At any Position\n3. At End."); printf("\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nDeleted Item: %d",start->data); start=start->next; start->prev=NULL; break; case 2: printf("\nPosition: "); scanf("%d",&pos); printf("\n\nFrom:\n\tf. forward. \n\tr. reverse."); fflush(stdin); scanf("%c",&ch1); if(ch1=='f') { present=start; while(i<pos-1 && present!=NULL)

{ present=present->next; i++; } t=present->next; printf("\nDeleted Item: %d",t->data); present->next=t->next; t1=t->next; t1->prev=present; } else if(ch1=='r') { present=end; while(i<pos-1 && present!=NULL) { present=present->prev; i++; } t=present->prev; printf("\nDeleted Item: %d",t->data); present->prev=t->prev; t1=t->prev; t1->next=present; } else printf("\nInvalid Option.");getch(); break; case 3: printf("\nDeleted Item: %d",end->data); end=end->prev; end->next=NULL; break; } } display() { char ch1; printf("\n\nFrom:\n\tf. forward. \n\tr. reverse."); printf("\nYour Choice: ");

fflush(stdin); scanf("%c",&ch1); if(ch1=='f') { present=start; if(start==NULL) printf("\nList is empty."); else { while(present!=NULL) { printf(" %d",present->data); present=present->next; } } } else if(ch1=='r') { present=end; while(present!=NULL) { printf(" %d ",present->data); present=present->prev; } } else printf("\nTry Again."); } Output:-

1. Create 2. Insert 3. Delete 4. Display 5. Exit. Your Choice:1

Element: 5

Do you want to continue (y/n):y Insert 1. At Begining 2. At any Position 3. At End. Your Choice:1 6 1. Create 2. Insert 3. Delete 4. Display 5. Exit. Your Choice: 4 a. forward b. backward a 65

Aim:To write C programs that implement stack (its operations) using Arrays Source code:#include<stdio.h> int a[5],top=-1; main() { int choice; do { clrscr(); printf("\n1.Push.\n2. Pop\n3.Display.\n4.Exit."); printf("\nYour Choice: "); scanf("%d",&choice); switch(choice) { case 1:push();break; case 2:pop();break; case 3: display(); printf("\nPress a key to continue..."); getch();break; case 4: exit(0); } }while(1); } push() { if(top==4) { printf("Stack is overflow."); printf("\nPress a key to continue.."); getch(); } else { printf("\nElement: "); top=top+1; scanf("%d",&a[top]); }

} pop() { if(top==-1) printf("\nStack is underflow."); else { printf("\nDeleted Item: %d",a[top]); top=top-1; } printf("\nPress a key to continue.."); getch(); } display() { int i; if(top==-1) printf("\nStack is empty."); else { printf("\nElements are : \n"); for(i=0;i<=top;i++) printf(" %d ",a[i]); } } Output:1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 1 Element: 1 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice:

1 Element:2 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 3 Elements are : 1 5 Press a key to continue... 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 2 Deleted Item: 5 Press a key to continue.. 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 3

Elements are : 1 Press a key to continue... 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice4

Aim:To write C programs that implement stack (its operations) using potinters Source code:#include<stdio.h> struct node { int data; struct node *next; }; struct node *start=NULL,*top=NULL; struct node *present,*new; main() { int ch; do { clrscr(); printf("\n1. Push\n2. Pop\n3. Display"); printf("\n4. Exit.\n\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: push();break; case 2: pop(); printf("\nPress a key.."); getch(); break; case 3: display(); printf("\nPress a key.."); getch(); break; case 4: exit(0); } }while(1); } push() {

new=(struct node*)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->next=NULL; if(start==NULL) start=top=new; else { top->next=new; top=new; } } pop() { if(start==NULL) printf("\nStack is empty."); else { printf("\nDeleted : %d",top->data); present=start; if(start==top) start=top=NULL; else { while(present->next!=top) { present=present->next; } present->next=NULL; top=present; } } } display() { present=start; if(start==NULL) printf("\nStack is empty."); else {

while(present!=NULL) { printf(" %d",present->data); present=present->next; } } } Output:1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 1 Element: 1 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 1 Element:2 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 3 Elements are : 1 5 Press a key to continue... 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice: 2 Deleted Item: 5 Press a key to continue.. 1.Push.

2. Pop 3.Display. 4.Exit. Your Choice: 3 Elements are : 1 Press a key to continue... 1.Push. 2. Pop 3.Display. 4.Exit. Your Choice4

Aim:To write C programs that implement Queue (its operations) using Arrays Source code:#include<stdio.h> int a[5],rear=-1,front=-1; main() { int choice; do { clrscr(); printf("\n1.Insert.\n2. Delete\n3.Display.\n4.Exit."); printf("\nYour Choice: "); scanf("%d",&choice); switch(choice) { case 1:insert();break; case 2:delete();break; case 3: display(); printf("\nPress a key to continue..."); getch();break; case 4: exit(0); } }while(1); } insert() { if(rear==4) { printf("Queue is full."); printf("\nPress a key to continue.."); getch(); } else { printf("\nElement: "); rear=rear+1; scanf("%d",&a[rear]);

} } delete() { if(rear==front) printf("\nQueue is empty."); else { front=front+1; printf("\nDeleted Item: %d",a[front]); } printf("\nPress a key to continue.."); getch(); } display() { int i; if(rear==front) printf("\nQueue is empty."); else { printf("\nElements are : \n"); for(i=front+1;i<=rear;i++) printf(" %d ",a[i]); } } Output:-

1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice:1 Element:5 1.Insert. 2. Delete 3.Display. 4.Exit.

Your Choice:1 Element:4 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice:1 Element:3 Elements are : 5 4 3 Press a key to continue... 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice: 2 Your Choice: 2 Deleted Item: 5 Press a key to continue.. 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice: 4

Aim:To write C programs that implement Queue (its operations) using Pointers Source code:#include<stdio.h> struct node { int data; struct node *next; }; struct node *rear=NULL,*front=NULL; struct node *present,*new; main() { int ch; do { clrscr(); printf("\n1. Insert\n2. Delete"); printf("\n3. Display\n4. Exit."); printf("\n\n\nYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: insert();break; case 2: delete(); printf("\nPress a key.."); getch(); break; case 3: display(); printf("\nPress a key.."); getch(); break; case 4: exit(0); } }while(1); } insert() {

new=(struct node*)malloc(sizeof(struct node)); printf("\nElement: "); scanf("%d",&new->data); new->next=NULL; if((rear==NULL)||(front==NULL)) rear=front=new; else { rear->next=new; rear=new; } } delete() { if(front==NULL) printf("\nQueue is empty."); else { printf("\nDeleted : %d",front->data); front=front->next; } } display() { present=front; if((front==NULL)||(rear==NULL)) printf("\nQueue is empty."); else { while(present!=NULL) { printf(" %d",present->data); present=present->next; } } }

Output:1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice:1 Element:5 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice:1 Element:4 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice:1 Element:3 Elements are : 5 4 3 Press a key to continue... 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice: 2 Your Choice: 2 Deleted Item: 5 Press a key to continue.. 1.Insert. 2. Delete 3.Display. 4.Exit. Your Choice: 4

Aim:To write a C program that uses Stack operations to perform the following: i) Converting infix expression into postfix expression ii) Evaluating the postfix expression

Source code:#include<stdio.h> #include<conio.h> int st[100]; int st_top=-1; int cal(char post[]); void in_post(char in[]); void push_item(int it); int pop_item(); int st_ISP(char t); int st_ICP(char t); /*main function*/ void main() { char in[100],post[100]; clrscr(); printf("\n\tEnter the Infix Expression: "); gets(in); in_post(in); getch(); } /*end main*/ void push_item(int it) { if(st_top==99) { printf("\n\n\t*STACK is Full*"); getch(); exit(1); } st[++st_top]=it; } int pop_item()

{ int it; if(st_top==-1) { getch(); } return(st[st_top--]); } /*Function for converting an infix expression to a postfix expression. */ void in_post(char in[]) { int x=0,y=0,z,result=0; char a,c, post[100]; char t; push_item('\0'); t=in[x]; while(t!='\0') { if(isalnum(t)) /*For checking whether the value in t is an alphabet or number. */ { post[y]=t; y++; } else if(t=='(') { push_item('('); } else if(t==')') { while(st[st_top]!='(') { c=pop_item(); post[y]=c; y++; } c=pop_item(); } else {

while(st_ISP(st[st_top])>=st_ICP(t)) { c=pop_item(); post[y]=c; y++; } push_item(t); } x++; t=in[x]; } while(st_top!=-1) { c=pop_item(); post[y]=c; y++; } printf("\n\tThe Postfix Expression is:"); for(z=0;z<y;z++) printf("%c",post[z]); printf("\n\nDo you want to evaluate the Result of Postfix Expression?(Y/N):"); scanf("%c",&a); if(a=='y' || a=='Y') { result=cal(post); printf("\n\n\tResult is: %d\n",result); getch(); } else if(a=='n' || a=='N') { exit(0); } } /*Determining priority of inside elements*/ int st_ISP(char t) { switch(t) { case '(':return (10);

case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Expression is invalid."); break; } return 0; } /*Determining priority of approaching elements*/ int st_ICP(char t) { switch(t) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Expression is invalid."); break; } return 0; } /*Evaluating the result of postfix expression*/ int cal(char post[]) { int m,n,x,y,j=0,len; len=strlen(post); while(j<len) { if(isdigit(post[j])) { x=post[j]-'0'; push_item(x); }

else { m=pop_item(); n=pop_item(); switch(post[j]) { case '+':x=n+m; break; case '-':x=n-m; break; case '*':x=n*m; break; case '/':x=n/m; break; } push_item(x); } j++; } if(st_top>0) { printf("Number of Operands are more than Operators."); exit(0); } else { y=pop_item(); return (y); } return 0; } Output:Enter an expression in postfix notation 68+92-/ The evaluation of given expression is 2.0000000

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

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

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

temp_node2->r_child = (struct treenode*)malloc(sizeof(struct treenode)); temp_node2 = temp_node2->r_child; if(temp_node2 == NULL) { printf("Value cannot be allocated.\n"); exit(0); } temp_node2->ele = a; temp_node2->l_child=temp_node2->r_child = NULL; } } return(t); } /* Function for Traversing the binary tree in inorder. */ void TraverseInorder(struct treenode *t) { if(t != NULL) { TraverseInorder(t->l_child); printf("%d\t",t->ele); in_order(t->r_child); } } /* Function for Traversing the binary tree in preorder. */ void TraversePreorder(struct treenode *t) { if(t != NULL) { printf("%d\t",t->ele); TraversePreorder(t->l_child); TraversePreorder(t->r_child); } } /* Function for Traversing the binary tree in postorder. */ void TraversePostorder(struct treenode *t) {

if(t != NULL) { TraversePostorder(t->l_child); TraversePostorder(t->r_child); printf("%d\t",t->ele); } } Output:Enter the number of node:6 Enter the data value:12 Enter the data value:10 Enter the data value:14 Enter the data value:9 Enter the data value:7 Enter the data value:6 ---------------------------MENU ---------------------------[1] Create a Binary Tree and use Traversal [2] Create a Binary Tree and use Traversal [3] Create a Binary Tree and use Traversal [4]exit ----------------------------Enter your choice:1 Binary tree using Inorder Traversal:6 7 9 10 12 14 ---------------------------MENU ---------------------------[1] Create a Binary Tree and use Traversal [2] Create a Binary Tree and use Traversal [3] Create a Binary Tree and use Traversal [4]exit ----------------------------Enter your choice:2 Binary tree using Preorder Traversal: 12 10 9 7 6 14 ---------------------------MENU ----------------------------

[1] Create a Binary Tree and use Traversal [2] Create a Binary Tree and use Traversal [3] Create a Binary Tree and use Traversal [4]exit ----------------------------Enter your choice:3 Binary tree using Preorder Traversal: 6 7 9 10 14 12 ---------------------------MENU ---------------------------[1] Create a Binary Tree and use Traversal [2] Create a Binary Tree and use Traversal [3] Create a Binary Tree and use Traversal [4]exit ----------------------------Enter your choice:4

Aim:To write C programs to perform the following searching operations for a Key value in a given list of integers : Linear search Source code:#include<stdio.h> main() { int a[10],i,n,f=0; clrscr(); printf("enter the elements int array:\n"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("ente which element you want to search"); scanf("%d",&n); for(i=0;i<10;i++) { if(n==a[i]) { f=1; printf("%d is found",n); break; } } if (f==0) printf("not found"); getch(); } Output:enter the elements int array: 1 2 3 4 5 6 7 8 9 10 ente which element you want to search6 6 is found

Aim:To write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : Binary search Source code:#include <stdio.h> #define MAX_LEN 10 /* Non-Recursive function*/ void b_search_nonrecursive(int l[],int num,int ele) { int l1,i,j, flag = 0; l1 = 0; i = num-1; while(l1 <= i) { j = (l1+i)/2; if( l[j] == ele) { printf("\nThe element %d is present at position %d in list\n",ele,j); flag =1; break; } else if(l[j] < ele) l1 = j+1; else i = j-1; } if( flag == 0) printf("\nThe element %d is not present in the list\n",ele); } /* Recursive function*/ int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a) { int m,pos; if (arrayStart<=arrayEnd) {

m=(arrayStart+arrayEnd)/2; if (l[m]==a) return m; else if (a<l[m]) return b_search_recursive(l,arrayStart,m-1,a); else return b_search_recursive(l,m+1,arrayEnd,a); } return -1; } void read_list(int l[],int n) { int i; printf("\nEnter the elements:\n"); for(i=0;i<n;i++) scanf("%d",&l[i]); } void print_list(int l[],int n) { int i; for(i=0;i<n;i++) printf("%d\t",l[i]); } /*main function*/ void main() { int l[MAX_LEN], num, ele,f,l1,a; int ch,pos; clrscr(); printf("======================================================"); printf("\n\t\t\tMENU"); printf("\n====================================================="); printf("\n[1] Binary Search using Recursion method"); printf("\n[2] Binary Search using Non-Recursion method"); printf("\n\nEnter your Choice:");

scanf("%d",&ch); if(ch<=2 & ch>0) { printf("\nEnter the number of elements : "); scanf("%d",&num); read_list(l,num); printf("\nElements present in the list are:\n\n"); print_list(l,num); printf("\n\nEnter the element you want to search:\n\n"); scanf("%d",&ele);

switch(ch) { case 1:printf("\nRecursive method:\n"); pos=b_search_recursive(l,0,num,ele); if(pos==-1) { printf("Element is not found"); } else { printf("Element is found at %d position",pos); } getch(); break; case 2:printf("\nNon-Recursive method:\n"); b_search_nonrecursive(l,num,ele); getch(); break; } } getch(); }

Output:====================================================== MENU ===================================================== [1] Binary Search using Recursion method [2] Binary Search using Non-Recursion method Enter your Choice:1 Enter the number of elements : 5 Enter the elements: 1 2 3 4 5 Elements present in the list are: 1 2 3 4 5

Enter the element you want to search: 5 Recursive method: Element is found at 4 position

Aim:To write C programs that implement the following sorting methods to sort a given list of integers in ascending order: Bubble sort Source code:#include<stdio.h> main() { int a[5],i,j,t; clrscr(); printf("\n enter the elements into array:\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<4;i++) { for(j=i;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\n after sorting:\n"); for(i=0;i<5;i++) { printf("\n%d",a[i]); } getch(); } Output:- enter the elements into array:10 13 24 35 44 after sorting: 10 13 24 35 44

Aim:To write C programs that implement the following sorting methods to sort a given list of integers in ascending order: Quick sort Source code:#include<stdio.h> main() { int a[5],i; clrscr(); printf("enter the elements:\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); sort(a,0,4); printf("\nafter sorting\n"); for(i=0;i<5;i++) printf("\n%d",a[i]); getch(); } sort(int x[],int left,int right) { int p,l,r; l=left; r=right; p=x[left]; while(left<right) { while((x[right]>=p) && (left<right)) right--; if(left!=right) { x[left]=x[right]; left++; } while((x[left]<=p) && (left<right)) left++; if(left!=right)

{ x[right]=x[left]; right--; } } x[left]=p; p=left; left=l; right=r; if(left<p) sort(x,left,p-1); if(right>p) sort(x,p+1,right); } Output:enter the elements: 45 95 12 46 78 after sorting 12 45 46 78 95

Aim:To write C programs that implement the following sorting methods to sort a given list of integers in ascending order:Insertion sort Source code:#include<stdio.h> #include<conio.h> main() { int a[5],i,j,t; clrscr(); printf("enter the elements\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=1;i<5;i++) { t=a[i]; j=i; while((j>0) && (a[j-1]>t)) { a[j]=a[j-1]; j=j-1; } a[j]=t; }

printf("\n after sorting the elements are\n"); for(i=0;i<5;i++) printf("\n%d",a[i]); getch(); } Output:enter the elements 78 45 65 12 19 after sorting the elements are 12 19 45 65 78

Aim:To write C programs that implement the following sorting methods to sort a given list of integers in ascending order: Merge sort Source code:#include<stdio.h> main() { int a[4],b[5],c[9],i,j,k,t; clrscr(); printf("enter the elements into array:A\n"); for(i=0;i<4;i++) scanf("%d",&a[i]); for(i=0;i<4;i++) { for(j=i+1;j<4;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\n enter the elements into array:B\n"); for(i=0;i<5;i++) scanf("%d",&b[i]); for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(b[i]>b[j]) { t=b[i]; b[i]=b[j]; b[j]=t; } }

} i=0;j=0;k=0; while(i<4 && j<5) { if(a[i]<b[j]) { c[k]=a[i]; i++; } else { c[k]=b[j]; j++; } k++; } while(i<4) { c[k]=a[i]; i++; k++; } while(j<5) { c[k]=b[j]; j++; k++; } printf("\n after sorting the elements are:\n"); for(k=0;k<9;k++) printf("\n%d",c[k]); getch(); } Output:enter the elements into array:A 13 56 98 12

enter the elements into array:B 95 46 19 37 54 after sorting the elements are: 12 13 19 37 46 54 56 95 98

Aim:To write C programs to implement the Lagrange interpolation. Source code:#include<stdio.h> #include<conio.h> #define MaxN 90 void main() { float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0; int i, j, n; clrscr(); printf("Enter the value of n: \n"); scanf("%d", &n); printf("Enter the values of x and y: \n"); for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated: "); scanf("%f", &x); for (i=0; i<=n; i++) { 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:2 Enter values of c and y: 2 3 4 5 6 7 Enter the values of x at which value of y is to be calculated:4 When x=4.0 y=5.0

Aim:To write C programs to implement the Newton- Gregory forward interpolation. Source code:#include<stdio.h> #include<conio.h> #define MaxN 100 #define Order_of_diff 4 void main () { float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1]; int i,j,n,k; clrscr(); printf("Enter the value of n \n"); scanf("%d",&n); printf("Enter the values of x and y"); for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated"); scanf("%f", &x); h=arr_x[1]-arr_x[0]; for(i=0; i<=n-1; i++) diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/ for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/ for(i=0; i<=n-j; i++) diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1]; i=0; while(!(arr_x[i]>x)) /* Finding x0 */ i++; i--; p=(x-arr_x[i])/h; y=arr_y[i];

for (k=1; k<=Order_of_diff; k++) { numerator *=p-k+1; denominator *=k; y +=(numerator/denominator)*diff_table[i][k]; } printf("When x=%6.1f, y=%6.2f\n",x, y); getch(); } Output:Enter the value of n:2 Enter the value of x and y: 2 3 4 5 6 7 Enter the value of x at which value of y is to be calculated:4 When x=4.0 y=5.0

Aim:To write C programs to implement the linear regression algorithm. Source code:#include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the values of x and y:\n"); for(i=0;i<n;i++) scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i<n;i++) sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy); printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1)

{ reg_coff_xy=corr_coff*(sx/sy); printf("\nThe value of linear regression coefficient is %f",reg_coff_xy); } else if(strcmp(type_coff,"y on x")==1) { reg_coff_yx=corr_coff*(sy/sx); printf("\nThe value of linear regression coefficient is %f",reg_coff_yx); } else printf("\nEnter the correct type of regression coefficient."); getch(); }

float mean(float *a, int n) { float sum=0, i=0; for(i=0;i<n;i++) sum=sum+a[i]; sum=sum/n; return (sum); } void deviation(float *a, float mean, int n, float *d, float *s) { float sum=0,t=0; int i=0; 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 x,y values:2 4 5 6 Enter type of regression coefficient as x on y or y on x: y on x The value of linear regression coefficient is 1.5000

Aim:To write C programs to implement Trapezoidal method. Source code:#include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of subintervals"); scanf("%f%f%d", &x0, &xn, &n); h=(xn-x0)/n;

if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn);

s=log(e1)+log(e2); for (i=1;i<=n-1;i++) s+=2*log(eval(postfix,x0+i*h)); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn); for (i=1;i<=n-1;i++) s+=2*eval(postfix,x0+i*h); } printf("Value of the integral is %6.3f\n",(h/2)*s); return(0); } /*Inserting the operands in a stack. */

void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; }

/*Removing the operands from a stack. */

float pop() {

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

top1--; return (item); }

/*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); }

else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); /*Assigning the popped element into the postfix array. */ postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='\0'; } int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); break; }

return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Invalid expression"); break; } return 0; }

/*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k);

} else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); break; } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); } return 0; } Output:-

Enter expression:1/(1+x) Enter x0,xn and no.of sub-intervals:0 1 2 The value of integral is 0.777ss

Aim:To write C programs to implement Simpson methods. Source code:#include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2, e3; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of sub-intervals: "); scanf("%f%f%d", &x0, &xn, &n);

h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); e3=4*eval(postfix, x0+h); s=log(e1)+log(e2)+log(e3);

for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } printf("The value of integral is %6.3f\n",(h/3)*s);

return(0); }

/*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; }

/*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("\n\tThe stack is empty\n\t"); getch(); } item=stack[top];

top--; return (item); } void push1(char item) { if(top1==79) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top1++; stack1[top1]=item; } return; }

/*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("\n\tThe stack1 is empty\n\t"); getch();

} item=stack1[top1]; top1--; return (item); }

/*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('(');

} else if(token==')') { while(stack1[top1]!='(') { ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++;

} postfix[j]='\0'; }

/*Determining the priority of elements that are placed inside the stack. */ int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); } return 0; }

/*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) {

case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Invalid expression"); } return 0; }

/*Calculating the result of expression, which is converted in postfix notation. */

float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else

{ t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop();

return (r); } return 0; } Output:Enter expression:1(1+x) Enter x0,xn and no.of sub-intervals:2 10 8 The value of integral is 1300

Vous aimerez peut-être aussi