Thursday, December 22, 2011

C Program for check the String is plaindrom or not.......

/* Plaindrom String */

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

void main()
{
 char str1[10],str2[10],i,l;
 clrscr();
 printf("Eneter the string:");
 gets(str1);

  l=strlen(str1);
  l--;

  i=0;

  while(l>=0)
  {
   str2[i]=str1[l];
   l--;
   i++;
   }
   str2[i]='\0';
   i=strcmp(str1,str2);

   if(i==0)
    printf("\nString is plaindrom");
   else
    printf("\nString is not plaindrom");

   getch();

}

C Program for Factorial useing Recursion....

#include<stdio.h>
#include<conio.h>
long int fact(long int);
 void main()
 { 
   long int  num,f;
   clrscr();
   printf("Input a number: ");
   scanf("%ld",&num);
   f=fact(num);
   printf("\nFactorial is %ld",f);
   getch();
 }
 long int fact(long int n)
 {
    if(n==0)
         return 1;
    else 
             return(n*fact(n-1)); 
 }

C Program for Bubble Sort.....

/************************Bubble Sorting**************************/

 #include<stdio.h> 
 #include<conio.h> 
  
  void main() 
   { 

       int a[100],n,i,j,t;
      clrscr(); 
      printf("\n\n Enter integer value for total no.s of elements to be sorted: "); 
       scanf("%d",&n); 
  
      for( i=0;i<=n-1;i++) 
                   scanf("%d",&a[i]); 
   for(i=n-2;i>=0;i--) 
          { 
             for(j=0;j<=i;j++) 
  
                   { 
                     if(a[j]>a[j+1]) 
                                     { 
                                       t=a[j]; 
                                      a[j]=a[j+1]; 
                                      a[j+1]=t; 
                                     } 
                    } 
            }          
        printf("\n\n Finally sorted array is: "); 
        for( i=0;i<=n-1;i++) 
    printf("\n%d",a[i]);
    getch();
  
   }

C Program for find the armstrong no with in range..

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
 int no,num,arm=0;
 int i,rev=0;
 clrscr();
 for(i=1;i<=500;i++)
{
 num=no=i;
 arm=0;
 rev=0;
 while(no!=0)
    {
     arm=no%10;
     no=no/10;
     rev=rev+(arm*arm*arm);
    }
  if(num==rev)
    printf("\nArmsrtrong no : %d",num);
}
getch();

}

Saturday, December 17, 2011

C Program for Multiplaction of two Matrix...

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

void main()
{
    int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix MAX 10\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix MAX 10\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        }
        printf("You have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Again row wise\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        }
        printf("You have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        if(r1==r2&&c1==c2)
        {
            printf("Now we add both the above matrix \n");
            printf("The result of the addition is as follows;\n");
            for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                {
                    add[i][j]=m1[i][j]+m2[i][j];
                    printf("%d\t",add[i][j]);
                }
                printf("\n");
            }
        }
        else
        {
            printf("Addition cannot be done as rows or columns are not equal\n");
        }
        printf("Now we multiply both the above matrix \n");
        printf("The result of the multiplication is as follows:\n");
       
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
        getch();
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
}

C Program for find a Prime Number...

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

void main()
{
int i,j=2,ch=0;
clrscr();
       printf("\nENTER ANY NUMBER: ");
       scanf("%d",&i);
  while(j<=i/2)
       {
    if(i%j==0)
      {
            printf("%d IS NOT PRIME",i);
            ch=1;
            break;
          }
        else
          {
           j++;
          }
      }
 if(ch==0)
   {
   printf("%d IS PRIME",i);
   }
   getch();

}

C Program for Binary Search....

/* binary Search*/

#include<conio.h>
#include<stdio.h>
int main()
{
  int a[10],i,n,m,c=0,l,u,mid;

  printf("Enter the size of an array->");
  scanf("%d",&n);

  printf("\nEnter the elements of the array in sorted order->");
  for(i=0;i<n;i++)
      scanf("%d",&a[i]);
 
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++)
      printf(" %d",a[i]);
 
  printf("\nEnter the number to be search->");
  scanf("%d",&m);
 
l=0,u=n-1;

  while(l<=u)
    {
      mid=(l+u)/2;
      if(m==a[mid])
    {
          c=1;
              break;
          }
      else if(m<a[mid])
    {
              u=mid-1;
        }
      else
          l=mid+1;
   }

  if(c==0)
      printf("\nThe number is not in the list");
  else
      printf("\nThe number is found at %d",mid+1);
  getch();
}