You are on page 1of 11

COMPUTER SCIENCE ~ DISCRETE MATHEMATICS

heoretical computer science includes areas of discrete mathematics relevant to computing. It draws heavily on graph theory and logic. Included within theoretical computer science is the study of algorithms for computing mathematical results. Computability studies what can be computed in principle, and has close ties to logic, while complexity studies the time taken by computations. Automata theory and formal language theory are closely related to computability. Petri nets and process algebras are used to model computer systems, and methods from discrete mathematics are used in analyzing VLSI electronic circuits. Computational geometry applies algorithms to geometrical problems, while computer image analysis applies them to representations of images. Theoretical computer science also includes the study of various continuous computational topics.

Applications of DISCRETE MATHEMATICs in COMPUTER

Properties of number: factors, factorial, prime factor, sum of digits, Matrices: sum, transpose, product, upper, lower, determinant Number theory: ncr, npr, Fibonacci, Armstrong number

#include<iostream.h> #include<conio.h> int main() { int num,i=1,j,k; cout<<"\nEnter a number:"; cin>>num; while(i<=num){ k=0; if(num%i==0){ j=1; while(j<=i){ if(i%j==0) k++; j++; } if(k==2) cout<<i<<"is a prime factor\n"; } i++; } getch(); return 0; }

#include<iostream>

//Calling the Header File.

int main() //Declaring the main function { using namespace std; //Tells the compiler that we'll be using all the standard C++ library functions int num,factorial=1; cout<<"Enter the number: "; //Ask for the number. cin>>num; for(int i=1;i<=num;i++) { factorial=factorial*i; } cout<<"The factorial of the given number is: "<<factorial<<endl; getch(); return 0;

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j; cout<<"Enter number of rows of A: "; cin>>m; cout<<"Enter number of coloumns of A: "; cin>>n; cout<<endl<<"Enter elements of matrix A: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element a"<<i<<j<<": "; cin>>a[i][j]; } } cout<<endl<<"Enter number of rows of B: "; cin>>o; cout<<"Enter number of coloumns of B: "; cin>>p; cout<<endl<<"Enter elements of matrix B: "<<endl; for(i=0;i<o;i++) { for(j=0;j<p;j++) { cout<<"Enter element b"<<i<<j<<": "; cin>>b[i][j]; } } cout<<endl<<"Displaying Matrix A: "<<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 B: "<<endl<<endl; for(i=0;i<o;i++) { for(j=0;j<p;j++) { cout<<b[i][j]<<" "; } cout<<endl<<endl; } if(n==o) { for(i=0;i<m;i++) { for(j=0;j<p;j++) { c[i][j]=0; for(int k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<p;j++) { cout<<c[i][j]<<" "; } cout<<endl<<endl; } }else cout<<"Multiplication not possible :("; getch();

#include <iostream.h> #include<conio.h> int main() { float A[3][3];// the matrix that is entered by user int i,j; float x,n=0;//n is the determinant of A cout<<"========== Enter matrix A =============================================\n"; for(i=0;i<3;i++) { cout<<endl; for(j=0;j<3;j++) { cout<<" A["<<i<<"]["<<j<<"]= "; cin>>A[i][j]; } }

for(i=0,j=0;j<3;j++) { if(j==2) n+=A[i][j]*A[i+1][0]*A[i+2][1]; else if(j==1) n+=A[i][j]*A[i+1][j+1]*A[i+2][0]; else n+=A[i][j]*A[i+1][j+1]*A[i+2][j+2]; } for(i=2,j=0;j<3;j++) { if(j==2) n-=A[i][j]*A[i-1][0]*A[i-2][1];

else if(j==1) n-=A[i][j]*A[i-1][j+1]*A[i-2][0]; else n-=A[i][j]*A[i-1][j+1]*A[i-2][j+2];

cout<<"\n========== The matrix A is ==========================================\n"; for(i=0;i<3;i++) { cout<<endl; for(j=0;j<3;j++) { cout<<" A["<<i<<"]["<<j<<"]= "<<A[i][j]; } } cout<<endl<<endl;

cout<<"=========================================================== ==========\n"<<endl; cout<<"The determinant of matrix A is "<<n<<endl<<endl; cout<<"=========================================================== =========="<<endl; getch(); return 0; }

#include <iostream.h> #include <conio.h> # define n 10 void main( ) { int mat[n][n],d,i,j; cout<< "\n Enter dimension of square matrix:"; cin >> d; for( i=0; i < d ; i++) { for( j = 0; j < d ; j++) { cout<<"\n Enter elements for "<<i+1<< "," << j+1 <<"location :"; cin >> mat[i][j]; } } clrscr(); cout<<"\n The Original matrix : \n\n"; for( i = 0; i < d ; i++) { for( j = 0; j < d ; j++) cout<< mat[i][j]<<"\t"; cout<< "\n"; } //lower half of left diagonal ----cout<<"\n The Lower half of the matrix : \n\n"; for( i = 0; i < d ; i++) { for( j = 0; j < d ; j++) { if(i > j) cout << mat [i][j] << " " ; else cout << " " << " "; } cout << "\n "; } getch ( ); }

#include<iostream.h> #include<conio.h> long factorial(int); int main() { clrscr(); int n,r,nr; int factorial_n,factorial_r,factorial_nr; long ncr; cout<<"\n Enter the value of n = "; cin>>n; cout<<"\n Enter the value of r = "; cin>>r; factorial_n=factorial(n); factorial_r=factorial(r); nr=n-r; factorial_nr=factorial(nr); ncr=factorial_n/(factorial_r*factorial_nr); cout<<"\n nCr = "<<n<<" C "<<r<<" = "<<ncr<<endl; getch(); return 0; } long factorial(int number) { long factorial=1; if(number==0 || number==1); else { for(int count=1;count<=number;count++) factorial=factorial*count; } return factorial; }

#include<iostream.h> #include<conio.h> #include<math.h> void main() { int n,sum=0,rem=0,cube=0,n1,i; clrscr(); cout<<"enter a number"; cin>>n; n1=n; while(n!=0) { rem=n%10; cube=pow(rem,3); sum=sum+cube; n=n/10; } if(sum==n1) cout<<"armstrong"; else cout<<"not armstrong"; getch(); }

You might also like