Thursday, July 19, 2012

Miscellaneous – III – Vampire No.



Vampire Number :

A vampire number a composite natural number must consists even digit of numbers that can be divided ito two equal parts as x and y. Both parts are know as fangs and both cannot ends with trailing zeroes. A number can be considered a Vampire number when product of the fang produce the same number and it can be in any order.

Example: 1260 is a vampire number, with 21 and 60 as fangs, since 21 × 60 = 1260.
126000 = 210 × 600 is not, as both 210 and 600 have trailing zeroes.

Some other Vampire Numbers are : 1395, 1435, 1530, 1827, 2187, 6880, 102510.



//Checking if a given number is a Vampire Number or not

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


int Digit(int n)
           {
              int c=0;
              while(n>0)
                   {
                        c++;
                        n/=10;
                   }
              return c;
           }
  void sort(char Kc[])
      {
           int i,j;
           char ch;
           for(i=0;i
              {
                   for(j=0;j
                        {
                            if(Kc[j]>Kc[j+1])
                                {
                                     ch=Kc[j];
                                     Kc[j]=Kc[j+1];
                                     Kc[j+1]=ch;
                                }
                        }
              }




//A series of Vampire Numbers

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

int Digit(int n)
           {
              int c=0;
              while(n>0)
                   {

                        c++;
                        n/=10;

                   }
              return c;
           }
  void sort(char Kc[])
      {
           int i,j;
           char ch;
           for(i=0;i
              {
                   for(j=0;j
                        {
                            if(Kc[j]>Kc[j+1])
                                {
                                     ch=Kc[j];
                                     Kc[j]=Kc[j+1];
                                     Kc[j+1]=ch;
                                }
                        }
              }
      }
  void print(int a[],int i)
      {
           int j;
           print("\n\tVampire Numbers : \n"”);
           for(j=0;j
              {
                   printf("\n\t%d ",a[j]);
              }
      }
  void main()
      {
           int i,j,k,c,a[100],t=0,flag;
           char *kStr,*catStr,*tmp;
           clrscr();
           for(i=11; i<=100; i++)
              {
                   for(j=11; j<=100; j++)
                        {
                            flag=0;
                            c=Digit(i*j);
                            if(c%2!=0)
                                {
                                     continue;
                                }
                            if(i%10==0 && j%10==0)
                                {
                                     continue; //both fangs cannot have trailing '0'
                                }
                            k = i * j;
                            catStr[0]='\0';
                            tmp[0]='\0';
                            kStr[0]='\0';
                            itoa(k,kStr,10);
                            itoa(i,catStr,10);
                            itoa(j,tmp,10);
                            strcat(catStr,tmp);
                            sort(kStr);
                            sort(catStr);
                            if(strcmpi(kStr,catStr)==0)
                                {
                                     for(k=0;k
                                           {
                                                if(a[k]==i*j)
                                                    {
                                                          flag=1;
                                                          break;
                                                    }
                                           }
                                      if(flag==0)
                                           {
                                                a[t++]=i*j;
                                           }

                                }
                        }
              }
           print(a,t);
           getch();
      }




Thursday, July 12, 2012

Miscellaneous – II - Permutation


Permutation : The idea of permuting ‘n’ number of objects is basic. For example, when n = 3 and the objects are a,b, and c, the permutations are abc, acb, bca, bac, cba, and cab. Here there are 6 permutations, or 3 · 2 · 1 = 3! of them. The product “3!” is read as “three factorial” and represents the product of all the positive integers between 1 and 3.



//Permutation of Numbers

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

int L=-1,n,num[50],term[50];
  void visited(int k)
      {
           int i;
           num[k]=++L;
           if(L==n)
              {
                   printf("\n\t\t\t");
                   for(i=0;i<n;i++)
                        {
                            printf("%d",term[num[i]]);
                        }
              }
           for(i=0;i<n;i++)
              {
                   if(num[i]==0)
                        {
                            visited(i);
                        }
              }
           L--;
           num[k]=0;
      }

  void main()
      {
           int i=0,j=0;
           clrscr();
           printf("\n\tEnter the Size ");
           scanf("%d",&n);
           printf("\n\tEnte %d Nos.\n",n);
           for(i=0;i<n;i++)
              {
                   num[i]=0;
                   printf("\t\t");
                   scanf("%d",&term[i+1]);
              }
           printf("\n\t\tCombination of : ");
           for(i=1;i<=n;i++)
              {
                   printf("%d",term[i]);
              }
           printf(" is = \n\n ");
           visited(0);
           getch();
      }



Permutation of Characters



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

  void swap(char Vch[], int f, int s)
      {
           char ch = Vch[s];
           Vch[s] = Vch[f];
           Vch[f] = ch;
      }

  void perm(char Vch[], int start, int end)
      {
           int i;
           int range = end - start;
           if (range == 1)
              {
                   printf("\t\t%s\n",Vch);
              }
           else
              {
                   for(i=0; i<range; i++)
                     {
                            swap(Vch, start, start+i);
                            perm(Vch, start+1, end);
                            swap(Vch, start, start+i);
                     }
              }
      }

  void main()
      {
           int n,L;
           char Str[50];
           clrscr();
           printf("\n\t\tEnter A String : ");
           gets(Str);
           L=strlen(Str);
           perm(Str, 0, L);
           getch();
      }




Thursday, July 5, 2012

Miscellaneous - I [Magic Square]



Magic Square or Wondrous Square – A odd size square matrix having all sums are equal to each other, like – sum of each row, each  column, sum of left diagonal and right diagonal are have to equal.



//Magic Square or Wondrous Square

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

  void main()
      {
           int j,i,row,col;
           int f=0,sr=0,sc=0,sdl=0,sdr=0,N;
           int magic[20][20];
           clrscr();
           printf("\n\tSize of Matrix (Preffered an Odd No) : ");
           scanf("%d",&N);
           for(i=0;i<N;i++)
              {
                   for(j=0;j<N;j++)
                        {
                            magic[i][j]=0;
                        }
              }
           row = N-1;
           col = N/2;
           magic[row][col] = 1;
           for (i = 2; i <= N*N; i++)
              {
                   if (magic[(row + 1) % N][(col + 1) % N] == 0)
                        {
                            row = (row + 1) % N;
                            col = (col + 1) % N;
                        }
                   else
                        {
                            row = (row - 1 + N) % N;
                        }
                   magic[row][col] = i;
              }

           for (i = 0; i < N; i++)
              {
                   f=0;
                   sr=sc=0;
                   printf("\t");
                   for (j = 0; j < N; j++)
                        {
                            sr=sr+magic[i][j];
                            sc=sc+magic[j][i];
                            if(i+j==N-1)
                                {
                                     sdr=sdr+magic[i][j];
                                }
                            if(i==j)
                                {
                                     sdl=sdl+magic[i][j];
                                }
                            printf("%3d ",magic[i][j]);
                        }
                   if(sr!=sc)
                        {
                            f=1;
                        }
                   printf("   Sum : Row    %d  Col  %d \n",sr,sc);
              }
                   if(f==0 && sdl!=sdr)
                        {
                            f=1;
                        }
                   printf("\n\t\t\t  Sum Left Dig.   %d    Right Dig   %d",sdl,sdr);
                   if(f==1)
                        {
                            printf("\n\n\t\tAll sums are not equal, it is not a Magic Square");
                        }
                   else
                        {
                            printf("\n\n\t\tIt is a Magic Square");
                        }
           getch();
      }