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



SUBSTRING DETECTION, COUNT AND REMOVAL

/*  SUBSTRING DETECTION, COUNT AND REMOVAL  */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
   char s1[50],s2[50],a[50],s3[50],r[10];
   int i,j,k,c,l1,l2,s=0,p1[10],p=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);
   l1=strlen(s1);
   l2=strlen(s2);
   c=0;
   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)
          {
            p1[p]=i+1;
            p++;
            c++;
            i=i+l2-1;
          }
       }
     else
       r[s++]=s1[i];
     }
   r[s++]='\0';
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   for(p=0;p<c;p++)
   printf("\n\t\t'%s' occurs at position  %d in
                                '%s'",s2,p1[p],s1);
   if(c==0)
     {
       printf("\n\t\tSubstring '%s' is not found",s2);
     }
   else
     {
       printf("\n\t\tSubstring '%s' is found at %d times in
                                            '%s'",s2,c,s1);
       printf("\n\t\tAfter detection of substring:  %s",r);
     }
   getch();
  }



INPUT:
------
Enter the string:  programming
Enter the substring:  m

OUTPUT:
-------
'm' occurs at position  7 in 'programming'
'm' occurs at position  8 in 'programming'
Substring 'm' is found at 2 times in 'programming'
After detection of substring:  prograing



INPUT:
------
Enter the string:  programming
Enter the substring:  ds

OUTPUT:
-------
Substring 'ds' is not found

Related Posts with Thumbnails