Tuesday, June 28, 2011

C Program to show a table of any number using function

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

void main()
{
void table();
clrscr();

table();
getch();
}
void table()
{
int n,i,r;
printf("enter a no to know table: ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf("%d*%d=%d\n",n,i,r);
}
}

C Program to illustrate the use of SHIFT Operator

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

void main()
{
int x,y;
clrscr();
printf("Read the integer from keyboard :- ");
scanf("%d",&x);
x<<=3;
y=x;
printf("\nThe left shifted data is = %d ",y);
getch();
}

C Program to use Bitwise AND Operator between two integers

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

void main()
{
int a,b,c;
clrscr();
printf("Read the integers from keyboard:- ");
scanf("%d %d",&a,&b);
c=a&b;
printf("\nThe Answer after ANDing is:  %d ",c);
getch();
}

C Program to add two numbers using pointers

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

void main()
{
int *p1,*p2,sum;
clrscr();
printf("enter two no's: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1+*p2;
printf("sum=%d",sum);
getch();
}

Wednesday, June 22, 2011

C Program to solve the 8 Queens Problem......

#include<stdio.h>
static int t[10]={-1};
void queens(int i);
int empty(int i);

void print_solution();
int main()
{
queens(1);
print_solution();
return(0);
}

void queens(int i)
{
for(t[i]=1;t[i]<=8;t[i]++)
{
if(empty(i))
{
if(i==8)
{
print_solution();
/* If this exit is commented, it will show ALL possible combinations */
exit(0);
}
else
{
// Recurse!
queens(i+1);
}

}// if
}// for
}


int empty(int i)
{
int j;
j=1;

while(t[i]!=t[j] && abs(t[i]-t[j])!=(i-j) &&j<8)j++;
return((i==j)?1:0);
}


void print_solution()
{
int i;
for(i=1;i<=8;i++)printf("\nt[%d] = [%d]",i,t[i]);
}

Thursday, June 9, 2011

C Program to print various STAR PATTERNS

1)









#include <stdio.h>
int main()
{
   char prnt = '*';
   int i, j, nos = 4, s;
   for (i = 1; i <= 5; i++)
  {
      for (s = nos; s >= 1; s--)
      { 
          // Spacing factor  
              printf("  "); 
      } 
      for (j = 1; j <= i; j++)
      {
          printf("%2c", prnt);
      } 
       printf("\n");  --nos;   // Controls the spacing factor
   }
 return 0;
}

2)










#include<stdio.h>
int main() 
{ 
          char prnt = '*'; 
          int i, j, k, s, c = 1, nos = 9;
          for (i = 1; c <= 4; i++) 
         {     // As we want to print the columns in odd sequence viz. 1,3,5,.etc  
                   if ((i % 2) != 0)  
                   {  
                           for (j = 1; j <= i; j++) 
                           { 
                                 printf("%2c", prnt);
                           }for (s = nos; s >= 1; s--) 
                             { //The spacing factor   
                                  if (c == 4 && s == 1) 
                                  {     break;    }   
            printf("  ");   }  
            for (k = 1; k <= i; k++)
           {   
                 if (c == 4 && k == 5) 
                {     break;    }   
                 printf("%2c", prnt); 
             }   printf("\n"); 
                   nos = nos - 4;  // controls the spacing factor   
                  ++c;  
                   } 
            } 
        return 0;
}

3)
















#include<stdio.h>
int main() 
{ 
       char prnt = '*'; 
int i, j, k, s, p, r, nos = 7;  
for (i = 1; i <= 5; i++)
{  for (j = 1; j <= i; j++) 
{
       if ((i % 2) != 0 && (j % 2) != 0)
       {
       printf("%3c", prnt);
       }else if ((i % 2) == 0 && (j % 2) == 0)
       {
           printf("%3c", prnt);
        }
       else 
      {
             printf("   ");
       }
       }
       for (s = nos; s >= 1; s--) 
      {  // for the spacing factor   printf("   ");  
       }  for (k = 1; k <= i; k++) 
           { //Joining seperate figures
             if (i == 5 && k == 1)  
                 { continue;}
             if ((k % 2) != 0)
               {printf("%3c", prnt);}
            else {printf("   ");}
            }
             printf("\n");nos = nos - 2;   // space control
           } 
               nos = 1;  // remaining half..
            for (p = 4; p >= 1; p--) 
          {  
           for (r = 1; r <= p; r++) 
             {
                    if ((p % 2) != 0 && (r % 2) != 0) 
                    {printf("%3c", prnt);
                     }
                    else if ((p % 2) == 0 && (r % 2) == 0) 
                   {
                         printf("%3c", prnt);
                   }
                   else 
                   {printf("   ");
                   }
                  }
                 for (s = nos; s >= 1; s--) 
                {   
                        printf("   ");  
                } 
                for (k = 1; k <= p; k++) 
               {   if ((k % 2) != 0) 
                     {    printf("%3c", prnt);
                      }else {    printf("   ");   }
               }  nos = nos + 2;  // space control 
               printf("\n");
               }
              return 0;
}

4)















#include<stdio.h>
 int main() 
{ 
char prnt = '*';
int i, j, s, nos = 0; 
for (i = 9; i >= 1; (i = i - 2))
{ 
for (s = nos; s >= 1; s--) 
{  
printf("  ");
} 
for (j = 1; j <= i; j++) 
{   if ((i % 2) != 0 && (j % 2) != 0) 
{    printf("%2c", prnt); 
 } else 
{    
printf("  "); 
  } 
}
  printf("\n");  nos++; 
}
nos = 3;
for (i = 3; i <= 9; (i = i + 2)) 
{for (s = nos; s >= 1; s--) 
{   printf("  ");
  }  for (j = 1; j <= i; j++) 
{   
if ((i % 2) != 0 && (j % 2) != 0)
{  
  printf("%2c", prnt); 
  } 
else
{  
  printf("  "); 
  } 
} 
nos--;
  printf("\n");
}
return 0;
}

5)



















#include<stdio.h>
int main()
{
char prnt = '*';
int i, j, k, s, nos = 4; 
for (i = 1; i <= 5; i++) 
{
for (s = nos; s >= 1; s--)
{ 
  printf("  ");
  } 
for (j = 1; j <= i; j++) {   printf("%2c", prnt);  } 
for (k = 1; k <= (i - 1); k++) 
{if (i == 1) {     continue;}
printf("%2c", prnt);
}
printf("\n"); 
  nos--;
}
nos = 1;
for (i = 4; i >= 1; i--)
{ 
for (s = nos; s >= 1; s--)
{   printf("  ");  } 
for (j = 1; j <= i; j++)
{   printf("%2c", prnt);  }
  for (k = 1; k <= (i - 1); k++) 
{   printf("%2c", prnt);  }
  nos++;  printf("\n");
}
nos = 3; 
for (i = 2; i <= 5; i++)
{
if ((i % 2) != 0) 
{
for (s = nos; s >= 1; s--) 
{    printf("  ");   }
   for (j = 1; j <= i; j++) 
{    printf("%2c", prnt);   }
  }
  if ((i % 2) != 0) 
{   printf("\n");   nos--;
  }
}
return 0;
}

// SOrry for this post . Actually the page setup was not working properly . That is why the programs are having a bit of heck. But I guarantee that once you feed them inot the compiler , they will give you the required output.

Please let me know if there is any error.

Tuesday, June 7, 2011

C Program to check whether a given matrix is symmetric or not

/* Concept:-  A matrix is symmetric if its transpose is same as the matrix itself*/

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

//write matrix

void write_mat(int a[][10],int n)
{
int i,j;
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%10d",a[i][j]);
printf("\n");
}
}

//read matrix

void read_mat(int a[][10],int n)
{
int i,j;
printf("Enter %d X %d matrix below:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}

//main function

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

//accept matrix

printf("Enter size of square matrix");
scanf("%d",&n);
read_mat(a,n);

//check if the matrix is symmetric or not

for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i][j]!=a[j][i])
{
printf("Not symmetric");
return;
}
printf("Symmetric");
getch();
}