Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Wednesday, June 13, 2012

C Programs Explained -2


/* Pascal triangle */

#include

long fact(int);
int main(){
    int line,i,j;

    printf("Enter the no. of lines: ");
    scanf("%d",&line);

    for(i=0;i
         for(j=0;j
             printf(" ");

         for(j=0;j<=i;j++)
             printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
         printf("\n");
    }
    return 0;
}

long fact(int num)
{
    long f=1;
    int i=1;
    while(i<=num)
{
         f=f*i;
         i++;
  }
  return f;
 }

Sample output:

Enter the no. of lines: 8
       1
      1 1
     1 2 1
    1 3 3 1
   1 4 6 4 1
  1 5 10 10 5 1
 1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
 


Cosine series :

#include

#include






#include

  



/*function prototypes*/

int factorial(int number);


int cosine(int value);

  



int main(void)

{


    int random;


    int cosineValue;


      

  


    printf("Enter cosine value");

    scanf("%d",&cosineValue);



    printf("The cosine value is %d",cosine(cosineValue));

      


    return 0;

}/*end main */


  

  


int factorial(int number)

{


    if(number<=1)

    {


        return 1;

    }else


    {

        return (number*factorial(number-1));


    }//end if...

  


}

  


int cosine(int value)

{


    int sum = 1;

    int i;


    int term=1;

    int power;


    int fact;

      


    for(i=0;i<=5;i++)

    {



          

        value = value*(-1);


        power = pow(value,i*2);

        fact=factorial(2*i);


        sum=sum+(power/fact); 

    }


    return sum; 

}

 



How to find inverse of a matrix in C code to find inverse of a matrix Inverse of a 3x3 matrix in c

#include

int main(){

  int a[3][3],i,j;
  float determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

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

  for(i=0;i<3;i++)
      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

   printf("\nInverse of matrix is: \n\n");

   for(i=0;i<3;i++)
{
      for(j=0;j<3;j++)

           printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
       printf("\n");
   }

   return 0;
}

Output :

Enter the 9 elements of matrix:
3
5
2
1
5
8
3
9
2

The matrix is

3       5       2
1       5       8
3       9       2
Inverse of matrix is:

0.70    -0.25   0.07
-0.09   -0.00   0.14
-0.34   0.25    -0.11

Exponential series :

int main ()
{
    int b,i;
    float a,c;
  
    printf("\nEnter base value a: ");
    scanf("\n%f", &a);
    printf("\nEnter exponent value b: ");
    scanf("\n%i\n", &b);
  
    c=1;
    i=1;    
  
    while(i<=b)
    {
        c*=a;   
        i++;
    }
      
  
    printf("\nThe answer is %.2f", c);
  
    return(0);
}





Recursive function for factorial in c

#include
int fact(int);
int main()
{
  int num,f;

  printf("\nEnter a number: ");

  scanf("%d",&num);

  f=fact(num);

  printf("\nFactorial of %d is: %d",num,f);

  return 0;
}

int fact(int n)
{
   if(n==1)
       return 1;
   else
       return(n*fact(n-1));
 }
 



Code to find determinant of a matrix

C program to calculate determinant of a matrix



#include
int main(){
  int a[3][3],i,j;
  int determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

  for(i=0;i<3;i++)
      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

  printf("\nDeterminant of matrix is: %d",determinant);

   return 0;
}



C code for Determinant of 2X2 matrix:

#include
int main(){
  int a[2][2],i,j;
  long determinant;

  printf("Enter the 4 elements of matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

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

  determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];

  printf("\nDeterminant of 2X2 matrix: %ld",determinant);
 
   return 0;
}

Output :

Enter the 4 elements of matrix: 4
8
3
9

The matrix is

4       8
3       9

Determinant of 2X2 matrix: 12

 



C code for Determinant of 3X3 matrix:

#include

int main(){

  int a[3][3],i,j;

  long determinant;



  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

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

  determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]);

  printf("\nDeterminant of 3X3 matrix: %ld",determinant);
 
   return 0;
}

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9

The matrix is

1       2       3
4       5       6
7       8       9

Determinant of 3X3 matrix: 0
 

Alogrithm:
Determinant is possible only for square matrixes i.e. n by n matrixes.
Determinant of 2 by 2 matrix:









Determinant of matrix has defined as: ad – cb

Determinant of 3 by 3 matrix:

Determinant of matrix has defined as: a00(a11*a22 – a21*a12) + a01(a10*a22 – a20*a12) + a02(a10*a21 – a20*a11)




Code for swapping in c


#include
int main()
{
    int a,b,temp;
   
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    printf("Before swapping: a = %d, b=%d",a,b);

    temp= a;
    a=b;
    b=a;
    printf("\nAfter swapping: a = %d, b=%d",a,b);

    return 0;
}

C program for swapping of two numbers using pointers

#include

int main(){

    int a,b;
    int *ptra,*ptrb;
    int *temp;

    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    printf("Before swapping: a = %d, b=%d",a,b);

    ptra = &a;
    ptrb = &b;

     temp = ptra;
    *ptra = *ptrb;
    *ptrb = *temp;

    printf("\nAfter swapping: a = %d, b=%d",a,b);
    return 0;
}

Sample output:

Enter any two integers: 5 10
Before swapping: a = 5, b=10
After swapping: a = 10, b=10


Swapping program in c using function

#include

void swap(int *,int *);
int main()
{

    int a,b;
   
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    printf("Before swapping: a = %d, b=%d",a,b);

    swap(&a,&b);

    printf("\nAfter swapping: a = %d, b=%d",a,b);
    return 0;
}

void swap(int *a,int *b)
{
    int *temp;
    temp = a;
    *a=*b;
    *b=*temp;
}

Sample output:
Enter any two integers: 3 6
Before swapping: a = 3, b=6
After swapping: a = 6, b=6
c