SUB SET FROM A GIVEN SETS
EXNO: 1
DATE:
AIM:
To find subsets from a given sets.
ALGORITHM:
STEP 1: Start the program
STEP 2: Declare subsets and functions with accept array of element. And convert into
subsets based on index “r”
STEP 3: In the main get the value for n.
STEP 4: Get the value for array element one after one
STEP 5: Call the function subsets with array element n, k as a parameter
STEP 6: Element k is the index with split the subsets.
Example;
K=2.subset of 2 element is created
STEP 7: Display the result.
STEP 8: Stop the program.
CODING
Prg 1://Subset from a given set
#include <stdio.h>
#inlcude<conio.h>
/* Function to generate subset */
void subset(int arr[], int data[], int start, int end, int index, int r)
{
int j, i;
if (index == r)
{
for (j = 0; j < r; j++)
printf("%d\t ", data[j]);
printf("\n");
return;
}
for (i = start; i <= end && end - i + 1 >= r - index; i++)
{
data[index] = arr[i];
subset(arr, data, i+1, end, index+1, r);
}
}
/* End of subset() */
/* Function to print the subset */
void printsubset(int arr[], int n, int r)
{
int data[6];
subset(arr, data, 0, n - 1, 0, r);
}
/* End of printsubset() */
void main()
{
int arr[20], k, n, i;
clrscr();
printf("Enter the number of input : ");
scanf("%d", &n);
printf("\nEnter the integers: \n");
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter value of k: ");
scanf("%d", &k);
printsubset(arr, n, k);
getch();
}
OUTPUT:
Enter the values of input :3
Enter the integers:
1
2
3
Enter the values of K:3
1 2
2 3
3 1
RESULT:
Thus the above program has been verified and executed successfully.
TRANSITIVE CLOSURE
EX.NO:2
DATE:
AIM:
To write a program to transitive closure
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the array values and using in for loop
STEP 3:To declare the variables int I,J;
STEP 4:Get the matrix values one by one to enter
STEP 5:To enter the array values of the set bracket at main function
STEP 6:To call the function in graph
STEP 7:Print the array values from the main which return values of 0
STEP 8:To save the program
STEP 9:Stop the program
CODING:
Prg 2://Transitive closure
#include<stdio.h>
#define V 4
void printSolution(int reach[][V]);
void transitiveClosure(int graph[][V])
{
int reach[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
reach[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
}
}
}
printSolution(reach);
}
void printSolution(int reach[][V])
{
int i,j;
printf ("Following matrix is transitive
closure of the given graph\n");
for ( i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
printf ("%d ", reach[i][j]);
printf("\n");
}
}
int main()
{
int graph[V][V] = { {1, 1, 0, 1}, {0, 1, 1, 0},
{0, 0, 1, 1}, {0, 0, 0, 1} };
clrscr();
// Print the solution
transitiveClosure(graph);
getch();
return 0;
}
OUTPUT:
Following the matrix is transitive closure of the given graph:
1111
0111
0011
0001
RESULT:
Thus the above program has been verified and executed successfully.
TO PROVE A SERIES
EX.NO: 3
DATE:
AIM:
To find a c program to prove a series 1/ (1*2) + 1/(2*3) ………+1/(n(n+1)) =
n/(n+1) is equal or not.
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the floating point values
STEP 3:Get the values for a series
STEP 4:To using the for loop to calculate the sum. The formula has been
Sum=sum+(1/(i*(i+1).
STEP 5:To declare the values for j. formula has been j=(n/n+1).
STEP 6:To save the program
STEP 7:Stop the program.
CODING:
Prg 3://To Prove the Series is Equal or not
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
float sum=0.0,n;
clrscr();
printf("enter the value for n:");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1/(i*(i+1)));
}
printf(“\n Sum=%f”,sum);
printf(“\n”);
j=(n/(n+1));
printf(“\n n/(n+1) = %f ”,j);
if(sum==j) {
printf("\n the series is equal");
}
else {
printf("\n the series is not equal");
}
getch();
}
OUTPUT:
Enter the values of n:3
0.500000 0.666667 0.750000
Sum= 0.750000
n(n+1)= 0.750000
the series is equal..
RESULT:
Thus the above program has been verified and executed successfully.
SUM OF THE SERIES
EX.NO:4
DATE:
AIM:
To write a program to perform the sum of series 1+(1+2)+(1+2+3)+...+(1+2+...n)
ALGORITHM:
STEP 1:Start the program
STEP 2:To declare the values
STEP 3:Get the values of n
STEP 4:To calculate the sum this formula is sum=sum+j
STEP 5:To save the program
STEP 6:Stop the program
CODING:
Prg 4:/*Sum of Series
sum = 1+ (1+2) + (1+2+3) + … + (1+2…+n)
=1+(1+2) + (1+2+3) + (1+2+3+4) +(1+2+3+4+5) */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,sum=0;
clrscr();
printf("enter the value for n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum=sum+j;
}
printf(“\n”);
}
printf("\n the series is =%d", sum);
getch()
}
OUTPUT:
Enter the values of n:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sum:35
RESULT:
Thus the above program has been verified and executed successfully.
FIBONACCI SERIES USING RECURSION
EX.NO:5
DATE:
AIM:
To find the Fibonacci series using recursion method.
ALGORITHM:
STEP 1:Start the program
STEP 2:Decalre the function Fibonacci which accept terms as a parameter
And return a integer values
STEP 3:Get the values for n
STEP 4:Call the function Fibonacci(counter)
STEP 5:The values of counter pass as argument to term
STEP 6:Calculate Fibonacci series for n term using
Fibonacci(term-1)+Fibonacci(term-2)using recursion call and return
The result to main
STEP 7:Display the result
STEP 8:Stop the program
CODING:
Prg 5://Fibonacci Series using Recursion
#include <stdio.h>
#include <conio.h>
int fibonacci(int term);
void main(){
int terms, counter;
printf("Enter number of terms in Fibonacci series: ");
scanf("%d", &terms);
printf("Fibonacci series till %d terms\n", terms);
for(counter = 0; counter < terms; counter++){
printf("%d\n", fibonacci(counter));
}
getch(); }
int fibonacci(int term)
{
if(term < 2)
return term;
return fibonacci(term - 1) + fibonacci(term - 2);
}
OUTPUT:
Enter the number of term in Fibonacci series:8
0
1
1
2
3
5
8
13
RESULT:
Thus the above program has been verified and executed successfully.
FACTIORIAL USING IN RECURSION
EX.NO:6
DATE:
AIM:
To find a factorial using recursion method
ALGORITHM:
STEP 1:Start the program
STEP 2:Declar factorial function with accept n as parameter and return integer values
STEP 3:Get the value for n in the main and call the Function factorial(n);
STEP 4:Method factorial execute the codes n*factorial(n-1)
STEP 5:Factorial function return the values of factorial for Main function
STEP 6:Stop the program
CODING:
Prg 6://Factorial using Recursion
#include <stdio.h>
int factorial(int n);
void main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, factorial(n));
}
int factorial(int n)
{
if (n >= 1)
return n* factorial(n-1);
else
return 1;
}
OUTPUT:
Enter the positive integer:3
Factorial of 3=6
RESULT:
Thus the above program has been verified and executed successfully.
MINIMUM COST SPANNING TREE
EX.NO:7
DATE:
AIM:
To find the program minimum cost spanning tree using Prims algorithm from a graph
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the variable array visited two dimensional array cost.minimum cost’0’
STEP 3:Get the number of nodes with”n”
STEP 4:Get adjacency matrix cost of [i][j].
STEP 5:Assign maximum values cost of [i][j]=”999” with cost of [i][j]=”0”
STEP 6:Mark visited[i]=1
STEP 7:Update the maximum cost min=cos[i][j] by visiting the unvisited nodes
STEP 8:Display the edgest with minimum cost and update the minimum path cost
STEP 9:Run the program
STEP 10:Stop the program
CODING:
Prg 7://Minimum cost spanning Tree
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost: %d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
OUTPUT:
Enter the number of nodes:3
Enter the adjacency matrix:
0 1 2
2 0 3
3 2 0
Edge 1:(12)cost:1
Edge 2:(13)cost:2
Minimum cost=3
RESULT:
Thus the above program has been verified and executed successfully.
SHORTEST PATH USING DIJKSTRA’S ALGORITHM
EXNO:8
DATE:
AIM:
To find the shortest path of using Dijkstra’s algorithm
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the variable and declare DIJKSTRA’S method with cost source and
targets
STEP 3:Enter the cost of connecting the nodes
STEP 4:Get the source nodes and target nodes
STEP 5:Find the shortest path from source to target and display the shortest path using
Dijkstra’s Algorithm
STEP 6:Stop the program
CODING:
Prg 8://Shortest path using Dijikstra’s Algorithm
#include<stdio.h>
#include<conio.h>
void main()
{
int path[2][2],i,j,min,a[3][3],p,st=1,ed=3,stp,edp,t[3],index;
clrscr();
printf("enter the cost matrix\n");
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths\n");
scanf("%d",&p);
printf("enter possible paths\n");
for(i=1;i<=p;i++)
for(j=1;j<=3;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=3;j++)
{
edp=path[i][j+1];
t[i]=t[i]+a[stp][edp];
if(edp==ed)
break;
else
stp=edp;
}
}
min=t[st];index=st;
for(i=1;i<=p;i++)
{
if(min>t[i])
{
min=t[i];
index=i;
}
}
printf("minimum cost %d",min);
printf("\n minimum cost path ");
for(i=1;i<=3;i++)
{
printf("--> %d",path[index][i]);
if(path[index][i]==ed)
break;
}
getch();
}
OUTPUT:
Enter the cost matrix :
005
002
000
enter number of paths : 2
enter possible paths :
12
23
13
minimum cost : 2
minimum cost path :
1–>2–>3
RESULT:
Thus the above program has been verified and executed successfully.
CONSTRUCT THE TRUTH TABLE
EXNO:9
DATE:
AIM:
To construct the truth table using three variables
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the 3 variables X,Y,Z and assign the values 0,1
STEP 3:Make a loop for variable X,Y,Z for 0,1
STEP 4:Find the truth table for X*Y+Z
STEP 5:Stop the program
CODING:
Prg 9://Constructing a Truth Table
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf(“\n X\tY\tZ\tXY+Z”);
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf(“\n%d\t%d\t%d\t”,x,y,z);
else
printf(“\n%d\t%d\t%d\t%d”,x,y,z,x*y+z);
}
getch();
}
OUTPUT:
X Y Z XY+Z
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 _
RESULT:
Thus the above program has been verified and executed successfully
DEMORGAN’S LAW
EX.NO:10
DATE:
AIM:
To prove the Demorgan’s Law
ALGORITHM:
STEP 1:Start the program
STEP 2:Declare the variables X and Y
STEP 3:Get the values for X and Y
STEP 4:To prove the program DEMORGAN’S LAW using the formula:
If((!(x<5)&&!(y>=7))&&!((x<5)!!(y>=7)))
STEP 5:For values x>5 and then y<7 the expression is executed for truth table
STEP 6:For values x<5 and then y>7 the expression is executed for the truth table
STEP 7:Stop the program
CODING:
Prg 10://To Prove De- Morgan’s Law
#include <stdio.h>
#include <math.h>
int main()
{
int x;
int y;
printf("Enter x: ");
scanf("%d",&x);
printf("Enter y: ");
scanf("%d",&y);
if ((!(x < 3) && !(y >= 7)) && !((x < 3) || (y >= 7)))
{
printf("True!\n\n");
}
else
{
printf("False!\n\n");
}
}
OUTPUT:
Enter X=6
Enter y=6
TRUE!!!!!
Enter X=7
Enter y=4
FALSE!!!!!
RESULT:
Thus the above program has been verified and executed successfully