Tuesday, 1 August 2017

Fibonacci series using recursion in c

#include <stdio.h>
#include <stdlib.h>

int main()
{
   
   int n,i;
   // Here n is number of terms in series.
   printf("Enter value of n\n");
   scanf("%d",&n);
   printf("Fibonacci series is\n");
   for(i=1;i<=n;i++){
        printf("%d  ",fib(i));
   }


}
int fib(int n){

    if(n==1||n==2){
        return (1);
    }
    else
        return (fib(n-1)+fib(n-2));

}

Addition of polynomial using array and structure in c

#include <stdio.h>
#include <stdlib.h>
struct poly{
    int exp;
    float coeff;
};
int main()
{
    struct poly a[50],b[50],c[50];
    int i,d1,d2;
    int k=0,l=0,m=0;
    printf("Enter the highest degree of 1st polynomial\n");
    scanf("%d",&d1);
    printf("Enter the highest degree of 2nd polynomial\n");
    scanf("%d",&d2);

    printf("Enter the 1st polynomial \n");

    for(i=0;i<=d1;i++){
            printf("enter the coefficient of x^%d\n",i);
            scanf("%f",&a[i].coeff);
            a[k++].exp = i;
    }

     printf("Enter the 2nd polynomial \n");

    for(i=0;i<=d2;i++){
            printf("enter the coefficient of x^%d\n",i);
            scanf("%f",&b[i].coeff);
            b[l++].exp = i;
    }

    printf("\nExpression of 1st polynomial is %.1f",a[0].coeff);
    for(i=1;i<=d1;i++){

        printf("+%.1fx^%d",a[i].coeff,a[i].exp);

    }

     printf("\nExpression of 2nd polynomial is %.1f",b[0].coeff);
    for(i=1;i<=d2;i++){

        printf("+%.1fx^%d",b[i].coeff,b[i].exp);

    }
    //Addition of polynomials

    if(d1>=d2){

            for(i=0;i<=d2;i++){
                c[m].coeff = a[i].coeff+b[i].coeff;
                c[m].exp = a[i].exp;
                m++;
            }
            for(i=d2+1;i<=d1;i++){
                c[m].coeff = a[i].coeff;
                c[m].exp = a[i].exp;
                m++;

            }
    }
    else{
            for(i=0;i<=d1;i++){
                c[m].coeff = a[i].coeff+b[i].coeff;
                c[m].exp = a[i].exp;
                m++;
            }
            for(i=d1+1;i<=d2;i++){
                c[m].coeff = b[i].coeff;
                c[m].exp = b[i].exp;
                m++;
            }
    }

    //Now print the sum of polynomials
    printf("\nExpression of sum of polynomial is %.1f",c[0].coeff);
    for(i=1;i<m;i++){

        printf("+%.1fx^%d",c[i].coeff,c[i].exp);

    }

    return 0;

}

Factorial of a given number using recursion in c


#include <stdio.h>
#include <stdlib.h>


int main()
{
   int n;
   // n is the number you want the factorial to be calculated.
   printf("Enter value of n\n");
   scanf("%d",&n);
   printf("factorial of given number is %d",fact(n));

}
int fact(int n){

    if(n>0){
        return (n*fact(n-1));
    }
    else
        return 1;

}


Matrix into its sparse representation in c

#include <stdio.h>
#include <stdlib.h>
struct element{
    int row,col,value;
}*sparse;
int main()
{
    int n,m,i,j,c=0;
    // here n is row and m is column of matrix.
    // c represents number of non zero elements.
    int **A;
    printf("Enter the value of n\n");
    scanf("%d",&n);
    printf("Enter the value of m\n");
    scanf("%d",&m);
    A = (int**) malloc(n*sizeof(int*));
    for(i=0;i<n;i++){
            A[i] = (int*) malloc(m*sizeof(int));
    }
    printf("Enter the elements of the matrix\n");
    for(i=0;i<n;i++){
             for(j=0;j<m;j++){
                scanf("%d",&A[i][j]);
                if(A[i][j]!=0){
                    c++;
                }
             }
    }
    printf("Entered Matrix is :\n");
    for(i=0;i<n;i++){
             for(j=0;j<m;j++){
                 printf("%d ",A[i][j]);
             }
    printf("\n");
    }
    printf("Number of non zero elements is :%d\n",c);

    if((c+1)*3 <(n*m)){

            sparse = (struct element*) malloc(sizeof(struct element)*(c+1));
            int k=0;
            sparse[k].row = n;
            sparse[k].col = m;
            sparse[k].value = c;
            k=k+1;
            for(i=0;i<n;i++){
                for(j=0;j<m;j++){
                    if(A[i][j]!=0){
                        sparse[k].row = i;
                        sparse[k].col = j;
                        sparse[k].value= A[i][j];
                        k++;
                    }
                }
            }
            printf("Sparse representation of your matrix is\n");
            for(k=0;k<c+1;k++){
                printf("%d %d %d\n",sparse[k].row,sparse[k].col,sparse[k].value);
            }

    }
    else{
        printf("Sparse representation is not efficient\n");
    }
    // Don't forget to free the matrix.

    for(i=0;i<n;i++){
        free(A[i]);

    }
    free(A);
}

 

Copyright @ 2015