You are on page 1of 42

INDEX

1. Program to add two matrices. 2. Program to perform a binary search. 3. Program to sort a array using Bubble Sort. 4. Program to compare two strings using strcmp() function. 5. Program to find factorial of a number (using recursion). 6. Program to find the fibonacci series 7. Program to find greatest of three numbers. 8. Program to sort a array using Insertion Sort. 9.Program to perform a linear search. 10.Program to sort a array using Merge Sort. 11.Program to multiply two matrices. 12.Program to print out prime numbers upto a given input. 13.Program to find the roots of a quadratic equation 14.Program to reverse a string. 15.Program to sort a array using Selection Sort. 16.Program to subtract two matrices. 17.Programs for swap two numbers without using 3rd variable 18.Programs for swap two numbers with using 3rd variable 19.Program to transpose a matrix 20.Program to convert a decimal number to binary,octal and hexadecimal.

Program to add two matrices.


#include<iostream.h> #include<stdio.h> void Add(int[10][10],int[10][10],int[10][10],int,int,int,int); int main() { int i,j,m,n; int A[10][10], B[10][10], C[10][10]; cout<<"\n Enter no. of rows n cols: "; cin>>m>>n; cout<<"\n Enter matrix A :"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>A[i][j];} } cout<<"\n Matrix A :\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<A[i][j]<<" ";} cout<<\n"; } cout<<"\nEnter matrix B :"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>B[i][j];}

} cout<<"\n Matrix B :\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<B[i][j]<<" ";} } cout<<\n";

Add(A,B,C,m,n); return 0; } void Add(int A[10][10],int B[10][10],int C[10][10], int m, int n, int i, int j) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { C[i][j]= A[i][j] + B[i][j];} } cout<<"\n Sum of the two matrices : \n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<C[i][j]<<" ";} cout<<\n"; } return; }

OUTPUT-

Enter no. of rows n cols: 3 3 Enter matrix A : 1 2 3 4 5 6 7 8 9 Matrix A : 1 2 3 4 5 6 7 8 9 Enter matrix B : 9 8 7 6 5 4 3 2 1 Matrix B : 9 8 7 6 5 4 3 2 1 Sum of the two matrices : 10 10 10 10 10 10 10 10 10

Program to perform a binary search


#include<iostream.h> #include<conio.h> void main() { int array[10]; int i, j, n, temp, num; int low,mid,high; clrscr(); cout<<"Enter the value of the array\t"; cin>>n; cout<<"Enter the elements one by one:\n"; for(i=0;i<n;i++) { cin>>array[i]; } for(i=0;i<n;i++) {

for(j=0;j<(n-i-1);j++) { if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } cout<<"Sorted array is...\n"; for(i=0;i<n;i++) { cout<<"\n"<<array[i]; } cout<<"Enter the element to be searched\n"; cin>>num; low=1; high=n; do { mid=(low+high)/2; if(num<array[mid]) high=mid-1; else if(num>array[mid]) low=mid+1; } while(num!=array[mid] && low<=high);

if(num==array[mid]) { cout<<"\n\tis present at position "<<array[i]<<i+1; } else { cout<<"Search is FAILED\n"; } getch(); }

OUTPUTEnter the value of array: 5 Enter the elements one by one: 12 23 52 25 10 Sorted array is... 10 12 23 25 52 Enter the element to be searched: 25 25 is present at position 4

Program to sort a array using Bubble Sort.


#include<iostream.h> #include<stdio.h> #include<conio.h> void bubbleSort(int, int); void main() { int arr[50],temp,i,j,n; clrscr(); cout<<"\nEnter any Value less Than 50: "; cin>>n;

cout<<"\n\tEnter The Values into ARRAY : "; for(i=0;i<n;i++) { cout<<"\n\n Enter Element no "<<i+1; cin>>arr[i]; } bubbleSort(arr[],n); cout<<"\n\n-- Sorted Series --"; for(i=0;i<n;i++) { cout<<"\n \n \t "<<arr[i]; } getch(); } void bubbleSort(int arr[], int n) { int swapped = 1; int j = 0; int tmp; while (swapped) { swapped = 0; j++; for (int i = 0; i < n - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = 1; } } } }

OUTPUTEnter any Value less Than 50: 6 Enter The Values into ARRAY: 25 65 84 75 16 11 -- Sorted Series -11 16 25 65 75 84

Program to compare two strings using strcmp() function.


#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str1[50],str[50];

cout<<"\n Enter the first string: "; gets(str1); cout<<"\n Enter the second string: "; gets(str2); if(strcmp(str1,str2)==0) { cout<<"\n Both strings are equal"; } else { cout<<"\n Strings are not equal"; getch(); }

OUTPUTEnter the first string: india Enter the second string: india Both strings are equal

Program to find factorial of a number (using recursion).


#include <iostream.h> #include <conio.h> int fact(int k) { if(k==0)

return 1; else return k*fact(k-1); }

void main() { int n; clrscr(); cout<<"\n Enter a number :"; cin>>n; cout<<"\n Factorial of <<n<<" is ""<<fact(n); getch(); }

Output:
Enter a number : 6 Factorial of 6 is 720

Program to find the fibonacci series


#include <iostream.h> #include <conio.h>

void main() { clrscr(); int a,b,c,n; cout<<"\n Enter the value of a :"; cin>>n; a=0; b=1; cout<<"\n Fibonacci series =\n "; cout<<a<<" "<<b; for(int i=2;i<=n-2;i++) { c=a+b; cout<<" "<<c; a=b; b=c; } getch(); }

OUTPUT
Enter the value of n :8 Fibonacci series = 0 1 1 2 3 5 8 13

Program to find greatest of three numbers.


#include <iostream.h>

#include <conio.h> void main() { clrscr(); int a,b,c; cout<<"\n\tEnter the first number :"; cin>>a; cout<<"\n\tEnter the second number :"; cin>>b; cout<<"\n\tEnter the third number :"; cin>>c; if(a>b && a>c) { cout<<"\n\tFirst number is greatest"; } if(b>a && b>c) { cout<<"\n\tSecond number is greatest"; } if(c>a && c>b) { cout<<"\n\tThird number is greatest"; } getch(); }

OUTPUT

Enter the first number:29 Enter the second number:34 Enter the third number:75 Third number is greatest

Program to sort a array using Insertion Sort.


#include<iostream.h> #include<stdio.h> #include<conio.h> void insertionSort(int, int); void main() { int arr[50],temp,i,j,n; clrscr(); cout<<"\nEnter any Value less Than 50: "; cin>>n; cout<<"\n\tEnter The Values into ARRAY : "; for(i=0;i<n;i++) { cout<<"\n\n Enter Element no "<<i+1; cin>>arr[i]; } insertionSort(arr[],n); cout<<"\n\n-- Sorted Series --"; for(i=0;i<n;i++) { cout<<"\n \n \t "<<arr[i]; } getch(); } void insertionSort(int arr[], int length)

{ int i, j, tmp; for (i = 1; i < length; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { tmp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = tmp; j--; } } }

OUTPUTEnter any Value less Than 50: 6 Enter The Values into ARRAY: 5 35 84 6 92 14 -- Sorted Series -5 6 14 35 84 92

Program to perform a linear search


#include<iostream.h> #include<conio.h> #include<stdlib.h> void main(){ int arr[100],i,element,no; clrscr(); cout<<"\nEnter the no of Elements: "; cin>>no; for(i=0;i<no;i++) { cout<<"\n Enter Element "<<i+1; cin>>arr[i]; } cout<<"\nEnter the element to be searched: "; cin>>element; for(i=0;i<no;i++) { if(arr[i] == element){ cout<<"\nElement found at position ",i+1; getch(); exit(1); }

} cout<<"\nElement not found"; getch(); }

OUTPUTEnter the no of Elements: 5 Enter Element 1: 12 Enter Element 2: 23 Enter Element 3: 52 Enter Element 4: 23 Enter Element 5: 10 Enter the element to be searched: 23 Element found at position 2

Program to sort a array using Merge Sort.


#include<iostream.h> #include<stdio.h> #include<conio.h> void mergeSort(int numbers[], int temp[], int array_size); void m_sort(int numbers[], int temp[], int left, int right); void merge(int numbers[], int temp[], int left, int mid, int right); void main() { int arr1[50],arr2[50]temp,i,j,n; clrscr(); cout<<"\nEnter any Value less Than 50: "; cin>>n; cout<<"\n\tEnter The Values into ARRAY : "; for(i=0;i<n;i++)

{ cout<<"\n\n Enter Element no "<<i+1; cin>>arr[i]; } mergeSort(arr1[], arr2[], n); cout<<"\n\n-- Sorted Series --"; for(i=0;i<n;i++) { cout<<"\n \n \t "<<arr1[i]; } getch(); } void mergeSort(int numbers[], int temp[], int array_size) { m_sort(numbers, temp, 0, array_size - 1); } void m_sort(int numbers[], int temp[], int left, int right) { int mid; if (right > left) { mid = (right + left) / 2; m_sort(numbers, temp, left, mid); m_sort(numbers, temp, (mid+1), right); } } void merge(int numbers[], int temp[], int left, int mid, int right) { int i, left_end, num_elements, tmp_pos; left_end = (mid - 1); tmp_pos = left; num_elements = (right - left + 1); merge(numbers, temp, left, (mid+1), right);

while ((left <= left_end) && (mid <= right)) { if (numbers[left] <= numbers[mid]) { temp[tmp_pos] = numbers[left]; tmp_pos += 1; left += 1; } else { temp[tmp_pos] = numbers[mid]; tmp_pos += 1; } } while (left <= left_end) { temp[tmp_pos] = numbers[left]; left += 1; tmp_pos += 1; } while (mid <= right) { temp[tmp_pos] = numbers[mid]; mid += 1; } tmp_pos += 1; mid += 1;

for (i=0; i < num_elements; i++) { numbers[right] = temp[right]; right -= 1; } }

OUTPUT-

Enter any Value less Than 50: 5 Enter The Values into ARRAY: 9 32 5 41 66 -- Sorted Series -5 9 32 41 66

Program to multiply two matrices.


#include<iostream.h> #include<stdio.h> void Multiply(int[10][10],int[10][10],int[10][10],int,int,int); int main() { int i,j,m; int A[10][10], B[10][10], C[10][10]; cout<<"\n Enter no. of rows/cols: ";

cin>>m; cout<<"\n Enter matrix A :"; for(i=0;i<m;i++) { for(j=0;j<m;j++) { cin>>A[i][j];} } cout<<"\n Matrix A :\n"; for(i=0;i<m;i++) { for(j=0;j<m;j++) { cout<<A[i][j]<<" ";} cout<<\n"; } cout<<"\nEnter matrix B :"; for(i=0;i<m;i++) { for(j=0;j<m;j++) { cin>>B[i][j];} } cout<<"\n Matrix B :\n"; for(i=0;i<m;i++) { for(j=0;j<m;j++) { cout<<B[i][j]<<" ";} cout<<\n"; } Multiply(A,B,C,m); return 0; } void Multipy(int A[10][10],int B[10][10],int C[10][10],int m, int i, int j) { int k;

cout<<"\n The multiplied matrix is:"; for(i=0;i<m;i++) { for(j=0;j<m;j++) { c[i][j]=0; for(k=0;k<m;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } } } for(i=0;i<m;i++) { for(j=0;j<m;j++) { cout<<c[i][j]; } cout<<"\n" } return;}

OUTPUTEnter no. of rows n cols: 3 3 Enter matrix A : 1 2 3 4 5 6 7 8 9 Matrix A : 1 2 3 4 5 6 7 8 9

Enter matrix B : 9 8 7 6 5 4 3 2 1 Matrix B : 9 8 7 6 5 4 3 2 1 Sum of the two matrices : 9 16 21 24 25 25 21 16 9

Program to print out prime numbers upto a given input.


#include<iostream.h> #include<conio.h>

main() { int num,prime_no,i=2,check=2; cout<<"enter the number upto which prime no.s is to be extracted\n"; cin>>num; cout<<prime numbers upto <<num<< are : ; while(i<=num) { check=2; while(i%check!=0) { } if(i==check) { Prime_no = prime_no + i; } i++; } Cout<<prime_no; getch(); } check++;

OUTPUTEnter no. upto which to find prime no. : 10

Prime numbers upto 10 are: 1 , 3 , 5 , 7 , Enter no. upto which to find prime no. : 50 Prime numbers upto 50 are: 1 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 ,

Program to find the roots of a quadratic equation


#include <iostream.h> #include <conio.h> #include <math.h> int main() { clrscr(); float a,b,c,d,root1,root2; cout << "Enter the 3 coefficients a, b, c : " << endl; cin>>a>>b>>c; if(!a){ if(!b) cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n"; else { d=-c/b; cout << "The solution of the linear equation is : " << d << endl; } } else { d=b*b-4*a*c; if(d>0) root1=(-b+sqrt(d))/(2*a); root2=(-b-sqrt(d))/(2*a); cout << "The first root = " << root1 << endl; cout << "The second root = " << root2 << endl; } getch(); return 0; }

OUTPUTEnter the 3 coefficients a, b, c : 4 4 -3 The first root = 0.5 The second root = -1.5

Program to reverse a string.


#include<iostream.h> #include<stdio.h> #include<string.h> void main() { char str[50],revstr[50]; int i=0,j=0; cout<<"Enter the string to be reversed : "; gets(str); for(i=strlen(str)-1;i>=0;i--) { revstr[j]=str[i]; j++; } revstr[j]='\0'; cout<<"Input String : "<<str; cout<<"\n Output String : "<<revstr; getch(); }

OUTPUTEnter the string to be reversed :KESHAV Input String : KESHAV

Output String : VAHSEK

Program to sort a array using Selection Sort.


#include<iostream.h> #include<stdio.h> #include<conio.h> void selectionSort(int, int); void main() { int arr[50],temp,i,j,n; clrscr(); cout<<"\nEnter any Value less Than 50: "; cin>>n; cout<<"\n\tEnter The Values into ARRAY : "; for(i=0;i<n;i++) { cout<<"\n\n Enter Element no "<<i+1; cin>>arr[i]; } selectionSort(arr[],n); cout<<"\n\n-- Sorted Series --"; for(i=0;i<n;i++) { cout<<"\n \n \t "<<arr[i]; }

getch(); } void selectionSort(int arr[], int n) { int i, j, minIndex, tmp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } } }

OUTPUTEnter any Value less Than 50: 6 Enter The Values into ARRAY: 5 35 84 6 92 14 -- Sorted Series -5 6 14 35 84 92

Program to subtract two matrices.


#include<iostream.h> #include<stdio.h> void Sub(int[10][10],int[10][10],int[10][10],int,int,int,int); int main() { int i,j,m,n; int A[10][10], B[10][10], C[10][10]; cout<<"\n Enter no. of rows n cols: "; cin>>m>>n; cout<<"\n Enter matrix A :"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>A[i][j];} } cout<<"\n Matrix A :\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<A[i][j]<<" ";}

cout<<\n";

cout<<"\nEnter matrix B :"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>B[i][j];} } cout<<"\n Matrix B :\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<B[i][j]<<" ";} } cout<<\n";

Sub(A,B,C,m,n); return 0; } void Sub(int A[10][10],int B[10][10],int C[10][10], int m, int n, int i, int j) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { C[i][j]= A[i][j] - B[i][j];} } cout<<"\n Subtract of the two matrices : \n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<C[i][j]<<" ";} cout<<\n"; } return; }

OUTPUTEnter no. of rows n cols: 3 3 Enter matrix A : 1 2 3 4 5 6 7 8 9 Matrix A : 1 2 3 4 5 6 7 8 9 Enter matrix B : 9 8 7 6 5 4 3 2 1 Matrix B : 9 8 7 6 5 4 3 2 1 Sum of the two matrices : -8 -6 -4 -2 0 2 4 6 8

Programs for swap two numbers without using 3rd variable


# include<iostream.h> # include<conio.h> void main() { int a, b ; clrscr() ; cout<<"Enter two numbers : "; cin>>a>>b; cout<<"\nBefore swapping : \n\n"; cout<<"a = \t ,b = "; a = a + b ; b = a - b ;

a = a - b ; cout<<"\n\nAfter swapping : \n\n"; cout<<"a = \t ,b = "; getch() ; }

OUTPUTEnter two numbers : 10 20 Before swapping : a = 10 b = 20 After swapping : a = 20 b = 10

Programs for swap two numbers with using 3rd variable


# include<iostream.h> # include<conio.h> void main() { int a, b ,c; clrscr(); cout<<"Enter two numbers : "; cin>>a>>b; cout<<"\nBefore swapping : \n\n";

cout<<"a = \t ,b = "; c=a; a=b; b=c; cout<<"\n\nAfter swapping : \n\n"; cout<<"a = \t ,b = "; getch() ; }

OUTPUTEnter two numbers : 10 20 Before swapping : a = 10 b = 20 After swapping : a = 20 b = 10

Program to transpose a matrix


#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],m,n,i,j; cout<<"Enter number of rows: ";

cin>>m; cout<<"Enter number of coloumns: "; cin>>n; if(m!=n) { cout<<"Matrix not square so Transpose not possible :("; } else { cout<<endl<<"Enter elements of matrix: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element a"<<i+1<<j+1<<": "; cin>>a[i][j]; } } cout<<endl<<"Displaying Matrix: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl<<endl; } cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl; for(i=0;i<m;i++)

{ for(j=0;j<n;j++) { cout<<a[j][i]<<" "; } cout<<endl<<endl; } } getch(); }

Output:
Enter number of rows: 2 Enter number of columns: 2 Enter Enter Enter Enter Enter elements of matrix: element a11: 11 element a12: 12 element a21: 21 element a22: 22

Displaying matrix: 11 12 21 22 Displaying matrix transpose: 11 21 12 22

Program to convert decimal number to binary,octal and hexadecimal


#include<iostream.h> #include<string.h> #include<conio.h> void dectobin(int );

void dectooct(int ); void dectohex(int ); main() {

int x,num,n; Cout<<"enter a decimal number"; Cin>>num; Cout<<\nits binary number equivalent: "; dectobin(num); Cout<<"\n its hexadecimal numbere quivalent: "; dectohex(num); Cout<<\n\nits octal number equivalent: "; dectooct(num); getch();

} void dectobin(int num) { int n=0,k=1; while(num!=0) { n=n+(num%2)*k; num=num/2; k=k*10; } Cout<<n<<\n; } void dectooct(int num) { int n,k=1,ans=0,t=num,i=1,p=1; while(t!=0) { t=t/8; p++;

} while(num>0) {k=1;n=num;i=p; while(i!=0) { n=n/8; i--; k=k*8; } p--; num=num-k*n; ans=ans*10+n; } Cout<<ans; }

void dectohex(int d) { int b,c=0,a[5],i=0; b=d; while (b>15) { a[i]=b%16; b=b/16; i++; c++; } a[i]=b; for (i=c;i>=0;--i) { if (a[i]==10) else if (a[i]==11) else if (a[i]==12) else if (a[i]==13) else if (a[i]==14) else if (a[i]==15) else } return; Cout<<"A"; Cout<<"B"; Cout<<"C"; Cout<<"D"; Cout<<"E"; Cout<<"F"; Cout<<a[i];

OutputEnter a decimal number: 20 Binary number equivalent: 10100 Hexadecimal number equivalent: 14 Octal number equivalent: 24

You might also like