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();
}

C Program to print Pascal's Triangle

#include<stdio.h>
void main()
{
int a[10][10];
int i,j,c,n;
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d %d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d",a[i][j]);
}

C Program to solve the Sine and Cosine series

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double sin(double x)
{
double numerator = x;
double denominator = 1.0;
double sign = 1.0;
double sin = 0;

// terms below define the number of terms you want
int terms = 10;
for ( int i = 1 ; i <= 10 ; i++ )
{
sin += numerator / denominator * sign;
numerator *= x * x;
denominator *= i*2 * (i*2+1);
sign *= -1;
}
return sin;
}
getch();
}

C Program to print the ascii equivalent of all chararters in the entered string


#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int ascii_value(char c);
void main()
{

 int i,a;
 char c;
 clrscr();
 printf("Please enter a string.");
 printf(" String will be terminated if you press Ctrl-Z.");
 printf(" STRING:-  ");
 for (i=0;(c=getchar())!=EOF;i++)
 {
  a=ascii_value(c);
  printf("%d%c",a,' ');
 }
 printf("     are the ascii values of the characters of the entered string");
 printf("     respectively.");

 getch();
}

 int ascii_value(char c)
 {
  int a;
  a=(int)c;
  return(a);
 }

C Program to convert a Decimal number to Decimal Number

#include <stdio.h>

void main()
{
int   num, bnum, dec = 0, base = 1, rem ;

printf(“Enter a binary number(1s and 0s)\n”);
scanf(“%d”, &num);           /*maximum five digits */

bnum = num;

while( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}

printf(“The Binary number is = %d\n”, bnum);
printf(“Its decimal equivalent is =%d\n”, dec);

C Program to convert a Decimal number to Binary Number

#include<stdio.h>
void main()
{
    int i,n1,t,w,a[100],i1=0;
    float n,f2;
   
    clrscr();
    printf("[b]PROGRAM FOR DECIMAL TO BINARY CONVERSION[/b]\n");
    printf("******************************************\n");
    printf("\n\nENTER A NUMBER:");
    scanf("%f",&n);
    w=n;
    f2=w;
   
    if(f2==n)
    {
        printf("");f2=0;
    }
    else
    {
        f2=n-f2;
    }
    n1=n;
   
    for(i=0;n1>=1;i++)
    {
        t=n1%2;
        n1=n1/2;
        i1++;
        a[i]=t;
    }
   
    printf("\n\nBINARY EQVALENT=>");
   
    for(i=(i1-1);i>=0;i--)
    {
        printf("%d",a[i]);
    }
    printf(".");
   
    for(i=1;i<=6;i++)
    {
        f2=f2*2;
        if(f2>=1)
        {
            printf("1");
            f2=f2-1;
        }
        else
        {     printf("0");
        }
    }
    getch();
}

Saturday, June 4, 2011

C Program to check whether a number is a Peterson Number or not

#include<stdio.h>
#include<conio.h>
void main()
{
         int n,c,s=0,m,i,f=1;
         clrscr();
         printf("\n ENTER A VALUE : ");
         scanf("%d",&n);
         m=n;
         while(n>o)
         {
              c=n%10;
              for(i=0;i<c;i++)
                   f=f*i;
              s=s+f;
              f=1;
              n=n/10;
         }
         if(s==m)
              printf("\n THE ENTERED NUMBER IS PETERSON
NUMBER.");
          else
              printf("\n THE ENTERED NUMBER IS NOT A
PETERSON NUMBER.");
          getch();
}

C Program without a Header File

It is possible to write a c program without using a header file .

For this we have to do the following :
We will first have to save the program with the extension .c  . eg: wht.c
After this execute the file. The compiler will itself include the header files.
This will work in the case of C++ also.

Example Problem:
void main()
{
int a,b,c;
printf("Enter the values of a and b ");
scanf("%d %d",&a,&b);
c= a+b;
c++
printf("The value is %d",c);
}

After writing the code , save with .c extension and then execute. 
Goodluck.....................................

C Program to run a program without the main() function....

 #include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()

{
printf(" hello ");
}