Saturday, March 19, 2016

C Program to calculate x^y using recursive function.

/*=================================================================================
 **
 **  File Name     : powRecursion.c
 **  Creation Date : Sat 19 Mar 2016 06:02:15 AM IST
 **  Last Modified : Sat 19 Mar 2016 06:15:27 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
long int power(int x, int y);
int i=1;
long int sum=1;
int main()
{
    printf("\n= = = = = Calculate power using recursive function = = = = =\n");
    int no, pow;
    long int result;
    printf("\nEnter a Number : ");
    scanf("%d",&no);
    printf("Enter the Power : ");
    scanf("%d",&pow);
    result = power(no,pow);
    printf("\n%d to the power %d is: %ld\n",no,pow,result);
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}
long int power(int x, int pow)
{
    if(i<=pow)
    {
        sum=sum*x;
        power(x,pow-1);
    }
    else
        return sum;
}

OutPut:

max@ubuntu:~/cprog$ gcc powRecursion.c -o powRecursion
max@ubuntu:~/cprog$ ./powRecursion

= = = = = Calculate power using recursive function = = = = =

Enter a Number : 2
Enter the Power : 10

2 to the power 10 is: 1024


= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to perform Arithematic Operation using function

/*=================================================================================
**
**  File Name     : mathOpe.c
**  Creation Date : Sat 19 Mar 2016 05:25:39 AM IST
**  Last Modified : Sat 19 Mar 2016 05:43:16 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**================================================================================*/
#include<stdio.h>
int addition(int x, int y);
int subtraction(int x, int y);
int multiplication(int x, int y);
int division(int x, int y);
int main()
{
    printf("\n= = = = = Arithematic Operation using function = = = = =\n");
    int n1, n2, sum, sub, mul, div;
    printf("\nEnter the value of 'n1' : ");
    scanf("%d",&n1);
    printf("Enter the value of 'n2' : ");
    scanf("%d",&n2);
    sum = addition(n1,n2);
    printf("\nThe sum of %d and %d is : %d\n",n1,n2,sum);
    sub = subtraction(n1,n2);
    printf("The subtraction of %d from %d is : %d\n",n2,n1,sub);
    mul = multiplication(n1,n2);
    printf("The multiplication  of %d and %d is : %d\n",n1,n2,mul);
    div = division(n1,n2);
    printf("The division of %d by %d is : %d",n1,n2,div);
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");

}
int addition(int x, int y)
{
    int k;
    k = x+y;
    return k;
}
int subtraction(int x, int y)
{
    int k;
    k = x-y;
    return k;
}
int multiplication(int x, int y)
{
    int k;
    k = x*y;
    return k;
}
int division(int x, int y)
{
    int k;
    k = x/y;
    return k;
}

OutPut:

max@ubuntu:~/cprog$ gcc mathOpe.c -o mathOpe
max@ubuntu:~/cprog$ ./mathOpe

= = = = = Arithematic Operation using function = = = = =

Enter the value of 'n1' : 10
Enter the value of 'n2' : 4

The sum of 10 and 4 is : 14
The subtraction of 4 from 10 is : 6
The multiplication  of 10 and 4 is : 40
The division of 10 by 4 is : 2

= = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Friday, March 18, 2016

C Program to print all prime numbers between any two given numbers

/*=================================================================================
 **
 **  File Name     : primePrint.c
 **  Creation Date : Sat 19 Mar 2016 05:08:00 AM IST
 **  Last Modified : Sat 19 Mar 2016 05:14:34 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = = = = = C Program to print Prime numbers = = = = = =\n");
    int n1, n2, i, j, stat;
    printf("\nEnter the first number : ");
    scanf("%d", &n1);
    printf("Enter the second number : ");
    scanf("%d", &n2);
    printf("\nThe Prime numbers between %d and %d are : \n", n1, n2);
    for(i=n1+1; i<n2; ++i)
    {
        stat=0;
        for(j=2; j<=i/2; ++j)
        {
            if(i%j==0)
            {
                stat=1;
                break;
            }
        }
        if(stat==0)
        printf("%d ",i);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc primePrint.c -o primePrint
max@ubuntu:~/cprog$ ./primePrint

= = = = = = = = = C Program to print Prime numbers = = = = = =

Enter the first number : 2
Enter the second number : 50

The Prime numbers between 2 and 50 are :
3 5 7 11 13 17 19 23 29 31 37 41 43 47

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Write a C Program to check if a string is pallindrome or not

/*=================================================================================
**
**  File Name     : stringPall.c
**  Creation Date : Sat 19 Mar 2016 04:44:11 AM IST
**  Last Modified : Sat 19 Mar 2016 04:59:05 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/
#include<stdio.h>
#include<string.h>
int main()
{
    printf("\n= = = = = C Program to pallindrome check = = = = =\n");
    char string[100];
    int i, len, stat=0;
    printf("\nEnter the String : ");
    scanf("%s",string);
    len = strlen(string);
    for(i=0; i<len; i++)
    {
        if(string[i] != string[len-i-1])
        {
            stat = 1;
            break;
        }
    }
    if(stat == 1)
    {
        printf("\n'%s' is NOT a pallindrome\n",string);
    }
    else
    {
        printf("\n'%s' is a pallindrome\n",string);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc stringPall.c -o stringPall
max@ubuntu:~/cprog$ ./stringPall

= = = = = C Program to pallindrome check = = = = =

Enter the String : manoj

'manoj' is NOT a pallindrome


= = = = = = = = = = = = = = = = = = = = = = = = = =
max@ubuntu:~/cprog$ ./stringPall

= = = = = C Program to pallindrome check = = = = =

Enter the String : abcdcba

'abcdcba' is a pallindrome


= = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to print the fibonacci series upto 'n' terms except first two

/*=================================================================================
 **
 **  File Name     : fibonacci.c
 **  Creation Date : Sat 19 Mar 2016 03:58:49 AM IST
 **  Last Modified : Sat 19 Mar 2016 04:14:17 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = Fibonacci Series upto 'n' terms = = = = =\n");
    int t1, t2, n, sum, count = 0;
    printf("\nEnter the first term : ");
    scanf("%d",&t1);
    printf("Enter the second term : ");
    scanf("%d",&t2);
    printf("Enter the number of terms you want to print : ");
    scanf("%d",&n);
    printf("\nThe first two term of fibonacci series are : %d , %d\n", t1, t2);
    printf("Remaining %d terms are : ",n);
    while (count < n)
    {
        sum = t1 + t2;
        printf("%d  ", sum);
        t1 = t2;
        t2 = sum;
        count++;
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc fibonacci.c -o fibonacci
max@ubuntu:~/cprog$ ./fibonacci

= = = = = Fibonacci Series upto 'n' terms = = = = =

Enter the first term : 3
Enter the second term : 5
Enter the number of terms you want to print : 10

The first two term of fibonacci series are : 3 , 5
Remaining 10 terms are : 8  13  21  34  55  89  144  233  377  610 

= = = = = = = = = = = = = = = = = = = = = = = = = =

Write a C Program to find the sum of factorial of 'n' numbers

/*=================================================================================
**
**  File Name     : sumFact.c
**  Creation Date : Sat 19 Mar 2016 03:38:50 AM IST
**  Last Modified : Sat 19 Mar 2016 03:50:55 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
        printf("\n= = = = = = = C Program to find sum of 'n' factorial = = = = = = =\n");
        int n, i, j, sum=0, fact=1;
        printf("\nEnter the value of 'n' : ");
        scanf("%d",&n);
        for(i=1; i<=n; i++)
        {
                for(j=1; j<=i; j++)
                {
                        fact=fact*j;
                }
                sum=sum+fact;
                fact=1;
        }
        printf("\nThe sum of factorial of 1 to %d is : %d\n",n,sum);
        printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc sumFact.c -o sumFact
max@ubuntu:~/cprog$ ./sumFact

= = = = = = = C Program to find sum of 'n' factorial = = = = = = =

Enter the value of 'n' : 6

The sum of factorial of 1 to 6 is : 873

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
max@ubuntu:~/cprog$ ./sumFact

= = = = = = = C Program to find sum of 'n' factorial = = = = = = =

Enter the value of 'n' : 10

The sum of factorial of 1 to 10 is : 4037913

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Monday, March 14, 2016

C Program to Multiply two Matrix Using Function

/*=================================================================================
 **
 **  File Name     : matMul.c
 **  Creation Date : Mon 14 Mar 2016 03:41:26 AM IST
 **  Last Modified : Mon 14 Mar 2016 05:46:23 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
void insert_ele(int a[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("Enter Element of row=%d , column= %d : ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
}

void mul_mat(int m1[10][10],int m2[10][10],int m3[10][10],int r1,int c1, int r2, int c2)
{
    int i,j,k,sum=0;
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            m3[i][j]=0;
        }
    }
    for(i=1;i<=r1;i++)
    {
        for(j=1;j<=c2;j++)
        {
            for (k=1;k<=c1;k++)
            {
                sum = sum + (m1[i][k] * m2[k][j]);
            }
            m3[i][j]=sum;
            sum=0;
        }
    }
}

void print_mat(int m[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("%d ",m[i][j]);
        }
        printf("\n");
    }
}

main()
{
    int m1[10][10],m2[10][10],m3[10][10],row,col,r1,c1,r2,c2;
    printf("\n= = = = = = Matrix Multiplication = = = = = =\n\n");
    printf("Enter number of rows in first matrix: ");
    scanf("%d",&r1);
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c1);
    insert_ele(m1,r1,c1);
    printf("\nThe elements of first matrix are:\n");
    print_mat(m1,r1,c1);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nEnter number of rows in second matrx: ");
    scanf("%d",&r2);
    while(r2 != c1)
    {
        printf("Error: Number of rows in 2nd matrix must be same as number of columns in 1st matrix\n");
        printf("\nEnter number of rows in second matrx: ");
        scanf("%d",&r2);
    }
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c2);
    insert_ele(m2,r2,c2);
    printf("\nThe elements of the second matrix are:\n");
    print_mat(m2,r2,c2);
   
    /*----------------------------------------------------*/
    mul_mat(m1,m2,m3,r1,c1,r2,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nThe resultant matrix is:\n");
    print_mat(m3,r1,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\n");
}

OutPut:

max@ubuntu:~$ gcc matMul.c -o matMul
max@ubuntu:~$ ./matMul

= = = = = = Matrix Multiplication = = = = = =

Enter number of rows in first matrix: 2
Enter number of colomns in second matrix: 4
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=1 , column= 4 : 4
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 3
Enter Element of row=2 , column= 3 : 2
Enter Element of row=2 , column= 4 : 1

The elements of first matrix are:
1 2 3 4
4 3 2 1
- - - - - - - - - - - - - - - - - - - - - - - - -

Enter number of rows in second matrx: 4
Enter number of colomns in second matrix: 3
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 5
Enter Element of row=2 , column= 3 : 6
Enter Element of row=3 , column= 1 : 3
Enter Element of row=3 , column= 2 : 2
Enter Element of row=3 , column= 3 : 1
Enter Element of row=4 , column= 1 : 4
Enter Element of row=4 , column= 2 : 5
Enter Element of row=4 , column= 3 : 6

The elements of the second matrix are:
1 2 3
4 5 6
3 2 1
4 5 6
- - - - - - - - - - - - - - - - - - - - - - - - -

The resultant matrix is:
34 38 42
26 32 38
- - - - - - - - - - - - - - - - - - - - - - - - -