Oct 21, 2011

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 info@c-program-example.com
* 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("%d\t",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("%d\t",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

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 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 info@c-program-example.com
* 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

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 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 info@c-program-example.com
* 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("\n\nSolution #%d:\n",count);
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
  {
   if(a[i]==j)
    printf("Q\t");
   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 Queens\n");
 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

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 program to cyclically permute the elements of an array.

Example programs to solve the problems of Arrays in C. In This program we, cyclically permute the array elements i.e. arr[0] becomes arr[1], arr[1] becomes arr[2]...so ..on. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>

void main ()
{
 int i,n,number[30];
 printf("Enter the value of the n = ");
 scanf ("%d", &n);
 printf ("Enter the numbers\n");
 for (i=0; i<n; ++i)
 {
  scanf ("%d", &number[i]);
 }
 number[n] = number[0];

 for (i=0; i<n; ++i)
 {
  number[i] = number[i+1];
 }
 printf ("Cyclically permted numbers are given below \n");
 for (i=0; i<n; ++i)
 printf ("%d\n", number[i]);
}
Read more Similar C Programs
Array In C

Simple C Programs

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

To get regular updates on new C programs, you can

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 program to delete the specified integer from the Array.

Example programs to solve the problems of Arrays in C. In This program we, delete the Array Element, if entered element is in the Array, Else gives the Error message. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

                                   */

#include <stdio.h>

void main()
{
 int  vectx[10];
 int  i, n, pos, element, found = 0;

 printf("Enter how many elements\n");
 scanf("%d", &n);

 printf("Enter the elements\n");
 for(i=0; i<n; i++)
 {
  scanf("%d", &vectx[i]);
 }

 printf("Input array elements are\n");
 for(i=0; i<n; i++)
 {
  printf("%d\n", vectx[i]);
 }

 printf("Enter the element to be deleted\n");
 scanf("%d",&element);

 for(i=0; i<n; i++)
 {
  if ( vectx[i] == element)
  {
   found = 1;
   pos = i;
   break;
  }
 }

 if (found == 1)
 {
  for(i=pos; i< n-1; i++)
  {
   vectx[i] = vectx[i+1];
  }

  printf("The resultant vector is \n");
  for(i=0; i<n-1; i++)
  {
   printf("%d\n",vectx[i]);
  }
 }
 else
  printf("Element %d is not found in the vector\n", element);

}         
Read more Similar C Programs
Array In C

Simple C Programs

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

To get regular updates on new C programs, you can

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 program to split the array from perticular position

Example programs to solve the problems of Arrays in C. In This program we, cyclically permute the array elements i.e. arr[0] becomes arr[1], arr[1] becomes arr[2]...so ..on. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>

void main ()
{
 int number[30];
 int i,n,a,j;

 printf ("Enter the value of n\n");
 scanf ("%d",&n);

 printf ("enter the numbers\n");
 for (i=0; i<n; ++i)
  scanf ("%d", &number[i]);

 printf ("Enter the position of the element to split the array \n");
 scanf ("%d",&a);

 for (i=0; i<a; ++i)
 {
  number[n] = number[0];

  for (j=0; j<n; ++j)
  {
   number[j] = number[j+1];
  }
 }

 printf("The resultant array is\n");
 for (i=0; i<n; ++i)
 {
  printf ("%d\n",number[i]);
 }
}           
Read more Similar C Programs
Array In C

Simple C Programs

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

To get regular updates on new C programs, you can

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 program to illustrate the concept of unions

C program to demonstrate unions. unions are like C structures, but every member occupies the same memory region. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
 union number
 {
  int  n1;
  float n2;
 };

 union number x;

 clrscr() ;

 printf("Enter the value of n1: ");
 scanf("%d", &x.n1);
 printf("Value of n1 =%d", x.n1);

 printf("\nEnter the value of n2: ");
 scanf("%d", &x.n2);
 printf("Value of n2 = %d\n",x.n2);

}  
Read more Similar C Programs
Union.

Structures.

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

To get regular updates on new C programs, you can

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 program to find size-of an array.

Example programs to solve the problems of Arrays in C. In This program we, find the size of the array using sizeof() operator. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
int main( )
{

 int i,num;
 printf("\nEnter the hoe many elements you want?\n");
 scanf("%d",&num);

 int a[num];
 printf("\nEnter the %d elements:\n",num);
 for(i=0;i<num;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("Elements in array are:\n");
 for(i=0;i<num;i++)
 {
  printf("%d\n",a[i]);
 }
 printf(" The size of the array is: %d Bytes.",(int)sizeof(a));

 return 0 ;
}
Read more Similar C Programs
Array In C

Simple C Programs

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

To get regular updates on new C programs, you can

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 Program to print Multiplication table.

C Program to print the multiplication table using while statement. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
int main() {
 int num, i = 1;
 printf("\n Enter any Number:");
 scanf("%d", &num);
 printf("Multiplication table of %d: \n", num);
 while (i <= 10) {
  printf("\n %d x %d = %d", num, i, num * i);
  i++;
 }
 return 0;
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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

To get regular updates on new C programs, you can

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 Program to demonstrate Formatting of Integers, Real Numbers and Strings.

C Program to format the outputs of Integers, Floats and Strings. Here we gives the all the format specifiers example. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

main() {
 clrscr();
 int num1 = 12345;
 long num2 = 987654;
 float num3 = 98.7654;
 printf("%d\n\n", num1);
 printf("%10d\n\n", num1);
 printf("%010d\n\n", num1);
 printf("%-10d\n\n", num1);
 printf("%10ld\n\n", num2);
 printf("%10ld\n\n", -num2);
 printf("%7.4f\n\n", num3);
 printf("%f\n\n", num3);
 printf("%7.2f\n\n", num3);
 printf("%-7.2f\n\n", num3);
 printf("%07.2f\n\n", num3);
 printf("%*.*f", 7, 2, num3);
 printf("\n\n");
 printf("%10.2e\n\n", num3);
 printf("%12.4e\n\n", -num3);
 printf("%-10.2e\n\n", num3);
 printf("%e\n\n", num3);
 printf(":%s:\n", "Hello, world!");
 printf(":%15s:\n", "Hello, world!");
 printf(":%.10s:\n", "Hello, world!");
 printf(":%-10s:\n", "Hello, world!");
 printf(":%-15s:\n", "Hello, world!");
 printf(":%.15s:\n", "Hello, world!");
 printf(":%15.10s:\n", "Hello, world!");
 printf(":%-15.10s:\n", "Hello, world!");
 getch();
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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

To get regular updates on new C programs, you can

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!


Oct 20, 2011

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 info@c-program-example.com
* 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

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 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 info@c-program-example.com
* 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 text\n");
 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

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 Program to count the occurence of perticular word in the string

C Program that counts the particular word in the given string. For example in the string " the string is the set of charterers" , Number of occurance of "the" is:2. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>

#include<string.h>

main()

{

 int strln,wordln,i,j,k,flag,count=0;

 char str[200],word[20];

 printf("Enter line of text:\n");

 gets(str);

 printf("Enter the word to count:\n");

 scanf("%s",word);

 strln=strlen(str);

 wordln=strlen(word);

 for(i=0;i<strln;i++)

 {

  if(str[i]==word[0]&&((str[i-1]==' '||i==0)&&(str[i+wordln]==' '||str[i+wordln]=='\0')))

  {

   for(flag=0,k=i+1,j=1;j<wordln;j++,k++)

   {

    if(str[k]==word[j])

    {

     flag++;

    }

   }

   if(flag==wordln-1)

   {

    count++;

   }

  }

 }

 printf("Number of occurence of '%s' = %d\n",word,count);

}

Read more Similar C Programs
Searching in C

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

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 Program to print the pyramid pattern.

C program to print the pyramid pattern. This program prints pyramid of 5 rows using a for loop. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
int main()
{
 int r, c,t=5;

 for ( r = 1 ; r <= 5 ; r++ )
 {
  for ( c = 1 ; c < t ; c++ )
   printf(" ");

  t--;

  for ( c = 1 ; c <= 2*r - 1 ; c++ )
   printf("*");

  printf("\n");
 }


 return 0;
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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

To get regular updates on new C programs, you can

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 Program to demonstrate Ternary condition.

C Program to demonstrate the Ternary conditional operator(?). Ternary operator is a short hand combination of the if-else statement. You can use the ternary operator in initialize lists. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
int main()
{
 int min,x,y;
 printf("Enter two numbers\n");
 scanf("%d%d",&x,&y);
 min=x <= y ? x : y;
 /* 
The ternary operator (?) can be rewritten using the if-else  statement:
if(x<=y)
  {
    min=x;
  }
  else
  {
   min=y;
  }*/
 printf("\n Small number is: %d",min);
 return 0;
} 
Read more Similar C Programs
Learn C Programming

Simple C Programs

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

To get regular updates on new C programs, you can

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!


Oct 19, 2011

C Program to find Binomial Coefficients.

C Program to find Binomial Integers. 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. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/
 
#include
void main()
{
 int i,j,n,k,min,c[20][20]={0};
 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=j)
     printf("%d\t",c[i][j]);
   }
   printf("\n");
  }
 }
 else
  printf("\n Invalid input \n Enter value n>=k \n");
}

Read more Similar C Programs
Learn C Programming

Number System

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

To get regular updates on new C programs, you can

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 program to check whether a given 5 digit number is palindrome or not

C program to check whether a given 5 digit number is palindrome or not. Palindromic number is a number that remains same, when it is reversed or can be read in the same way in either direction. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>

int main()
{
 int num,rem,i,rev=0,num1,count=0;
 printf("Enter the five digit number:\n");
 scanf("%d",&num);
 num1=num;
 while(num>0)
 {
  rem=num%10;
  rev=rem+rev*10;
  num=num/10;
  count++;
 }
 if(count==5)
 {
  if(num1==rev)
  {
   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;
}
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

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 program to convert the given Binary number into its equivalent Decimal,Octal and Hexadecimal numbers.

C Program to convert binary number into its equivalent Decimal, Octal, and Hexadecimal number systems. In this program we convert the binary number to decimal number, then converted that decimal to octal and decimal to hexadecimal. 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>
#include<string.h>
void hexadecimal();
void main()
{
 int   num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j;


 printf("Enter the binary number(1s and 0s)\n");
 scanf("%d", &num);           

 bnum = num;

 while( num > 0)
 {
  rem = num % 10;
  if((rem==0) || (rem==1))
  {
   dec = dec + rem * base;
   num = num / 10 ;
   base = base * 2;
   flag=1;
  }
  else
  {
   flag=0;
   printf("\nEnter binary number\n");
   break;
  }
 }
 if(flag==1)
 {
  printf("The Binary number is = %d\n", bnum);
  printf("Its decimal equivalent is =%d\n", dec);
  dec1=dec;
  dec2=dec1;
  while(dec>0)
  {
   rem=dec%8;
   oct[i]=rem;
   dec=dec/8;
   i++;
   counter++;

  }
  counter--;
  printf("\n Its octal equivalent is:");
  while(counter>=0){
   printf("%d" ,oct[counter]);
   counter--;
  }

  printf("\nIts Hexa Decimal  equivalant is:  ");
  hexadecimal(dec2);

 }
}
void hexadecimal(long n)
{
 long i;
 if(n>0)
 {
  i=n%16;
  n=n/16;
  hexadecimal(n);
  if(i>=10)
  {
   switch(i)
   {
   case 10:
    printf("A");
    break;
   case 11:
    printf("B");
    break;
   case 12:
    printf("C");
    break;
   case 13:
    printf("D");
    break;
   case 14:
    printf("E");
    break;
   case 15:
    printf("F");
    break;
   }
  }
  else
   printf("%ld",i);
 }
}
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

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 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 info@c-program-example.com
* 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("%d\t",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

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 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 info@c-program-example.com
* 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

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 Program to implement Breadth First Search (BFS)

C program to implement Breadth First Search(BFS). Breadth First Search is an algorithm used to search the Tree or Graph. BFS search starts from root node then traversal into next level of graph or tree and continues, if item found it stops other wise it continues. The disadvantage of BFS is it requires more memory compare to Depth First Search(DFS). 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 info@c-program-example.com
* 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],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
 for(i=1;i<=n;i++)
  if(a[v][i] && !visited[i])
   q[++r]=i;
 if(f<=r)
 {
  visited[q[f]]=1;
  bfs(q[f++]);
 }
}
void main()
{
 int v;
 clrscr();
 printf("\n Enter the number of vertices:");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  q[i]=0;
  visited[i]=0;
 }
 printf("\n Enter graph data in matrix form:\n");
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
   scanf("%d",&a[i][j]);
 printf("\n Enter the starting vertex:");
 scanf("%d",&v);
 bfs(v);
 printf("\n The node which are reachable are:\n");
 for(i=1;i<=n;i++)
  if(visited[i])
   printf("%d\t",i);
  else
   printf("\n Bfs is not possible");
 getch();
}
Read more Similar C Programs
Data Structures
Depth 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

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 Program to find the Inverse of the Matrix.

C Program to find the Inverse of a Matrix. To find the Matrix Inverse, matrix should be a square matrix and Matrix Determinant is should not Equal to Zero. if A is a Square matrix and |A|!=0, then AA'=I (I Means Identity Matrix). 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 info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h> 
#include<math.h> 
float detrminant(float[][], float);
void cofactors(float[][], float);
void trans(float[][], float[][], float);
main() {
 float a[25][25], n, d;
 int i, j;
 printf("Enter the order of the matrix:\n");
 scanf("%f", &n);
 printf("Enter the elemnts into the matrix:\n");
 for (i = 0; i < n; i++) {
  for (j = 0; j < n; j++) {
   scanf("%f", &a[i][j]);
  }
 }
 d = detrminant(a, n);
 printf("\nTHE DETERMINANT IS=%2f", d);
 if (d == 0)
  printf("\nMATRIX IS NOT INVERSIBLE\n");
 else
  cofactors(a, n);
}

float detrminant(float a[25][25], float k) {
 float s = 1, det = 0, b[25][25];
 int i, j, m, n, c;
 if (k == 1) {
  return (a[0][0]);
 } else {
  det = 0;
  for (c = 0; c < k; c++) {
   m = 0;
   n = 0;
   for (i = 0; i < k; i++) {
    for (j = 0; j < k; j++) {
     b[i][j] = 0;
     if (i != 0 && j != c) {
      b[m][n] = a[i][j];
      if (n < (k - 2))
       n++;
      else {
       n = 0;
       m++;
      }
     }
    }
   }
   det = det + s * (a[0][c] * detrminant(b, k - 1));
   s = -1 * s;
  }
 }
 return (det);
}

void cofactors(float num[25][25], float f) {
 float b[25][25], fac[25][25];
 int p, q, m, n, i, j;
 for (q = 0; q < f; q++) {
  for (p = 0; p < f; p++) {
   m = 0;
   n = 0;
   for (i = 0; i < f; i++) {
    for (j = 0; j < f; j++) {
     b[i][j] = 0;
     if (i != q && j != p) {
      b[m][n] = num[i][j];
      if (n < (f - 2))
       n++;
      else {
       n = 0;
       m++;
      }
     }
    }
   }
   fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1);
  }
 }
 trans(num, fac, f);
}

void trans(float num[25][25], float fac[25][25], float r)

{
 int i, j;
 float b[25][25], inv[25][25], d;
 for (i = 0; i < r; i++) {
  for (j = 0; j < r; j++) {
   b[i][j] = fac[j][i];
  }
 }

 d = detrminant(num, r);
 inv[i][j] = 0;
 for (i = 0; i < r; i++) {
  for (j = 0; j < r; j++) {
   inv[i][j] = b[i][j] / d;
  }
 }

 printf("\nTHE INVERSE OF THE MATRIX:\n");
 for (i = 0; i < r; i++) {
  for (j = 0; j < r; j++) {
   printf("\t%2f", inv[i][j]);
  }
  printf("\n");
 }
}
Read more Similar C Programs
Matrix Programs
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

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!