Showing posts with label for control structure. Show all posts
Showing posts with label for control structure. Show all posts

Tuesday, February 22, 2011

Matrix Program!!

This Program helps us to add a 3*3 matrix. The concept of 'arrays'is used here. For loops are used to get the inputs and print them. To add matrix of order n*n change all the 3 to n.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
 {
  int a[3][3],b[3][3],c[3][3];
  int i,j;
  clrscr();
  printf("\nEnter the first matrix:");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
  printf("\nThe first matrix is");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",a[i][j]);
   }
  printf ("\nEnter the second matrix is");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("\t%d",&b[i][j]);
  printf("\nThe Second Matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",b[i][j]);
   }
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      c[i][j]=a[i][j]+b[i][j];
  printf("\nThe Resultant matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",c[i][j]);
   }
  getch();
}

Monday, February 21, 2011

Factorial Program!!

This program computes the factorial of the given number and displays it. The control structure 'for' is used to determine the factorial. When there is a number of iteration is carried out till a desired output or condition is solved , we go for the 'for' statement.

LOGIC:
 Factroial of 'n' is denoted as 'n!' 

 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
j=1;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=j*i;
}
printf("\n Factorial of %d is :%d",n,j);
getch();
}