Pages

Tuesday, 4 November 2014

computer programming expected programs



  1. MULTIPLICATION OF TWO MATRIX

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[5][5],b[5][5],c[5][5],i,j,k,r1,c1,r2,c2;
  printf("\nEnter the row and column of first matrix");
  scanf("%d %d",&r1,&c1);
  printf("\nEnter the row and column of second matrix");
  scanf("%d %d",&r2,&c2);
  if(r1==c2)
{
      printf("\nEnter the First matrix->");
      for(i=0;i<r1;i++)
{
            for(j=0;j<c1;j++)
{
           scanf("%d",&a[i][j]);
                        }
            }
      printf("\nEnter the Second matrix->");
      for(i=0;i<r2;i++)
            {
             for(j=0;j<c2;j++)
                        {
           scanf("%d",&b[i][j]);
                        }
            }
     
      for(i=0;i<r1;i++)
            {
            for(j=0;j<c1;j++)
                        {
           c[i][j]=0;
                         for(k=0;i<r1;k++)
{       
                                    c[i][j]=c[i][j]+a[i][k]*b[k][j];
                                     }
      }
  }
}
  printf("\nThe multiplication of two matrix is\n");
  for(i=0;i<r1;i++)
{
             for(j=0;j<c1;j++)
{
           printf("%d\t",c[i][j]);
            }
  }
  getch();
}

OUTPUT
Enter the row and column of first matrix
2            2
Enter the row and column of second matrix
2           2
Enter the First matrix->1      2        3      4
Enter the second matrix->5      6        7      8
The multiplication of two matrix is 
19       22
43       50

  1. PROGRAM TO SWAP TWO NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
   int x, y, temp;
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temp = x;
   x    = y;
   y    = temp;
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
   getch();
}

OUTPUT
Enter the value of x and y
10             20
Before Swapping X=10  Y=20
After Swapping X=20   Y=10

  1. ‘C’ PROGRAM TO PRINT FIBONACCI SERIES
#include<stdio.h>
#include<conio.h>
void main()
{
   int n, first = 0, second = 1, next, c;

   printf("Enter the number of terms\n");
   scanf("%d",&n);
   printf(“\n0”);
   printf(“\n1”); 
   printf("First %d terms of Fibonacci series are :-\n",n);

   for ( c = 2 ; c < n ; c++ )
   {
     
   next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
 getch();
}

OUTPUT
Enter the number of terms  5
First 5 terms of Fibonacci series are
0
1
1
2
3

  1. ‘C’ PROGRAM TO FINE FACTORIAL OF A GIVEN NUMBER

SIMPLE PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
  int c, n, fact = 1;

  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
    fact = fact * c;

  printf("Factorial of %d = %d\n", n, fact);
 getch();
}

OUTPUT
Enter a number to calculate it's factorial 6
Factorial of 6 = 720

  1. USING RECURSIVE FUNCTION (FACTORIAL)
#include<stdio.h>
#include<conio.h>
void main()
     int factorial(int);
 void main()
    {
    int num;
    int result;
     printf("Enter a number to find it's Factorial: ");
    scanf("%d", &num);
    if (num < 0)
       {
        printf("Factorial of negative number not possible\n");
        }
    else
    {
        result = factorial(num);
        printf("The Factorial of %d is %d.\n", num, result);
    }
    getch();
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));
    }
}

OUTPUT
Enter a number to find it's Factorial -5
Factorial of negative number not possible
Enter a number to find it's Factorial 6
The Factorial of 6 IS 720

6.    C’ PROGRAM TO FIND THE ROOT OF QUADRATIC EQUATIONS.
#include <stdio.h>
#include<conio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
void main()
{
  float a, b, c, determinant, r1,r2, real, imag;
  printf("Enter coefficients a, b and c: ");
  scanf("%f%f%f",&a,&b,&c);
  determinant=b*b-4*a*c;
  if (determinant>0)
  {
      r1= (-b+sqrt(determinant))/(2*a);
      r2= (-b-sqrt(determinant))/(2*a);
      printf("Roots are: %.2f and %.2f",r1 , r2);
  }
  else if (determinant==0)
  {
    r1 = r2 = -b/(2*a);
    printf("Roots are: %.2f and %.2f", r1, r2);
  }
  else
  {
    real= -b/(2*a);
    imag = sqrt(-determinant)/(2*a);
    printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
  }
  getch();
}
OUTPUT
Enter coefficients a, b and c: 2.3
4
5.6
Roots are: -0.87+1.30i and -0.87-1.30i








7.      ’C’ PROGRAM TO CHECK PRIME NUMBER

#include <stdio.h>
#include<conio.h>
void main()
{
   int  a,i,n, c = 0;

   printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);

   for ( i = 1 ; i <= n ; i++ )
   {
a=n%i;
if(a==0)
      {
c=c+1;   
      }
   }
   if ( c == 2)
      printf("This is prime.\n");
 else
printf(“not prime”);
   getch();
}


No comments:

Post a Comment