Fibonacci series
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following sequence:
1 , 1 , 2 , 3 , 5 , 13 , 18 ,21 , 34 ,55 , 89 , .....
1 , 1 , 2 , 3 , 5 , 13 , 18 ,21 , 34 ,55 , 89 , .....
- or (oftern in modern usage)
- 0 1 , 1 , 2 , 3 , 5 , 13 , 18 ,21 , 34 ,55 , 89 , ....
- .in mathematical terms any term of fibonacci series is given by
- a(n) = a(n-1) + a(n-2)
-
-
- Non recursive c program to print fibonacci series
-
-
-
- #include<stdio.h>
- void fib(int n)
- {
- int a=1,b=1,s,i;
- if(n==1)
- {
- printf(" %d ",a);
- return;
- }
- if(n==2)
- {
- printf(" %d %d ",a,b);
- return;
- }
- printf(" %ld %d ",a,b);
- for(i=2;i<n;i++)
- {
- s=a+b;
- a=b;
- b=s;
- printf("%d ",s);
- }
- }
- main()
- {
- int num;
- printf("Enter the number of terms : ");
- scanf("%d",&num);
- fib(num);
- }
- recursive c program to print fibonacci series
- #include<stdio.h>
- int fib(int n)
- {
- if(n==0)
- return 0;
- if(n==1)
- return 1;
- return (fib(n-1)+fib(n-2));
- }
- main()
- {
- int i,num;
- printf("Enter the number of terms : ");
- scanf("%d",&num);
- for(i=0;i<num;i++)
- {
- printf(" %d",fib(i));
- }
- }
-
-
-
Program for finding factorial of a large number
-
-
-
- logic:- store the result in array with each element of array as a single digit
- so we first multiply each element with num and then for each element we store them as the rightmost digit remains at it's location while the remaining part is added to the next element and the last element is again stored as single digit and the remaining is stored on next till remaining part becomes 0
#include<stdio.h>
void factorial(int num)
{
int temp,i,k=0,a[200]={0};
a[0]=num--;
while(num)
{
for(i=0;i<=k;i++)
a[i]*=num;
for(i=0;i<=k;i++)
{
temp=a[i]/10;
a[i]=a[i]%10;
a[i+1]=temp+a[i+1];
if(i==k)
{
temp=a[k+1];
while(temp>0)
{
k++;
a[k]=temp%10;
temp=temp/10;
}
i=k;
}
}
num--;
}
for(i=k;i>=0;i--)
printf("%d",a[i]);
}
main()
{
int num;
printf("Enter the number : ");
scanf("%d",&num);
factorial(num);
}
-
Program for multiplication of two matrix
#include<stdio.h>
main()
{
int row1,col1,row2,col2,i,j,k,temp;
printf("Enter the dimension of first matrix : ");
scanf("%d%d",&row1,&col1);
printf("\nEnter the dimension of second matrix : ");
scanf("%d%d",&row2,&col2);
int a[row1][col1],b[row2][col2];
printf("\nEnter the first matrix : \n");
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the second matrix : \n");
for(i=0;i<row2;i++)
for(j=0;j<col2;j++)
scanf("%d",&b[i][j]);
int c[row1][col2];
if(col1!=row2)
{
printf("number of column of first matrix must be equal to number of rows of second matrix");
exit(1);
}
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
temp=0;
for(k=0;k<col1;k++)
temp=temp+a[i][k]*b[k][j];
c[i][j]=temp;
}
}
printf("\nThe resulting matrix is \n");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
printf(" %d ",c[i][j]);
printf("\n");
}
}
Bawal
ReplyDelete