Professional Documents
Culture Documents
Output
Submitted To
Submitted By
Ms Sumita Gupta
Index
S. No
Program
Date
Sign
Program 2
Program to implement bubble sort
Source Code //PROGRAM TO IMPLEMENT BUBBLE SORT #include <stdio.h> #include <conio.h> void main( ) { clrscr(); int arr[20],n=0,i,j,temp=0 ; printf("\t\t PROGRAM TO IMPLEMENT BUBBLE SORT-----BY SUGANDHA BHATIA"); printf("\n Enter the number of elements :"); scanf("%d",&n); printf("\nEnter the array elements :\n"); for( i=0;i<n;i++) scanf("%d",&arr[i]); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < (n - 1)-i ; j++ ) { if ( arr[j] > arr[j + 1] ) { temp = arr[j] ; arr[j] = arr[j + 1] ; arr[j + 1] = temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i < n ; i++ ) printf ( "%d\t", arr[i] ) ; getch(); }
Output
Program 3
Program to implement selection sort Source Code //Program to implement selection sort #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[20],i,j,n,x=0,small=0,temp=0; printf("\tProgram to implement selection sort-----By Sugandha Bhatia\n"); printf("\n\nEnter the number of elements :"); scanf("%d",&n); printf("\nEnter array elements :\n") ; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { x=a[i]; small=i; for(j=i+1;j<n;j++) { if(x>a[j]) { x=a[j]; small=j; } } temp=a[i]; a[i]=x; a[small]=temp; } printf("Elements after sorting are :\n"); for(i=0;i<n;i++) { printf("%d",a[i]); printf("\t");
} getch(); } Output
Program 4
Program to implement linear search Source Code //Program to implement linear search #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[20],n=0,ele=0,i=0,pos=0,flag=0; printf("\n\tProgram to implement linear search----By Sugandha Bhatia\n"); printf("\nEnter the size of array :"); scanf("%d",&n); printf("\nEnter the array elements:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the element to be searched :"); scanf("%d",&ele); for(i=0;i<n;i++) { if(a[i]==ele) { pos=i+1; flag=1; } } if(flag==1) printf("Element found at position %d",pos); else printf("Element not found !!"); getch(); }
Output
Program 5
Program to implement binary search without using recursion Source code //Program to implement binary search #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[20],n=0,ele=0,beg=0,mid=0,last=0,i=0,pos=0; printf("\n\tProgram to implement binary search----By Sugandha Bhatia\n"); printf("\nEnter the size of array :"); scanf("%d",&n); printf("\nEnter the array elements:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the element to be searched :"); scanf("%d",&ele); beg=0; last=n-1; for(i=0;i<n;i++) { mid=(beg+last)/2 ; if(ele>a[mid]) { beg=mid+1; } else if(ele<a[mid]) { last=mid-1; } else if(ele==a[mid]) { pos=mid; } } if(ele==a[mid]) printf("Element found at position %d",pos+1); else printf("Element not found !!!") ; getch(); }
Output
Program 6
Program to implement binary search using recursion Source code #include<stdio.h> #include<conio.h> int binarysearch(int a[],int n,int beg,int last) { int mid; if (beg > last) return -1; mid = (beg + last)/2; if(n == a[mid]) { printf("The element is at position %d\n",mid+1); return 0; } if(n < a[mid]) { last = mid - 1; binarysearch(a,n,beg,last); } if(n > a[mid]) { beg = mid + 1; binarysearch(a,n,beg,last); } return 0; } void main() {clrscr(); int a[50]; int n,no,x,result; printf("\nPROGRAM TO IMPLEMENT BINARY SEARCH USING RECURSION----BY SUGANDHA BHATIA"); printf("\n\n\nEnter the number of terms : "); scanf("%d",&no); printf("\nEnter the elements :\n"); for(x=0;x<no;x++) scanf("%d",&a[x]); printf("\nEnter the number to be searched : "); scanf("%d",&n); result = binarysearch(a,n,0,no-1); if(result == -1) printf("Element not found"); getch(); }
Output
int gcd(int min,int div) { int r=(div%min); if(r!=0) { div=min; min=r; min=gcd(min,div); } return min; } Output
Program 8 Program to implement tower of Hanoi using recursion Source Code #include<stdio.h> #include<conio.h> #include<math.h> void hanoi(int x, char from, char to, char aux) { if(x==1) printf("Move Disk From %c to %c\n",from,to); else { hanoi(x-1,from,aux,to); printf("Move Disk From %c to %c\n",from,to); hanoi(x-1,aux,to,from); } } void main( ) { int disk; int moves; clrscr(); printf("Enter the number of disks you want to play with:"); scanf("%d",&disk); moves=pow(2,disk)-1; printf("\nThe No of moves required is=%d \n",moves); hanoi(disk,'A','C','B'); getch( ); } Output