Wednesday, 9 November 2011

Write a C program in C to carry out following operations on strings using library functions... 1) To concatenate a string s2 to string s1 2) To find the length of a given string 3) To compare two strings s1 nad s2 4) To copy a string s2 to another string s1


 # include<stdio.h>
 void main()
 {
  char a[20],b[20],c[20],d[20];
  int z1=0,z2=0;
  clrscr();

  printf("\nEnter First String : ");
  scanf("%s",&a);
  printf("\nEnter Second String: ");
  scanf("%s",&b);

  printf("\n*** Length *** \n");
  z1 = strlen(a);
  printf("\nLength of String1 = %d\n",z1);
  z2 = strlen(b);
  printf("\nLength of String2 = %d\n",z2);

  printf("\n*** Compare ***\n");
  if (z1>z2)
  {
    printf("\nString1 is greater than String2\n");
  }
  else if(z1<z2)
  {
    printf("\nString2 is greater than String1\n");
  }
  else if (z1==z2)
  {
   printf("\nBoth strings are equal....\n");
  }

  printf("\n*** Copy ***\n");
  printf("\nBefore Copy\n");
  printf("\nString1 = %s\n",a);
  printf("\nString2 = %s\n",b);
  printf("\nAfter Copy\n");
  z1 = strcpy(a,b);
  printf("\nString1 = %s\n",z1);
  printf("\nString2 = %s\n",b);

  printf("\n*** Concatenate ***\n");
  z1 = strcat(a,b);
  printf("\nThe concatinated value is %s\n",z1);

  getch();
 }

Write a program in C to compute addition/subraction of two matrices.Use functions to read,display and add/subtract the matrices.



  # include<stdio.h>
  # include<conio.h>
  void main()
  {
  void show(int c[2][2]);
  void insert(int l[2][2]);
  void add(int a[2][2],int b[2][2]);
  void sub(int a[2][2],int b[2][2]);
  int x[2][2],y[2][2],op;

  clrscr();
  printf("\n Enter First Matrix  : \n");
  insert(x);
  printf("\n Eneter Second Matrix: \n");
  insert(y);
  printf("\n First Matrix is     : \n");
  show(x);
  printf("\n");
  printf("\n Second Matrix is    : \n");
  show(y);
  printf(" \n\n");
  printf("\n 1) Addition \n");
  printf("\n 2) Subtraction \n");

  printf("\nEnter your option: \n\n");
  scanf("%d",&op);
  switch(op)
  {
   case 1 : add(x,y);
   break;
   case 2 : sub(x,y);
   break;
  }
   getch();
 }

 // insert function allow us to insert values of matrix
 void insert(int c[2][2])
 {
   int i,j;
   for(i=0; i<2; i++)
   {
    for(j=0; j<2; j++)
     {
       scanf("%d",&c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }    // end of insert function

  // show function allow us to display the element of matrix
  void show(int c[2][2])
  {
   int i,j;
   printf(" \n ");
   for(i=0; i<2; i++)
   {
    for(j=0; j<2; j++)
     {
       printf(" %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of show function

 // add function allow us to add two matrix
 void add(int a[2][2],int b[2][2])
 {
  int i,j,c[2][2];
  printf("\n\nAddition is \n\n");
  for(i=0; i<2; i++)
  {
   for(j=0; j<2; j++)
     {
      c[i][j] = a[i][j] + b[i][j]; // Addition of two matrix is perform here
      printf("\t %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of add function

 // sub function allow us to Subtract two matrix
 void sub(int a[2][2],int b[2][2])
 {
  int i,j,c[2][2];
  printf("\n\n Sunstraction is \n\n");
  for(i=0; i<2; i++)
  {
   for(j=0; j<2; j++)
     {
      c[i][j] = a[i][j] - b[i][j]; // Substraction of two matrix is perform here
      printf("\t %d ",c[i][j]);
     } // end of inner for loop
   }   // end of outer for loop
 }     // end of sub function

// Write a program in C to find out prime numbers between 1 to "N"


#include<stdio.h>
 void main()
 {
   int i,n=1,num;
   clrscr();
   printf("\nEnter your range to find out prime numbers: ");
   scanf("%d",&num);
   printf("\nPrime numbers between %d are: \n\n",num);
   while(n<num)   // Outer while loop
   {
     i=2;
     while(i<num)  // Inner while loop
      {
 if(n%i==0)
  {
     break;
  }           // end of if statement
 else
  {
     i++;
  }           // end of else statement
      }           // End of inner while loop
    if(i==n)
      {
printf(" %d ",i);
      }
    n++;
  }          // end of outer while loop
    getch();
 }

 Output:-
 Enter your range to find out prime numbers: 100

Prime numbers between 100 are:

2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73

79  83  89  97

/* Write a program in C to read an integer and display each of digit of the integer in English.*/


void main()
 {
   long int num=0,n=0,n1=0,rev=0,rev1=0;
   clrscr();

   printf("\n\nEnter any number: ");
   scanf("%ld",&num);
   printf("\n\n");
 
  while(num>0)
   {
     n   = (num%10);
     rev = (rev*10)+n;
     num = (num/10);
   }
 
 rev1 = rev;

   do
   {
     n1   = (rev1%10);
     rev1 = (rev1/10);

     switch(n1)
     {
       case 0 : printf(" ZERO ");
break;
       case 1 : printf(" ONE ");
break;
       case 2 : printf(" TWO ");
break;
       case 3 : printf(" THREE ");
break;
       case 4 : printf(" FOUR ");
break;
       case 5 : printf(" FIVE ");
break;
       case 6 : printf(" SIX ");
break;
     

       case 7 : printf(" SEVEN ");
break;
       case 8 : printf(" EIGHT ");
break;
       case 9 : printf(" NINE ");
break;
     } // end of switch statement
  }while(rev1>0);
  getch();
 }

 Output:-

 Enter any number: 1502

 ONE  FIVE  ZERO  TWO



Wednesday, 2 November 2011

Print Factorial of given no. by using recursion......


#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num;
clrscr();

printf("\n enter a number : \n ");
scanf("%d",& num);

printf("\n Factorial = %d ", fact(num));
getch();
}
int fact (int num)
{
if (num ==1)
return 1;
else
return ( num * fact(num-1));
}