Fibonacci series program in c using recursion



#include<stdio.h>
 
int Fibonacci(int);
 
main()
{
   int n, i = 0, c;
 
   scanf("%d",&n);
 
   printf("Fibonacci series\n");
 
   for ( c = 1 ; c <= n ; c++ )
   {
      printf("%d\n", Fibonacci(i));
      i++; 
   }
 
   return 0;
}
 
int Fibonacci(int n)
{
   if ( n == 0 )
      return 0;
   else if ( n == 1 )
      return 1;
   else
      return ( Fibonacci(n-1) + Fibonacci(n-2) );
} 

Pascal triangle in c



   1
  1 1
 1 2 1
1 3 3 1
#include<stdio.h>
 
long factorial(int);
 
main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");
 
      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}

sum of the diagonals in a matrix


#include<stdio.h>
int main(){
  int a[10][10],i,j,sum=0,m,n;
  printf(“\nEnter the row and column of matrix: “);
  scanf(“%d %d”,&m,&n);
  printf(“\nEnter the elements of matrix: “);
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf(“%d”,&a[i][j]);
  printf(“\nThe matrix is\n”);
  for(i=0;i<m;i++){
      printf(“\n”);
      for(j=0;j<m;j++){
      printf(“%d\t”,a[i][j]);
      }
 }
 for(i=0;i<m;i++){
     for(j=0;j<n;j++){
          if(i==j)
              sum=sum+a[i][j];
     }
 }
 printf(“\n\nSum of the diagonal elements of a matrix is: %d”,sum);
 return 0;
}

Sample output:

Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2
3
5
6
7
9
2
6
7
The matrix is
2 3 5
6 7 9
2 6 7
Sum of the diagonal elements of a matrix is: 16

pattern programs


      Write a C program to print the following pattern:
         *
      *  *
   *  *  *
*  *  *  *
Program:
/* This is a simple mirror-image of a right angle triangle */
#include <stdio.h>
int main() {
 char prnt = ‘*’;
 int i, j, nos = 4, s;
 for (i = 1; i <= 5; i++) {
for (s = nos; s >= 1; s–) {  // Spacing factor
   printf(”  “);
  }
  for (j = 1; j <= i; j++) {
   printf(“%2c”, prnt);
  }
  printf(“\n”);
  –nos;   // Controls the spacing factor
 }
 return 0;
}

C program to print the Arrow pattern of star using loop | using asterisk create an arrow pattern in C | Printing Patterns of star in C


#include<stdio.h>

int main()
{
char prnt = '*';
int i, j, k, s, sp, nos = 0, nosp = -1;
for (i = 9; i >= 3; (i = i - 2))
{
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
for (sp = nosp; sp >= 1; sp--)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
if (i == 9 && k == 1)
{
continue;
}
printf("%2c", prnt);
}
nos++;
nosp = nosp + 2;
printf("\n");
}
nos = 4;
for (i = 9; i >= 1; (i = i - 2))
{
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
nos++;
printf("\n");
}

return 0;
}

TRIANGLE PATTERN NO.-7


/*
        *
      **
    ***
  ****
*****
*/

/* This is a simple mirror-image of a right angle triangle */

#include <stdio.h>
#include<conio.h>

main()
{
       char prnt = ‘*’;
       int i, j, nos = 4, s;
       for (i = 1; i <= 5; i++)
       {
       for (s = nos; s >= 1; s–)
       printf(” “);

       for (j = 1; j <= i; j++)
       printf(“%c”, prnt);

       printf(“\n”);
       –nos;
       }
       getch();
}