C Program implement Kruskal’s algorithm.

Write a C Program implement Kruskal’s algorithm.
Kruskal’s algorithm is a greedy algorithm that finds the minimum spanning tree of a graph. Graph should be weighted, connected, and undirected.Minimum spanning tree is a spanning tree with weight less than or equal to the weight of every other spanning tree. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("nntImplementation of Kruskal's algorithmnn");
printf("nEnter the no. of verticesn");
scanf("%d",&n);
printf("nEnter the cost adjacency matrixn");
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;
}
}
printf("nThe edges of Minimum Cost Spanning Tree arenn");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("n%d edge (%d,%d) =%dn",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("ntMinimum cost = %dn",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Read more Similar C Programs
Data Structures
Dijkstras Algorithm

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement Warshall’s Algorithm

Data structures using C, Here we solve the Warshall’s algorithm using C Programming Language. Warshall’s algorithm enables to compute the transitive closure of the adjacency matrix of any digraph.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{ ;
if(a>b)
return(a);
else
return(b);
}
void main()
{
int p[10][10]={0},n,e,u,v,i,j;
clrscr();
printf("n Enter the number of vertices:");
scanf("%d",&n);
printf("n Enter the number of edges:");
scanf("%d",&e);
for(i=1;i<=e;i++)
{
printf("n Enter the end vertices of edge %d:",i);
scanf("%d%d",&u,&v);
p[u][v]=1;
}
printf("n Matrix of input data: n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%dt",p[i][j]);
printf("n");
}
warshal(p,n);
printf("n Transitive closure: n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%dt",p[i][j]);
printf("n");
}
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement Floyd’s Algorithm

Data structures using C, Here we solve the Floyd’s algorithm using C Programming Language. Floyd’s algorithm uses to find the least-expensive paths between all the vertices in a Graph.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
{
if(a<b)
return(a);
else
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;;
clrscr();
printf("n Enter the number of vertices:");
scanf("%d",&n);
printf("n Enter the number of edges:n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("n Enter the end vertices of edge%d with its weight n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("n Matrix of input data:n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d t",p[i][j]);
printf("n");
}
floyds(p,n);
printf("n Transitive closure:n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d t",p[i][j]);
printf("n");
}
printf("n The shortest paths are:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=j)
printf("n <%d,%d>=%d",i,j,p[i][j]);
}
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to solve N Queen’s problem

N Queen’s problem is the puzzle. Placing chess queens on a chessboard, so thatNo two queens attack each other. Here we use the Brute-Force method to solve the problem.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("nnSolution #%d:n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Qt");
else
printf("*t");
}
printf("n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Queensn");
scanf("%d",&n);
queen(n);
printf("nTotal solutions=%d",count);
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find a minimum spanning tree using Prim’s algorithm

C Program to implement the Prim’s algorithm. Prims algorithm is a greedy algorithm that finds the minimum spanning tree of a graph. Graph should be weighted, connected, and undirected. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#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();
}
Read more Similar C Programs
Data Structures
Dijkstras Algorithm

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to search the perticulur pattern in the string using Horspool method.

C Program to find the substring in a String using the Horspool method. This algorithm uses the Brute-Forse method which searches the text between 0 and n-m, and after each cycle it shifts the pattern by one position to the right. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
#include<conio.h>
#define MAX 500
int t[MAX];
void shifttable(char p[])
{
int i,j,m;
m=strlen(p);
for(i=0;i<MAX;i++)
t[i]=m;
for(j=0;j<m-1;j++)
t[p[j]]=m-1-j;
}
int horspool(char src[],char p[])
{
int i,j,k,m,n;
n=strlen(src);
m=strlen(p);
printf("nLength of text=%d",n);
printf("n Length of pattern=%d",m);
i=m-1;
while(i<n)
{
k=0;
while((k<m)&&(p[m-1-k]==src[i-k]))
k++;
if(k==m)
return(i-m+1);
else
i+=t[src[i]];
}
return -1;
}
void main()
{
char src[100],p[100];
int pos;
clrscr();
printf("Enter the text in which pattern is to be searched:n");
gets(src);
printf("Enter the pattern to be searched:n");
gets(p);
shifttable(p);
pos=horspool(src,p);
if(pos>=0)
printf("n The desired pattern was found starting from position %d",pos+1);
else
printf("n The pattern was not found in the given textn");
getch();
}
Read more Similar C Programs
Data Strucures

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find Binomial Coefficients

C Program to find Binomial Integers without using recursion.

Binomial coefficients are positive integers that are coefficient of any term in the expansion of (x + a) the number of combination’s of a specified size that can be drawn from a given set.

There are many ways to compute the Binomial coefficients. Like,

In this post we will be using a non-recursive, multiplicative formula.

The program is given below:

// C program to find the Binomial coefficient. Downloaded from www.c-program-example.com 
#include<stdio.h> 
void main() {
    int i, j, n, k, min, c[20][20]={0};
    printf("This program is brought to you by www.c-program-example.com\n" );     
    printf("\n Enter the value of n: ");     
    scanf("%d", &n);     
    printf("\n Enter the value of k: ");     
    scanf("%d", &k);
    if(n >= k) {         
        for(i=0; i<=n; i++) {             
            min = i<k? i:k;
            for(j = 0; j <= min; j++) {
                 if(j==0 || j == i) {
                     c[i][j] = 1;
                 } else {
                     c[i][j] = c[i-1][j-1] + c[i-1][j];
                 }
             }
         }
         printf("%d\t",c[n][k]);
         printf("\n");
     } else {
         printf("\n Invalid input \n Enter value n>=k \n");
     }
}

Sample output

Links

C program to check whether a given 5 digit number is palindrome or not

Check for Palindromic number. C program to check whether a given 5 digit number is palindrome or not.

Problem Statement

Write a C program to check if a given 5 digit number is palindrome or not. Take input from the user, check if the given number is a palindromic number and display appropriate message in the end.

A number is said to be a palindromic number if it remains same when reversed. For example 12321 is a palindromic number, where as 12345 is not. You can read more about Palindromic number.

Solution

The program takes input number from the user. Then the digits of the given number are reversed. Reversed number is compared with the original number to check if they are equal. If they are equal, the original number is a palindrome. If they are not equal, then the number is not a palindrome.

The Program

/* C program to check whether a given 5 digit number is palindrome or not
* (c) www.c-program-example.com
*/
#include<stdio.h>
int main() {
int num, rem, i, rev = 0, num1, count = 0;
printf("Enter a five digit number:\n");
scanf("%d", &num);
num1 = num;
// reverse the given number
while(num > 0) {
rem = num % 10;
rev = rem + rev * 10;
num = num / 10;
count++;
}
if(count == 5) {
if(num1 == rev) { // if it's palindrome, reverse & original number will be same
printf("The Given Number %d is Palindrome", num1);
} else {
printf("The Given Number %d is NOT Palindrome",num1);
}
} else {
printf("The given %d number is not a five digit number!",num1);
}
return 0;
}
view raw palindrome.c hosted with ❤ by GitHub

Sample Output

Palindrome number or not

Related Programs

  1. C program to check whether a given string is palindrome or not
  2. Number System

To get regular updates on new C programs, you can Follow @c_program

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to create a subsets using backtracking method.

C Program to find the subsets in the set. We use the backtracking method to solve this problem. Backtracking is the refinement method of Brute-Force method. Backtrack method means it finds the number of sub solutions and each may have number of sub divisions, and solution chosen for exactly one. Backtracking method is a recursive method. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
/*
* You can find this program on GitHub
* https://github.com/snadahalli/cprograms/blob/master/subsets.c
*/
void main()
{
int i,j,n,temp,total=0;
clrscr();
printf("n Enter how many numbers:n");
scanf("%d",&n);
printf("n Enter %d numbers to th set:n",n);
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];
}
printf("n Input the sum value to create sub set:n");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("n The given %d numbers in ascending order:n",n);
for(i=0;i<n;i++)
printf("%d t",w[i]);
if((total<sum))
printf("n Subset construction is not possible");
else
{
for(i=0;i<n;i++)
inc[i]=0;
printf("n The solution using backtracking is:n");
sumset(-1,0,total);
}
getch();
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("n{t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%dt",w[j]);
printf("}n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–>

(c) www.c-program-example.com

C program to implement Depth First Search(DFS)

C program to implement Depth First Search(DFS). Depth First Search is an algorithm used to search the Tree or Graph. DFS search starts from root node then traversal into left child node and continues, if item found it stops other wise it continues. The advantage of DFS is it requires less memory compare to Breadth First Search(BFS).Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("n Enter the adjacency matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("n Graph is connected");
else
printf("n Graph is not connected");
getch();
Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com