The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)
Showing posts with label Recursion. Show all posts
Showing posts with label Recursion. Show all posts

Tuesday, May 29, 2012

Writing the Given Number in alphabets


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

int main()
{
  int i,b,tmp,nod=0;
  scanf("%d",&b);
  tmp = b;
  while(tmp)
  {
    nod++;      
    tmp = tmp/10;          
  }
  printf("\n");
  while(nod>0)
  {  
   nod--;  
   tmp = 1;      
   for(i=0;i<nod;i++)
    tmp *= 10;
   switch(b/tmp)
   {
     case 0 : printf("Zero "); break;
     case 1 : printf("One "); break;
     case 2 : printf("Two "); break;
     case 3 : printf("Three "); break;
     case 4 : printf("Four "); break;
     case 5 : printf("Five "); break;
     case 6 : printf("Six "); break;
     case 7 : printf("Seven "); break;
     case 8 : printf("Eight "); break;
     case 9 : printf("Nine "); break;
     default: break;              
   }  
   b %= tmp;      
  }
  getch();
  return 0;
}

Friday, April 20, 2012

Dijkstra's Algorithm 2


#include<conio.h>
#include<stdio.h>
#define INFNITY 999

int a[10][10],p[10],d[10],n;
voidprintArray(){

inti,j;
for(i=0;i<n;i++)
    {
for(j=0;j<n;j++)
        {
printf("%d\t",a[i][j]);
        }
printf("\n");
    }
}

voiduAdjacencyMatrix(){
inti,j,c=1,w;
for(i = 0;i < n; i++)
    {
for(j = 0;j < i; j++)
        {
a[i][j] = INFNITY;
a[j][i] = INFNITY;

        }
a[i][i] = INFNITY;

    }

while(c!=-1)
    {
printf ("\ni , j , w :");           
scanf("%d%d%d",&i,&j,&w);
a[i][j]=w;
     //printf ("c? :");
scanf("%d",&c);
    }
printArray();
getch();
}
voidinitializeSingleSource(int i)
{
int j;
for(j=0;j<n;j++)
     {
p[j]=-1;
d[j]=INFNITY;
     }
d[i]=0;
}           
voidtracePath(int a)
{
if(p[a]!=-1)
tracePath(p[a]);
printf("-->%d",a);
}

voidshowAllPaths()
{
int i;
for(i=0;i<n;i++)
    {
if(p[i]!=-1)
printf("\n%d-->%d",p[i],i);    
    }
}

int main()
{
inti,j,k;
printf("enter the number of vertices : ");
scanf("%d",&n);
uAdjacencyMatrix();
printf("enter the  source vertex : ");
scanf("%d",&i);
initializeSingleSource(i);

for(i=0;i<n-1;i++)
    {
for(j=0;j<n;j++)
        {
for(k=0;k<n;k++)
           {
if((a[j][k]!=INFNITY)&& d[k]>d[j]+a[j][k])
                  {
p[k]=j;
d[k]=d[j]+a[j][k];
                  }
           }             

        } 
    }
int t=0;

for(j=0;j<n;j++)
        {
for(k=0;k<n;k++)
           {
if((a[j][k]!=INFNITY)&& d[k]>d[j]+a[j][k])
                  {
                     t=-1;
                  }
if(t==-1)
break;    

           }             
if(t==-1)
break;    

        }
if(t==-1)
printf("\nexiting");
showAllPaths();
printf("\n Enter the vertex for shortest path : ");
scanf("%d",&i);
tracePath(i);
getch();
return(0);
}

Tuesday, April 10, 2012

Travelling Salesperson Problem


#include<stdio.h>
#include<conio.h>
#define max 10
#define INFINITY 1000

int w[max][max];
intn_size;
int p[max];
ints_size;
int stack[max];
int route[max];

int min;

voidin_dat()
{
printf("\n Enter the number of nodes : ");
scanf("%d",&n_size);
fflush(stdin);

int i;

for(i=0;i<n_size;i++)
 {
w[i][i] = 0;
for(int j=i+1;j<n_size;j++)
  {
printf("\n\n Enter the weight of edge '%c' and '%c': ",65+i,65+j);
scanf("%d",&w[i][j]);
w[j][i] = w[i][j];
  }
 }
}

voiddis_dat()
{
printf("\n The Path adjacent matrix \n");
printf("\n ");

for(int i=0;i<n_size;i++)
 {
printf("\n");
for(int j=0;j<n_size;j++)
  {
printf("\t %d",w[i][j]);

  }
 }
getch();
fflush(stdin);
}

voidfindshort(intsr,intct,intss)
{

if(ct == 0)
  {
inttdst=0;    
 //  printf("\n Checking ss= %d sr='%c' :",ss,65+sr);   
   // for(int j=0;j<ss;j++)    
   //   {
    //    printf(" %c",65+stack[j]);   
     // }

for(int k=0;k<ss-1;k++)
    {
tdst += w[stack[k]][stack[k+1]];

     //printf(" p[%c]=%c",65+stack[k],65+stack[k+1]);
    }
if(min >tdst)
    {
min = tdst;
for(int l=0;l<ss+n_size-1;l++)
      {
route[l] = stack[l];

       //printf(" p[%c]=%c",65+stack[k],65+stack[k+1]);
      }
    }
  }
else
  { 
  //int ret = -1;
  //int min = INFINITY;
  //inttdst;
for(int i=0;i<n_size;i++)
  {
intind = 0;                    
if(w[sr][i] != 0)
    {
    //  printf("\n Checking ss= %d sr='%c' i='%c' :",ss,65+sr,65+i);         
for(int j=0;j<ss;j++)    
      {
      //  printf(" %c",65+stack[j]);   
if(stack[j] == i)
        {
ind = -1;         
        }    
      } 
     // printf(" (%d)",min); 

if(ind != -1)
      {
      // printf(" .. VALID .. ");     
stack[ss] = i;     
       //ret = 0;    
       //printf(" w[%c][%c] = %d",65+sr,65+i,w[sr][i]);    
findshort(i,ct-1,ss+1);      
       } 
      }
    }
  } 

}

void MSG()
{
chars,d;
intsi,di;   
printf("\nEnter the source node : ");
scanf("%c",&s);
fflush(stdin);
si = (int) s-65;
stack[0] = si;
s_size = 1;
min = INFINITY;
findshort(si,n_size-1,1);
printf("\n The shortest distance from '%c' : %d",s,min);
getch();
printf("\n The shortest path : ");
for(int k=0;k<n_size;k++)
  {
printf(" %c",65+route[k]);
  }
getch();
}

int main()
{
// clrscr();
in_dat();
dis_dat();
MSG();
return 0;
}

Wednesday, April 4, 2012

Palindrome of Number


#include<stdio.h>

int chk_pal(long int);

int main()
{
 int t,res;
 long num;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%ld",&num);
  while(num <= 1000000)
  {
   res = chk_pal(++num);
   if(res == 1)
    break;        
  }  
  if(num <= 1000000 && res == 1)
   printf("%d\n",num);
         
 }
   
 return 0;  
}

int chk_pal(long int i)
{
 long tmp,rev = 0;
 int j=0;
 tmp = i;
 while(i!=0)
 {
  rev = (long) rev*10 + (i%10);              
  i /= 10;      
 }
  if(rev == tmp)
    return 1;
  else
    return 0;  
}

Sunday, March 18, 2012

Find Palindrome of String


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

char a[20];
int l;

int palin(int num)
{
 if(num>l/2)
  return 1;
 if(a[num] == a[l-num-1])
  return palin(num+1);
 else
  return 0;
}


void main()
{
 clrscr();
 printf("Enter a String : ");
 gets(a);
 l = strlen(a);
 int ret = palin(0);
 if(ret == 1)
  printf("\n Palindrome");
 else
  printf("\n Not Palindrome");
 getch();
}

Thursday, September 1, 2011

The 3n+1 Problem


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

void _3nplus1(int n,int&pt)
{
 pt++;
 if(n != 1)
 {
  if(n % 2 == 0)
    _3nplus1(n/2,pt);
  else
    _3nplus1(3*n+1,pt);
 }
}

void main()
{
 clrscr();
 int i,j,k=0,l=0;
 scanf("%d %d",&i,&j);

 do
 {
  _3nplus1(i,k);
  i++;
  if(k > l)
   l = k;
  k = 0;
 }while(i<=j);
 printf("\n\n%d",l);
 getch();
}

Wednesday, January 26, 2011

Infix-Postfix Conversion and Expression Evalution



#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#define max 50

char infx[max];
char pstfx[max];
char stack[max];
int nstore[max];
int nstack[max];

int top;
int cnt = 0;

int set_prcdnce(char ch)
{
 switch(ch)
 {
 case '^' : return 3;
 case '*' : return 2;
 case '/' : return 2;
 case '+' : return 1;
 case '-' : return 1;
 default  : return 0;
 }
}

int evaluate(char opt)
{
 int a,b;
 b = nstack[top]; top-=1;
 a = nstack[top];
 switch(opt)
 {
 case '^' : return pow(a,b);
 case '*' : return a*b;
 case '/' : return a/b;
 case '+' : return a+b;
 case '-' : return a-b;
 }
}

void stck_stat(char ch,int cho)
{
 int i;
 if(isalpha(ch) && cho == 2)
  {
  printf("  %d \t",nstore[cnt]);  cnt++;
  }
 else
  printf("  %c \t",ch);
 for(i=0;i<=top;i++)
  {
  if(cho==1)
   printf(" %c",stack[i]);
  else
   printf(" %d",nstack[i]);
  }
}

void main()
{
clrscr();
int flag,i,tb=0,j=0;
int cpd,ppd;
 do
 {
  flag = 0;
  printf("\n Enter the infx expression : ");
  gets(infx);
  if(strlen(infx) > max )
  {
   printf("\n\n WARNING : Exceeding the limits !! \n\n");
   flag = 1;
  }
  for(i=0;infx[i]!='\0';i++)
   {
    if(infx[i]=='(')
     tb++;
    if(infx[i]==')')
     tb--;
   }
  if(tb != 0)
  {
   printf("\n\n WARNING : Unequal Right n Left Brackets in the expression !! \n\n");
   flag = 1;
  }
 }while(flag == 1);
printf("\n\n The expression in its infix form : ");
puts(infx);
int x = strlen(infx);
infx[x] = ')';   infx[x+1] = '\0';
x+=1;
top = 0;
stack[top] = '(';
printf("\n\n     Element\t  Stack Status\t  Postfix Expression");
printf("\n     -------- \t ---------------  -------------------- \n\n");
for(i=0;i<strlen(infx);i++)
   {
    switch(infx[i])
    {
    case '(' :  top += 1;
stack[top] = infx[i];
break;
    case ')' :  while(stack[top] != '(')
{
pstfx[j] = stack[top];
top -= 1; j++;
}
top -= 1;
break;
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  cpd = set_prcdnce(infx[i]);
ppd = set_prcdnce(stack[top]);
if(cpd < ppd)
{
while(cpd<=ppd)
{
if(top == -1) break;
pstfx[j] = stack[top];
top -= 1; j++;
ppd = set_prcdnce(stack[top]);
}
}
top += 1; stack[top] = infx[i];
break;
    default :   pstfx[j] = infx[i]; j++;
break;
    }
   printf("  %-2d. ",i+1);
   stck_stat(infx[i],1);
   gotoxy(40,11+i);
   puts(pstfx);
   }
pstfx[j] = '\0';
printf("\n\n The expression in postfix form : ");
puts(pstfx);
j=0;
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("\n Enter value for %c :",pstfx[i]);
     scanf("%d",&nstore[j]);  j++;
   }
 }

j=0;
printf("\n\n Now, The postfix expression becomes as : ");
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("%d,",nstore[j]);  j++;
   }
  else
   printf("%c,",pstfx[i]);
 }
printf("\n\n Evaluating the stack as follows : ");
printf("\n\n Element Check \t Stack Status");
printf("\n ----------- \t ------------ \n");
j = 0;   top = -1;
for(i=0;i<strlen(pstfx);i++)
 {
 printf("\n %-2d ",i+1);
 switch(pstfx[i])
    {
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  nstack[top] = evaluate(pstfx[i]);
break;
    default :   top +=1; nstack[top] = nstore[j]; j+=1;
break;
    }
 stck_stat(pstfx[i],2);
 }
printf("\n\n After, Evaluating the expression the result is : %d",nstack[top]);
getch();
}