Sunday, February 19, 2012

String concatenation using pointer


/* A program to concatenate two string using pointer  */

#include<stdio.h>

void concatenate_string(char*, char*);

int main()
{
    char original[100], add[100];

    printf("Enter source string\n");
    gets(original);

    printf("Enter string to concatenate\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);

    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;

   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Matrix multiply using pointer


/*A program to multiply two matrices using pointer */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;
    int *ptr1,*ptr2,*ptr3;
    ptr1=a;ptr2=b;ptr3=c;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",ptr1+i*10+j);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",ptr2+i*10+j);
            }


        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=*(ptr1+i*10+k)**(ptr2+k*10+j);
            *(ptr3+i*10+j)=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",*(ptr3+i*10+j));
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Matrix multiply


/* A program to multiply two matrices */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",&a[i][j]);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",&b[i][j]);
            }
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=a[i][k]*b[k][j];
            c[i][j]=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",c[i][j]);
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Sunday, January 15, 2012

sum using recursive function


/* A program to find the sum of given non-negative integer numbers using a recursive function */

#include<stdio.h>

int sum(int a)
{
    scanf("%d",&a);
    if(a<=0)
        return a; /*can return zero value as well*/
    else
        return(a+sum(a));
}
int main()

{
    int a;
    printf("enter numbers\n");
    printf("sum= %d",sum(a));
    return 0;

}

Saturday, January 14, 2012

Vowel count


/*A program to  Count vowel*/

#include<stdio.h>

int count_vowels(char []);
int check_vowel(char);

main()
{
char array[100];
int c;

printf("Enter a string\n");
gets(array);

c = count_vowels(array);

printf("Number of vowels: %d\n", c);

return 0;
}

int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;

do
{
d = a[c];

flag = check_vowel(d);

if ( flag == 1 )
count++;

c++;
}while( d != '\0' );

return count;
}

int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */

if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;

return 0;
}

Sunday, January 8, 2012

Add two matrices


/*A program to add two matrices*/

#include<stdio.h>

int main()
{
   int m, n, c, d, mat1[10][10], mat2[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&mat1[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d",&mat2[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = mat1[c][d]+ mat2[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t",sum[c][d]);

      printf("\n");
   }
   return 0;
}

largest element of an array


/*A program to find largest number in an array*/

#include<stdio.h>

int main()
{
    int array[10], large, c, location = 1;
    printf("Enter 10 integers\n");

    for ( c = 0 ; c < 10 ; c++ )
        scanf("%d", &array[c]);

    large = array[0];

    for ( c = 1 ; c < 10 ; c++ )
    {
        if ( array[c] > large )
        {
           large = array[c];
           location = c+1;
        }
    }

    printf("Largest number is %d at location number %d.\n",  large,location);
    return 0;
}

Saturday, January 7, 2012

sorting in ascending order


/*A program to sort integers in ascending order */

#include<stdio.h>

int main()
{
   int array[100], n, c, d, temp, k;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
       scanf("%d", &array[c]);

   for ( c = 1 ; c < n ; c++ )
   {
       for ( d = 0 ; d < c  ; d++ )
       {
           if ( array[c] < array[d] )
           {
              temp = array[d];
              array[d] = array[c];

     for ( k = c ; k > d ; k-- )
        array[k] = array[k-1];

     array[k+1] = temp;
           }
       }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
       printf("%d\n", array[c]);

   return 0;
}

Palindrome string


/*A program to check whether a string ia a palindrome or not*/

#include <stdio.h>

int main()
{
   char text[100];
   int begin, middle, end, length = 0;

   gets(text);

   while ( text[length] != '\0' )
      length++;

   end = length - 1;
   middle = length/2;

   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("is not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("is a Palindrome.\n");

   return 0;
}

Palindrome Number


/*A program to check whether given number is a palindrome or not */

#include<stdio.h>

int main()
{
   int n, reverse = 0, temp;

   printf("Enter a number to check whether it is a palindrome or not:\n");
   scanf("%d",&n);
   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("\n%d is a palindrome number.\n", n);
   else
      printf("\n%d is not a palindrome number.\n", n);

   return 0;
}