Saturday, December 4, 2010

C program to find the sum of two matrix

Program Code

//C program to find the sum of two matrix
#include < stdio.h >
#include< conio.h >

void main()
{
int a[3][3], b[3][3], c[3][3], i, j;
clrscr();
printf("Enter the elements of first 3x3 matrix");
for (i = 0;i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of second 3x3 matrix");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\nSum of the two matrices is as follows:");
for (i = 0;i < 3; i++)
{
printf("\n");
for (j = 0; j < 3; j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("%d \t", c[i][j]);
}
}
getch();
}


Input :
Enter the elements of first 3x3 matrix
1
2
3
4
5
6
7
8
9
Enter the elements of second 3x3 matrix
1
2
3
2
3
1
3
1
2


Ouput :
Sum of the two matrices is as follows
2 4 6
6 8 7
10 9 11


C program to find transpose of a matrix

Hello I want to discuss about the c program for transpose of the matrix.
Transpose matrix means interchange the rows with the columns and columns with the rows.

Program Code


#include < stdio.h >
#include < conio.h >
void main()
{
int arr[3][3],i,j;
clrscr();
printf("Enter elements for the array \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Original array entered by the user is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\n Transpose of the array is \n");
for(i=0;i < 3;i++)
{
for(j=0;j < 3;j++)
printf("%d ",arr[j][i]);
printf("\n");
}
getch();
}

Input:


Enter elements for the array
1
2
3
4
5
6
7
8
9

Output:


Original array entered by the user is
1 2 3
4 5 6
7 8 9

Transpose of the array is
1 4 7
2 5 8
3 6 9


Thursday, December 2, 2010

C Program For Floyd's Triangle

What is a Floyd's Triangle
It is a pattern of numbers arranging in this format.
Format of numbers like
1
2 3
4 5 6
7 8 9 10


Program Code


//C program to print Floyd’s triangle
#include < stdio.h>
#include < conio.h>
void main()
{
int i , j , r ;
int num=0 ;
clrscr();
printf(“How many rows you want in the triangle:”);
scanf(“%d”,&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
{
printf(“\t%d”,++num);
}
printf(“\n”);
}
getch();
}

Input/Output


floyd triangle

Sunday, November 28, 2010

C Program For Fibonacci Series

Hello everybody I want to discuss about How to generate a Fibonacci series in C programming.

What is Fibonacci Series?


The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
In mathematics it is defined by recurrence relation.

Program Code


  


//C Program for generate Fibonacci series
#include "stdio.h"
#include "conio.h"
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}

Input\Output