0% found this document useful (0 votes)
13 views12 pages

Discrete Maths Programs

The document contains multiple C and C++ programs that demonstrate various algorithms and concepts such as the Tower of Hanoi, even/odd detection using finite state machines, power set generation, GCD calculation using recursion, permutations and combinations, prime number checking, binomial coefficients, Euclidean distance calculation, polynomial roots finding, and shortest path algorithms using Floyd's method. Each program includes input prompts and outputs results based on user-provided data. The code snippets illustrate fundamental programming techniques and mathematical concepts.

Uploaded by

rikunsahoo26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Discrete Maths Programs

The document contains multiple C and C++ programs that demonstrate various algorithms and concepts such as the Tower of Hanoi, even/odd detection using finite state machines, power set generation, GCD calculation using recursion, permutations and combinations, prime number checking, binomial coefficients, Euclidean distance calculation, polynomial roots finding, and shortest path algorithms using Floyd's method. Each program includes input prompts and outputs results based on user-provided data. The code snippets illustrate fundamental programming techniques and mathematical concepts.

Uploaded by

rikunsahoo26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Tower of Hanoi
#include<stdio.h>
void TOH(int n,char x,char y,char z) {
if(n>0) {
TOH(n-1,x,z,y);
printf("Move disk %d from rod %c to rod
%c\n",n,x,y);
TOH(n-1,z,y,x);
}
}
int main() {
int n=3;
TOH(n,'A','C','B');
return 0;
}
WAP to detect whether a number is even or odd using finite state machine
#include<iostream>
using namespace std;

bool isEven(int n)
{
return(!(n & 1));
}

int main()
{
int n;
cout<<"Enter a number"<<endl;
cin>>n;

isEven(n)?cout<<"Even ":
cout<<"Odd ";

return 0;
}
WAP to find the power set for a given number
#include<stdio.h>
#include<math.h>
#define ROW 8
#define COL 256

int main()
{
int i,j,k,num,temp,flag;
int table[COL][ROW]={0};

char set[ROW][20];

printf("\nEnter the no of set elements ");


scanf("%d",&num);
printf("\nEnter any %d elements: \n", num);

for(i=0;i<num;i++)
{
fflush(stdin);
gets(set[i]);
}

for(i=0;i<pow(2,num);i++)
{
temp=i;
for(j=0;j<num;j++)
{
table[i][j]=temp%2;
temp=temp/2;
}
}

printf("\nThe power set is: \n");


for(i=0;i<pow(2,num);i++)
{
printf("\n\n(");
for(j=0;j<num;j++)
{
flag=0;
if(table[i][j]==1)
{
for(k=j+1;k<num;k++)
if(table[i][k]==1)
flag=1;

if(flag==1)
printf(" %s ", set[j]);
else
printf(" %s ", set[j]);
}
}
printf(")");
}
return 0;
}
WAP to find GCD of two number using recursion
#include <stdio.h>

int gcd(int a, int b) {


if (b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int result = gcd(num1, num2);
printf("The GCD of %d and %d is %d\n", num1, num2,
result);
return 0;
}
Find Permutation and Combination result for a given pair of values n and r
#include<stdio.h>
// #include<conio.h>
long permutation(int n,int r);
long combination(int n,int r);
long factorial(int num);
int main(void)
{
int n,r;
// clrscr();
printf("Enter n:");
scanf("%d",&n);
printf("Enter r:");
scanf("%d",&r);
printf("permutation %d\n",permutation(n,r));
printf("Combination %d\n",combination(n,r));
// getch();
return 0;
}
long permutation(int n,int r)
{
return factorial(n)/factorial(n-r);
}
long combination(int n,int r)
{
return permutation(n,r)/factorial(r);
}
long factorial(int num)
{
long long fact=1;
while(num>0)
{
fact *=num;
num--;
}
return fact;
}
WAP to check a number is prime or not
#include<iostream>
// #include<conio.h>
using namespace std;
int main()
{
// clrscr();
int i,n,c=0;
cout<<"Enter a number:";
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
++c;
}
}
if(c==2)
cout<<"Prime Number";
else
cout<<"Not Prime Number";
// getch();
return 0;
}
To find Binomial Coefficients
#include<stdio.h>
int BC(int n, int k);

int main()
{
int n,k;
printf("Enter n and k : ");
scanf("%d%d",&n,&k);
printf("Binomial coefficient: ",BC(n,k));
printf("%d\n",BC(n,k));

return 0;
}

int BC(int n, int k)


{
if(k==0 || k==n)
return 1;
return BC(n-1,k-1) + BC(n-1,k);
}
Calculate the Euclidian distance between two points
#include<stdio.h>
#include<math.h>

int main()
{
int x1,y1,x2,y2;

int dist;

printf("Enter point(x1,y1)\n");
scanf("%d%d",&x1,&y1);
printf("Enter point(x2,y2)\n");
scanf("%d%d",&x2,&y2);

dist=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

printf("Distance between (%d %d) and (%d %d) is:


%d\n",x1,y1,x2,y2,dist);

return 0;
}
To find roots of the polynomials
#include<stdio.h>
#include<math.h>
// #include<conio.h>
int main()
{
double a,b,c,discriminant,root1,root2,realpart,imagepart;
// clrscr();
printf("Enter co-efficients a,b and c:");
scanf("%lf%lf%lf",&a,&b,&c);
discriminant=b*b-4*a*c;
if(discriminant>0)
{
root1=(-b+sqrt(discriminant))/(2*a);
root2=(-b-sqrt(discriminant))/(2*a);
printf("root1=%lf and root2=%lf",root1,root2);
}
else if(discriminant==0)
{
root1=root2=-b/(2*a);
printf("root1=root2=%lf",root1);
}
else
{
realpart=-b/(2-a);
imagepart=sqrt(-discriminant)/(2-a);
printf("root1=%lf+%lfi and root2=%f-
%ffi",realpart,imagepart,realpart,imagepart);
}
// getch();
return 0;
}
To find shortest path pair in a plane
#include <iostream>
#include <cstdlib>
#define max 10
#define infi 999
using namespace std;
int p[max][max];
// All Pairs Shortest Path using Floyd's Algorithm

void allpairshort(int a[max][max], int n)


{
int k, i, j;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][k] + a[k][j] < a[i][j])
{
a[i][j] = a[i][k] + a[k][j];
p[i][j] = k;
}
}
}
}
}

// Storing the shortest path

void shortest(int i, int j)


{
int k = p[i][j];
if (k > 0)
{
shortest(i, k);
cout<<" "<<k<<" ";
shortest(k, j);
}
}
// Display the Shortest Path
void findpath(int a[max][max], int i, int j, int n)
{
cout<<"Path from " << i <<" to "<< j << ":";
if (a[i][j] < infi)
{
cout<<" "<<i<<" ";
shortest(i, j);
cout<<" "<<j<<" ";
}
}
// Main Contains Menu
int main()
{
int i, j;
int a[][10] = {{0, 10, infi, 30, 100},
{infi, 0 , 50, infi, infi},
{infi, infi , 0, infi, 10},
{infi, infi , 20, 0, 60},
{infi, infi , infi, infi, 0},
};

allpairshort(a, 5);
findpath(a, 0, 4, 5);
return 0;
}

You might also like