Showing posts with label C PROGRAM. Show all posts
Showing posts with label C PROGRAM. Show all posts

COMMAND LINE ARGUMENTS

/*  COMMAND LINE ARGUMENTS  */

#include<stdio.h>
void main(int argc,char *argv[])
 {
  FILE *f1;
  char c;
  if(argc>2)
  printf("Insufficient argument");
  f1=fopen(argv[1],"r");
  printf("The content of given input file is as
                                      follows\n");
  while((c=getc(f1))!=EOF)
     printf("%c",c);
  fclose(f1);
 }

OUTPUT:
-------
C:\Documents and Settings\sss>cd..
C:\Documents and Settings>cd..
C:\>cd turboc3
C:\TURBOC3>comline comline.c
  The content of given input file is as follows
  #include<stdio.h>
  void main(int argc,char *argv[])
    {
     FILE *f1;
     char c;
     if(argc>2)
     printf("Insufficient argument");
     f1=fopen(argv[1],"r");
     printf("The content of given input file is as  
                                         follows\n");
     while((c=getc(f1))!=EOF)
        printf("%c",c);
     fclose(f1);
    }



FILE COPYING

/                 /*  FILE COPYING  */

#include<stdio.h>
#include<stdlib.h>
void main()
  {
    FILE *fp1,*fp2;
    int i;
    clrscr();
    printf("\n\t\tINPUT:");
    printf("\n\t\t------");
    printf("\n\t\tCreated a text file name as sample.txt
       with the content like 'This is my first program.'");
    if((fp1=fopen("D:\sample.txt","r"))==NULL)
     {
       printf("Cannot open file\n");
       exit(1);
     }
    if((fp2=fopen("D:\sampledup.txt","w"))==NULL)
     {
       printf("cannot open file\n");
       exit(1);
     }
    while((i=getc(fp1))!=EOF)
       putc(i,fp2);
    fclose(fp1);
    fclose(fp2);
    printf("\n\t\tOUTPUT:");
    printf("\n\t\t-------");
    printf("\n\t\tThe text copied into file sampledup.txt
                                      as follows\n");
    printf("\n\t\t");
    fp2=fopen("D:\sampledup.txt","r");
     while((i=getc(fp2))!=EOF)
        putchar(i);
    fclose(fp2);
    getch();
  }

INPUT:
------
Created a text file name as sample.txt with the content like This is my first program.

OUTPUT:
-------
The text copied into file sampledup.txt as follows
This is my first program.

BINARY SEARCH

/*  BINARY SEARCH  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   int a[20],n,i,loc,mid,beg,end,flag=0,item;
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter the number of elements:  ");
   scanf("%d",&n);
   printf("\n\t\tEnter the elements:\n");
   for(i=0;i<n;i++)
    {
     scanf("%d",&a[i]);
    }
   printf("\n\t\tEnter the element to be search:  ");
   scanf("%d",&item);
   loc=0;
   beg=0;
   end=n-1;
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   while((beg<end)&&(item!=a[mid]))
    {
     mid=((beg+end)/2);
     if(item==a[mid])
       {
         printf("\n\t\tSearch is successful.");
         loc=mid;
         printf("\n\t\tPosition of the item is: 
                                      %d",loc+1);
         flag=flag+1;
       }
      else if(item<a[mid])
        end=mid-1;
     else
        beg=mid+1;
    }
   if(flag==0)
     {
       printf("\n\t\tSearch is not successful.");
     }
  getch();
}



INPUT:
------
Enter the number of elements:  5
Enter the elements:
354
234
22
575
45
Enter the element to be search:  22

OUTPUT:
-------
Search is successful.
Position of the item is:  3


INPUT:
------
Enter the number of elements:  5
Enter the elements:
23
42
54
65
24
Enter the element to be search:  30

OUTPUT:
-------
Search is not successful

BUBBLE SORT

/*  BUBBLE SORT  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   int a[20],n,i,j,temp;
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter the number of elements:  ");
   scanf("%d",&n);
   printf("\n\t\tEnter the elements:\n");
   for(i=0;i<n;i++)
    {
     scanf("%d",&a[i]);
    }
   for(i=0;i<n;i++)
    {
      for(j=0;j<n-i-1;j++)
       {
         if(a[j]>a[j+1])
          {
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
          }
       }
    }
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   printf("\n\t\tAfter sorting the elements are:");
   for(i=0;i<n;i++)
    {
      printf("\n\t\t\t\t%d",a[i]);
    }
   getch();
 }


INPUT:
------
Enter the number of elements:  5
Enter the elements:
70
56
23
67
89

OUTPUT:
-------
After sorting the elements are:
          23
          56
          67
          70
          89

DETERMINANT OF A MATRIX

/*  DETERMINANT OF A  MATRIX  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   float a[10][10],mul,det=1.0,b;
   int i,j,k,n;
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter the value for n:  ");
   scanf("%d",&n);
   printf("\n\t\tEnter the matrix\n");
   for(i=1;i<=n;i++)
    {
      for(j=1;j<=n;j++)
       {
        scanf("\t\t%f",&b);
        a[i][j]=b;
       }
      printf("\n\t\t");
    }
   for(i=1;i<=n;i++)
    {
     for(j=1;j<=n;j++)
      {
        if(i==j) continue;
        mul=a[j][i]/a[i][i];
        for(k=1;k<=n;k++)
         {
          a[j][k]=a[j][k]-mul*a[i][k];
         }
      }
    }
   for(i=1;i<=n;i++)
     {
      for(j=1;j<=n;j++)
       {
         if(i==j)
         det=det*a[i][i];
       }
     }
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   if(det==0)
     {
       printf("\n\t\tThe given matrix is Singular Matrix");
       exit(0);
     }
    else
     {
       printf("\n\t\tDeterminant of matrix is:   %f",det);
     }
   getch();
 }

INPUT:
------
Enter the value for n:  2
Enter the matrix:
   2  3
   4  5

OUTPUT:
-------
Determinant of matrix is:   -2.000000

MATRIX ADDITION, SUBTRACTION AND MULTIPLICATION

    /*  MATRIX ADDITION, SUBTRACTION AND MULTIPLICATION  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   int i,j,c,r,k;
   int a[20][20],b[20][20],ma[20][20],ms[20][20];
   int mm[20][20];
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter the value for row and column:  ");
   scanf("%d%d",&c,&r);
   printf("\n\t\tEnter the value for matrix A\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }
       printf("\n");
     }
   printf("\n\t\tEnter the value for matrix B\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
          scanf("%d",&b[i][j]);
        }
       printf("\n");
     }
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          ma[i][j]=a[i][j]+b[i][j];
          ms[i][j]=a[i][j]-b[i][j];
         }
     }
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           mm[i][j]=0;
           for(k=0;k<c;k++)
             {
               mm[i][j] +=a[i][k]*b[k][j];
             }
         }
     }
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   printf("\n\t\tThe addition matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t\t%d",ma[i][j]);
         }
       printf("\n");
     }
   printf("\n\t\tThe subtraction matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           printf("\t\t%d",ms[i][j]);
         }
       printf("\n");
     }
   printf("\n\t\tThe multiplication matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           printf("\t\t%d",mm[i][j]);
         }
       printf("\n");
     }
   getch();
 }


INPUT:
------
Enter the value for row and column:  2   2
Enter the value for matrix A
4   3
6   2
Enter the value for matrix B
8   1
0   5

OUTPUT:
-------
The addition matrix is:
          12   4
          6    7
The subtraction matrix is:
          -4   2
           6  -3
The multiplication matrix is:
           32  19
           48  16

MAXIMUM AND MINIMUM

/*  MAXIMUM AND MINIMUM  */

#include<stdio.h>
#include<conio.h>
int min,max;
void main()
  {
    int i,n,a[10];
    clrscr();
    printf("\n\t\tINPUT:");
    printf("\n\t\t------");
    printf("\n\t\tEnter the number of elements:  ");
    scanf("%d",&n);
    printf("\n\t\tEnter the elements:\n");
    for(i=0;i<n;i++)
     {
       scanf("%d",&a[i]);
     }
    min=max=a[0];
    maxmin(a,n);
    printf("\n\t\tOUTPUT:");
    printf("\n\t\t-------");
    printf("\n\t\tThe minimum value is:   %d",min);
    printf("\n\t\tThe maximum value is:   %d",max);
    getch();
  }
maxmin(int a[],int n)
  {
    n--;
    if(n==0)
       return;
    if(max<a[n])
       max=a[n];
    if(min>a[n])
       min=a[n];
    maxmin(a,n);
    return;
  }


INPUT:
------
Enter the number of elements:  5
Enter the elements:
54
334
87
34
90

OUTPUT:
-------
The minimum value is:   34
The maximum value is:   334




GCD OF TWO NUMBERS

/*  GCD OF TWO NUMBERS  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   int a,b,gcd;
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter two numbers:  ");
   scanf("%d%d",&a,&b);
   gcd=recgcd(a,b);
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   printf("\n\t\tThe GCD of %d and %d is = %d ",a,b,gcd);
   getch();
 }
int recgcd(int x, int y)
 {
   int r;
   if(y = = 0)
     {
       return(x);
     }
   else
     {
       r=x%y;
       return(recgcd(y,r));
     }
  }

  
INPUT:
------
Enter two numbers: 
           8 
           4

OUTPUT:
-------
The GCD of 8 and 4 is = 4




FINDING AND REPLACING SUBSTRINGS

/*  FINDING AND REPLACING SUBSTRINGS  */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char s1[50],s2[50],s3[50],a[20],r[50];
  int i,j,k,l1,l2,s=0,p,c=0;
  clrscr();
  printf("\n\t\tINPUT:");
  printf("\n\t\t------");
  printf("\n\t\tEnter the string:  ");
  gets(s1);
  printf("\n\t\tEnter the substring:  ");
  gets(s2);
  printf("\n\t\tEnter the replace string:  ");
  gets(s3);
  l1=strlen(s1);
  l2=strlen(s2);
  for(i=0;i<l1;i++)
    {
      k=0;
      if(s1[i]==s2[0])
       {
         for(j=i;j<i+l2;j++)
          {
            a[k]=s1[j];
            k++;
          }
         a[k]='\0';
         if(strcmp(a,s2)==0)
           {
             for(p=0;s3[p]!='\0';p++)
              {
                r[s++]=s3[p];
              }
             i=i+l2-1;
             c++;
           }
         else
            r[s++]=s1[i];
       }
      else
         r[s++]=s1[i];
     }
  r[s++]='\0';
  printf("\n\t\tOUTPUT:");
  printf("\n\t\t-------");
  if(c==0)
    {
      printf("\n\t\tThe Substring is not Found");
    }
  else
    {
      printf("\n\t\tThe Substring is Found");
      printf("\n\t\tReplaced string is :  %s",r);
    }
  getch();
}


INPUT:
------
Enter the string:  I am doing BCA
Enter the substring:  BCA
Enter the replace string:  Bachelor Degree

OUTPUT:
-------
The Substring is Found
Replaced string is :  I am doing Bachelor Degree


INPUT:
------
Enter the string:  I am doing BCA
Enter the substring:  cs
Enter the replace string:  bca

OUTPUT:
-------
The Substring is not Found



Related Posts with Thumbnails