Saturday, July 31, 2010

SIMPLE C PROGRAMS PART-2




 

/*USING forLOOP(PRINT THE NUMBERS 1 TO 100)*/
main()
{
   int i;
   for(i=1;i<=100;i++)
      printf("%d\t",i);
 }



/*PRINT THE NUMBERS(1 T0 100)EVEN OR ODD*/
main()
{
    int i;
    for(i=1;i<=100;i++)
    {
      if(i%2==0)
       printf("\n%d IS EVEN",i);
      else
       printf("\t%d IS ODD",i);
     }
  }

   

/*PRINT THE YEAR 1000-2000 LEAP YEAR OR NOT*/
main()
{
   int year;
   for(year=1000;year<=1020;year++)
   {
      if(year%4==0)
       printf("\n%d IS LEAP YEAR",year);
      else
       printf("\t%d IS NOT LEAP YEAR",year);
    }
 }





/*PRINT THE NUMBERS BETWEEN TWO LIMITS*/
main()
{
     int a,b;
     for(scanf("%d%d",&a,&b);a<=b;a++)
       printf("%d\n",a);
 }
      

/*PRINT THE NUMBERS BETWEEN THE LIMITS IS EVEN OR ODD*/
main()
{
     int a,b;
     for(scanf("%d%d",&a,&b);a<=b;a++)
     {
       if(a%2==0)
         printf("%d IS EVEN\n",a);
       else
         printf("%d IS ODD\t",a);
      }
}
           [aug050233@linsoft aug050233]$ cat p106.c

/*PRINT THE YEARS BETWEEN THE TWO LIMITS IS LEAP YEAR OR NOT*/
main()
{
     int b,c;
     for(scanf("%d%d",&b,&c);b<=c;b++)
     {
        if(b%4==0)
         printf("%d IS LEAP YEAR\n",b);
        else
         printf("%d IS NOT LEAP YEAR\t",b);
      }
 }



/*READ AND DISPLAY THE NUMBER UNTIL -1000*/
main()
{
     int b;
     for(scanf("%d",&b);b!=-1000;scanf("%d",&b))
       printf("%d",b);
 }



/*CLASSIFY THE NUMBER IS POSITIVE,NEGATIVE OR EQUAL UNTIL -1000*/
main()
{
     int b;
     for(scanf("%d",&b);b!=-1000;scanf("%d",&b))
     {
        if(b==0)
         printf("%d IS ZERO",b);
        else
          if(b>0)
            printf("%d IS POSITIVE",b);
          else
            printf("%d IS NEGATIVE",b);
       }
 }
               

/*COUNT THE NUMBER POSITIVE,NEGATIVE OR ZERO UNTIL -1000*/
main()
{
     int b,i=0,j=0,k=0;
     for(scanf("%d",&b);b!=-1000;scanf("%d",&b))
     {
       if(b==0)
          i++;
       else
         if(b>0)
           j++;
         else
           k++;
      }
      printf("NUMBER OF ZEROS=%d\n",i);
      printf("NUMBER OF POSITIVE NUMMBERS=%d\n",j);
      printf("NUMBER OF NEGATIVE NUMBERS=%d\n",k);
  }

     

/*SUM OF THE POSITIVE,NEGATIVE NUMBERS UNTIL -1000*/
main()
{
      int b,s1=0,s2=0;
      for(scanf("%d",&b);b!=-1000;scanf("%d",&b))
      {
         if(b>0)
           s1=s1+b;
         else
           s2=s2+b;
       }
      printf("SUM OF POSITIVE NUMBERS=%d\n",s1);
      printf("SUM OF NEGATIVE NUMBERS=%d\t",s2);
  }



/*MEAN OF POSITIVE,NEGATIVE NUMBERS UNTIL -1000*/
main()
{
     float m1,m2;
     int b,s1=0,s2=0,i=0,j=0;
     for(scanf("%d",&b); b!=-1000;scanf("%d",&b))
     {
        if(b>0)
        {
          s1=s1+b;
          i++;
         }
        else
         {
           s2=s2+b;
           j++;
          }
       }
      m1=(float)s1/i;
      m2=(float)s2/j;
      printf("MEAN OF POSITIVE NUMBERS=%f\n",m1);
      printf("MEAN OF NEGATIVE NUMBERS=%f\n",m2);
}
           

/*READ AND DISPLAY CHARACTER UNTIL '$' */
main()
{
     char ch;
     for(scanf("%c",&ch);ch!='$';scanf("%c",&ch))
       printf("%c",ch);
 }


    

/*CLASSIFY THE CHARACTERS UPPER,LOWER,DIGIT OR SPECIAL CHARACTER*/
main()
{
     char ch;
     for(scanf("%c",&ch);ch!='$';scanf("%c",&ch))
     {
        if(ch>='A'&&ch<='Z')
          printf("%c IS UPPER CASE\n",ch);
        else
          if(ch>='a'&&ch<='z')
            printf("%c IS LOWER CASE\n",ch);
          else
            if(ch>='0'&&ch<='9')
              printf("%c IS DIGIT\n",ch);
            else
              printf("%c IS SPECIAL CHARACTER\n",ch);
        }
}



 

/*COUNT THE NUMBERS OF UPPER,LOWER,DIGIT OR SPECIAL CHARACTER*/
main()
{
     char ch;
     int i=0,j=0,k=0,t=0;
     for(scanf("%c",&ch);ch!='$';scanf("%c",&ch))
     {
       if(ch>='A'&&ch<='Z')
         i++;
       else
         if(ch>='a'&&ch<='z')
           j++;
         else
           if(ch>='0'&&ch<='9')
             k++;
           else
             t++;
       }
      printf("NUMBER OF UPPER CASE CHARACTER=%d\n",i);
      printf("NUMBER OF LOWER CASE CHARACTER=%d\n",j);
      printf("NUMBER OF DIGITS=%d\n",k);
      printf("NUMBER OF SPECIAL CHARACTERS=%d\n",t);

 }


          

/*READ AND DISPLAY 10 NUMBERS*/
main()
{
   int i,b;
   for(i=1;i<=10;i++)
   {
     scanf("%d",&b);
     printf("%d\t",b);
    }
}


      [aug050233@linsoft aug050233]$ cat p116.c

/*MEAN OF 10 NUMBERS*/
main()
{
    float m1;
    int i,b,s1=0;
    for(i=0;i<10;i++)
    {
      scanf("%d",&b);
      s1=s1+b;
     }
    m1=(float) s1/i;
    printf("MEAN OF 10 NUMBERS=%f",m1);
}




/*BIGGEST OF 10 NUMBERS*/
main()
{
     int i,b,c,big=0;
     for(i=1;i<=10;i++)
     {
       scanf("%d",&b);
       c=(b>big)?big=b:big;
      }
     printf("BIGGEST OF 10 NUMBERS=%d\n",c);

}



  

/*READ AND DISPLAY 10 NUMBERS USING ARRAY*/
main()
{
  int i,a[50],j;
  for(i=0;i<10;i++)
   scanf("%d",&a[i]);
  for(j=0;j<10;j++)
   printf("%d\n",a[j]);
 }

  [aug050233@linsoft aug050233]$ cat p119.c

/*MEAN OF 10 NUMBERS USING ARRAY*/
main()
{
     float m;
     int i,a[50],j,s=0;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
     for(j=0;j<10;j++)
       s=s+a[j];
     m=(float)s/j;
     printf("MEAN OF 10 NUMBERS=%f",m);
 }



/*BIGGEST OF 10 NUMBERS*/
main()
{
     int i,a[50],c,j;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
     c=0;
     for(j=0;j<10;j++)
       c=(a[j]>c)? a[j]:c;
     printf("BIGGEST OF 10 NUMBERS=%d",c);
 }



/*POSITION OF BIGGEST NUMBER*/
main()
{
     int i,a[50],j,c;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
      c=0;
      for(j=0;j<10;j++)
      c=(a[j]>a[c])?j:c;
      printf("POSITION OF BIGGEST NUMBER=%d",c);
 }


/*POSITION OF BIGGEST AS WELL AS SMALLEST OF 10 NUMBERS*/
main()
{
     int i,a[50],c,d,j;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
     c=0;
     d=0;
     for(j=0;j<10;j++)
     {
       c=(a[j]>a[c])?j:c;
       d=(a[j]
      }
     printf("POSITION OF BIGGEST NUMBER=%d\n",c);
     printf("POSITION OF SMALLEST NUMBER=%d\n",d);
 }




/*INTERCHANGE POSITION OF BIGGEST AS WELL AS SMALLEST NUMBER*/
main()
{
     int i,a[50],j,c,d,temp;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
     c=0,d=0;
     for(j=0;j<10;j++)
     {
       c=(a[j]>a[c])?j:c;
       d=(a[j]
      }
      temp=a[c];
      a[c]=a[d];
      a[d]=temp;
      for(i=0;i<10;i++)
       printf("%d",a[i]);
 }



   

/*SECOND BIGGEST NUMBER*/
main()
{
     int i,a[50],j,c,d;
     for(i=0;i<10;i++)
     scanf("%d",&a[i]);
     c=0,d=0;
      for(j=0;j<10;j++)

        c=(a[j]>c)?a[j]:c;
      for(j=0;j<10;j++)
        d=(a[j]!=c&&a[j]>d)?a[j]:d;

      printf("SECOND BIGGEST NUMBER=%d\n",d);
 }






/*INTERCHANGE THE POSITION OF SECOND BIGGEST AND SECOND SMALLEST NUMBER*/
main()
{
     int i,a[50],c,d,j,e,f,temp;
     for(i=0;i<10;i++)
      scanf("%d",&a[i]);
     c=0,d=0;
     for(j=0;j<10;j++)
     {
       c=(a[j]>a[c])?j:c;
       d=(a[j]
      }
     e=d;
     f=c;
     for(j=0;j<10;j++)
     {
       e=(j!=c&&a[j]>a[e])?j:e;
       f=(j!=d&&a[j]
      }
     temp=a[e];
     a[e]=a[f];
     a[f]=temp;
     for(i=0;i<10;i++)
      printf("%d\n",a[i]);
 }



/*READ AND DISPLAY 3*3 MATRIX*/
main()
{
     int i,j,a[50][50];
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
        scanf("%d",&a[i][j]);
      }
      for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
         printf("%d",a[i][j]);
         printf("\n");
       }
 }

/*TRANSPOSE THE 3*3 MATRIX*/
main()
{
    int i,j,a[50][50];
    for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
       scanf("%d",&a[i][j]);
     }
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
        printf("%d",a[j][i]);
        printf("\n");
      }
   }
    c

/* SUM OF 3*3 MATRIX*/
main()
{
    int i,j,a[50][50],b[50][50],c[50][50];
    for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
       scanf("%d",&a[i][j]);
     }
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
        scanf("%d",&b[i][j]);
      }
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
         c[i][j]=a[i][j]+b[i][j];
      }
      for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
        printf("%d",c[i][j]);
        printf("\n");
       }
  }
  

/*PRODUCT  OF 3*3 MATRIX*/
main()
{
     int i,j,a[50][50],b[50][50],c[50][50],k;
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
        scanf("%d",&a[i][j]);
      }
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
        scanf("%d",&b[i][j]);
      }
     c[i][j]=0;
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
       {
         for(k=0;k<3;k++)
          c[i][j]=c[i][j]+a[i][k]*b[k][j];
        }

      }

      for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
         printf("%d",c[i][j]);
         printf("\n");

       }
   }




/*READ AND DISPLAY A WORD*/
main()
{
    char  a[50];
    int i;
    for(i=0,scanf("%c",&a[i]);a[i]!=' ';i++,scanf("%c",&a[i]));
     a[i]='\0';
     for(i=0;a[i]!='\0';i++)
      printf("%c",a[i]);
  }



/*READ AND DISPLAY A SENTENCE UNTIL '.'*/
main()
{
     char a[50];
     int i;
     for(i=0,scanf("%c",&a[i]);a[i]!='.';i++,scanf("%c",&a[i]));
     a[i]='\0';
     for(i=0;a[i]!='\0';i++)
      printf("%c",a[i]);
 }   

/*READ AND DISPLAY A LINE*/
main()
{
    char a[50];
    int i;
    for(i=0,scanf("%c",&a[i]);a[i]!='\n';i++,scanf("%c",&a[i]));
     a[i]='\0';
     for(i=0;a[i]!='\0';i++)
      printf("%c",a[i]);
  }


/* READ AND DISPLAY THE TEXT UNTIL '$'*/
main()
{
    char a[50];
    int i;
    for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
     a[i]='\0';
     for(i=0;a[i]!='\0';i++)
      printf("%c",a[i]);
 }
     

/*COUNT THE NUMBER OF UPPER,LOWER,DIGIT OR SPECIAL CHARACTER IN THE SENTENCE*/
main()
{
     char a[50];
     int i,j=0,k=0,l=0,t=0;
     for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
      a[i]='\0';
      for(i=0;a[i]!='\0';i++)
      {
        if(a[i]>='A'&&a[i]<='Z')
          j++;
        else
          if(a[i]>='a'&&a[i]<='z')
            k++;
          else
            if(a[i]>='0'&&a[i]<='9')
              l++;
            else
              t++;
       }
       printf("NUMBER OF UPPER CASE CHARACTER=%d\n",j);
       printf("NUMBER OF LOWER CASE CHARACTER=%d\n",k);
       printf("NUMBER OF DIGITS=%d\n",l);
       printf("NUMBER OF SPECIAL CHRACTER=%d\n",t);
  }
        

/*COUNT THE NUMBER OF WORDS,LINES AND CHRACTER*/
main()
{
     char a[50];
     int i,w=0,l=0,c=0;
     for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
      a[i]='\0';
      for(i=0;a[i]!='\0';i++)
      {
         if((a[i]!=' '&&a[i+1]==' ')||(a[i]!='\n'&&a[i+1]=='\n')||
                   (a[i]!='\t'&&a[i+1]=='\t'))

             w++;
         if(a[i]=='\n')
            l++;
          c++;
        }
       printf("NUMBER OF WORDS=%d\n",w);
       printf("NUMBER OF LINES=%d\n",l);
       printf("NUMBER OF CHRACTERS=%d\n",c);
  }

         

/*READ THE ARRAY AND COPY TO ANOTHER ARRAY*/
main()
{
     char a[50],b[50];
     int i,n;
     for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
      a[i]='\0';
      scanf("%d",&n);
      for(i=0;i
       b[i]=a[i];
      b[i]='\0';
      for(i=0;b[i]!='\0';i++)
       putchar(b[i]);
  }


/*COPY THE FIRST n CHRACTER TO ANOTHER ARRAY*/
main()
{
     char a[50],b[50];
     int i,n;
     for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
      a[i]='\0';
      scanf("%d",&n);
      for(i=0;i
       b[i]=a[i];
      b[i]='\0';
      for(i=0;b[i]!='\0';i++)
       putchar(b[i]);
  }

   

/*COPY THE nth CHRACTER FROM mth POSITION */
main()
{
    char a[50],b[50];
    int i=0,m,n,j;
    for(i=0,a[i]=getchar();a[i]!='$';i++,a[i]=getchar());
    a[i]='\0';
    scanf("%d%d",&m,&n);
    for(i=m,j=0;a[j]!='\0'&&j
     b[j]=a[i];
    b[j]='\0';
    for(i=0;b[i]!='\0';i++)
     putchar(b[i]);
 }


/*COPY THE LAST n CHRACTER*/
main()
{
     char a[50],b[50];
     int i,n,j;
     for(i=0;(a[i]=getchar())!='$';i++);
      a[i]='\0';
      scanf("%d",&n);
     for(j=0,i=i-n;a[i]!='\0';i++,j++)
      b[j]=a[i];
     b[j]='\0';
     for(i=0;b[i]!='\0';i++)
      printf("%c",b[i]);
  }

/*UPPER TO LOWER*/
main()
{
     char a[50],b[50];
     int i;
     for(i=0;(a[i]=getchar())!='$';i++);
      a[i]='\0';
     for(i=0;a[i]!='\0';i++)
     {
       b[i]=(a[i]>='A'&&a[i]<='Z')?a[i]+32:a[i];
      }
     b[i]='\0';
     for(i=0;b[i]!='\0';i++)
      putchar(b[i]);
  }

/*COMMA TO SEMICOLON*/
main()
{
     char a[50],b[50];
     int i;
     for(i=0;(a[i]=getchar())!='$';i++);
     a[i]='\0';
     for(i=0;a[i]!='\0';i++)
     {
       b[i]=(a[i]==',')?';':a[i];
      }
     b[i]='\0';
     for(i=0;b[i]!='\0';i++)
      putchar(b[i]);
 }

[aug050233@linsoft aug050233]$ cat p142.c

/*DELETE COMMA*/
main()
{
    char a[50],b[50];
    int i,j;
    for(i=0;(a[i]=getchar())!='$';i++);
     a[i]='\0';
    for(i=0,j=0;a[i]!='\0';)
    {
       if(a[i]==',')
         i++;
       else
        {
          b[j]=a[i];
          j++;
          i++;
         }
      }
     b[j]='\0';
     for(i=0;b[i]!='\0';i++)
      putchar(b[i]);
  }



/*REVERSE THE TEXT*/
main()
{
    char a[50],b[50];
    int i,j;
    for(i=0;(a[i]=getchar())!='$';i++);
     a[i]='\0';
    for(i--,j=0;i>=0;)
    {
      b[j]=a[i];
      j++;
      i--;
     }
    b[j]='\0';
    for(i=0;b[i]!='\0';i++)
     putchar(b[i]);
  }
      c

/*MERGE THE ARRAY*/
main()
{
     char a[50],b[50],c[50];
     int i,j;
     for(i=0;(a[i]=getchar())!='$';i++);
      a[i]='\0';
     for(j=0;(b[j]=getchar())!='$';j++);
      b[j]='\0';
     for(i=0,j=0;a[i];)
     {
       c[j]=a[i];
       j++;
       i++;
      }
     for(i=0;b[i];)
     {
       c[j]=b[i];
       j++;
       i++;
      }
     c[j]='\0';
     for(i=0;c[i]!='\0';i++)
      putchar(c[i]);
  }


/*CONVERT FORTRAN ASSIGNMENT TO PASCAL ASSIGNMENT*/
main()
{
  char a[50],b[50];
  int i,j;
  for(i=0;(a[i]=getchar())!='$';i++);
   a[i]='\0';
  for(i=0,j=0;a[i];i++)
  {
    if(a[i]=='=')
    {
      b[j]=':';
      j++;
      b[j++]='=';

     }
    else
      if(a[i]=='\n')
      {
        b[j]=';';
        j++;
        b[j++]='\n';
       }
      else
      {
        b[j]=a[i];
        j++;
      }
   }
   b[j]='\0';
   for(i=0;b[i]!='\0';i++)
    putchar(b[i]);
 }


        /*CONVERT PASCAL TO FORTRAN ASSIGNMENT*/
main()
{
    char a[50],b[50];
    int i,j;
    for(i=0;(a[i]=getchar())!='$';i++);
     a[i]='\0';
    for(i=0,j=0;a[i]!='\0';i++)
    {
      if(a[i]==':'|| a[i]=='=')
        b[j++]='=';
      else
        if(a[i]==';')
          b[j++]='\n';
        else
          b[j++]=a[i];
     }
     b[j]='\0';
     for(i=0;b[i]!='\0';i++)
      putchar(b[i]);
  }
   

/*COUNT THE NUMBER OF LINES IN TEXT*/
main()
{
   char a[50];
   int i,k;
   for(i=0;(a[i]=getchar())!='$';i++);
    a[i]='\0';
   for(i=0,k=0;a[i];i++)
   {
     if(a[i]=='\n')
       k++;
    }
   printf("NUMBER OF LINES IN TEXT=%d",k);
 }






/*PRINT THE STARTING POSITION OF EACH LINE*/
main()
{
    char a[50];
    int i;
    for(i=0;(a[i]=getchar())!='$';i++);
     a[i]='\0';
    for(i=0;a[i];i++)
    {
      if(a[i]=='\n')
       printf("%d",i+1);
     }
  }



/*STORE THE STARTING POSITION OF A LINE IN AN ARRAY*/
main()
{
     char a[50],b[50];
     int i,j;
     for(i=0;(a[i]=getchar())!='$';i++);
      a[i]='\0';
     j=0,b[j]=0;
     for(i=0,j++;a[i];i++)
     {
       if(a[i]=='\n')
       {
         b[j]=i+1;
         j++;
        }
      }
     b[j]=-1;
     for(i=0;b[i]!=-1;i++)
      printf(" %d ",b[i]);
  }

/*DISPLAY THE nth LINE*/
main()
{
     char a[50],b[50];
     int i,j,n;
     for(i=0;(a[i]=getchar())!='$';i++);
      a[i]='\0';
     j=0,b[j]=0;
     for(i=0,j++;a[i]!='\0';i++)
     {
       if(a[i]=='\n')
       {
         b[j]=i+1;
         j++;
        }
      }
     b[j]=-1;
     scanf("%d",&n);
     for(i=b[n-1];a[i]!='\n';i++)
       printf("%c",a[i]);
 }



No comments:

Post a Comment