Friday, July 22, 2011

Program to print numbers in Traingular pattern {Type 15}


1                                     1
1  2                             2 1
1  2  3                    3  2  1
1  2  3  4           4  3  2  1
1  2  3  4  5  5  4   3  2  1
1  2  3  4           4  3  2  1
1  2  3                    3  2  1
1  2                             2 1
1                                     1

==========================================================
/*Serial No.40     [swami11.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l=1,m=1,n;
clrscr();
printf("Enter the last number==>");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
for(k=1;k<=2*n-1-l;k++)
printf(" ");
for(j=i;j>=1;j--)
printf("%d",j);
l=l+2;
printf("\n");
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
printf("%d",j);
for(k=1;k<=m+1;k++)
printf(" ");
for(j=n-i;j>=1;j--)
printf("%d",j);
m=m+2;
printf("\n");
}
getch();
}

Program to print numbers in Traingular pattern {Type 14}


1  2  3  4  5  5  4  3  2  1
1  2  3  4           4  3  2  1
1  2  3                   3  2  1
1  2                            2  1
1                                    1
1                                    1
1  2                            2  1
1  2  3                   3  2  1
1  2  3  4           4  3  2  1
1  2  3  4  5  5  4  3  2  1
=======================================================

/*Serial No.41     [swami12.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l=0,m=1,n;
clrscr();
printf("Enter the last number==>");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1-i;j++)
printf("%d",j);
for(k=1;k<=l;k++)
printf(" ");
for(j=n+1-i;j>=1;j--)
printf("%d",j);
l=l+2;
printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
for(k=1;k<=2*n-1-m;k++)
printf(" ");
for(j=i;j>=1;j--)
printf("%d",j);
m=m+2;
printf("\n");
}
getch();
}

Program to print numbers in Traingular pattern {Type 13}


                1
           1   2  1
       1  2   3  2  1
    1  2  3  4  3  2  1
1  2  3  4  5  4  3  2  1
    1  2  3  4  3  2  1
       1   2  3  2  1
          1   2   1
               1 

==========================================================
/*Serial No.51     [swami23.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the last number for series==>");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("%d",k);
for(k=i-1;k>=1;k--)
printf("%d",k);
printf("\n");
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=i;j++)
printf(" ");
for(k=1;k<=n-i;k++)
printf("%d",k);
for(k=n-1-i;k>=1;k--)
printf("%d",k);
printf("\n");
}
getch();
}

Program to print numbers in Traingular pattern {Type 12}


1
2  2
3  3  3
4  4  4  4
5  5  5  5  5
=====================================================
/*Serial No.4     [swami36.cpp]*/

#include<stdio.h>
#include<conio.h>
int main()
{clrscr();
int n,i,j,k=1;
printf("Enter values of n=") ;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",k);
printf("\n");
k++;
}
getch();
}

Program to print Parkside's Traingle

PARKSIDE'S TRAINGLE
1
2  3
4  5  6
7  8  9  10
.....................
================================================ 
/*Serial No.3     [swami35.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int count,j,i,m,n;
clrscr();
printf("enter the no. of lines u want to print=");
scanf("%d",&m);
printf("enter the starting value=");
scanf("%d",&n);
count =n;
i=1;
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",count);
count++;
if(count>=32764)
break;
}
}
getch();
}

Program to print numbers in Traingular pattern {Type 11}


              1
           1    2
       1    2    3
    1     2    3    4
 1     2    3    4    5
    1    2    3     4
       1     2    3
           1    2
              1

===========================================================
/*Serial No.34     [swami154.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=(n-1);k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{printf(" %d",j);}
printf("\n");
}
for(i=(n-1);i>=1;i--)
{
for(k=(n-1);k>=i;k--)
{printf(" ");}
for(j=1;j<=i;j++)
{printf(" %d",j);}
printf("\n");
}
getch();
}

Program to print numbers in Traingular pattern {Type 10}


 1    2    3   4   5
   1   2   3    4
     1   2   3
       1    2
          1

============================================================
/*Serial No.33     [swami153.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(k=(n-1);k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{printf(" %d",j);}
printf("\n");
}
getch();
}

Program to print numbers in Traingular pattern {Type 1} {Version 2}


1
1  2
1  2  3
1  2  3  4
1  2  3  4  5

=============================================================
/*Serial No.32     [swami152.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{printf("%d",j);}
printf("\n");}
getch();
}

Program to print numbers in Traingular pattern {Type 9}


1  2  3  4  5
    1  2  3  4
        1  2  3
            1  2
                1

============================================================
/*Serial No.31     [swami151.cpp]*/

#include<stdio.h> 
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
printf("\n");
for(i=n;i>=1;i--)
{
for(k=(n-1);k>=i;k--)
{printf(" ");}
for(j=1;j<=i;j++)
{printf("%d",j);}
printf("\n");}
getch();
}

Program to print numbers in Traingular pattern {Type 8}


            1
         1    2
      1    2   3
   1    2   3   4
1    2    3   4   5

-------------------------------------------------------------------------------------------
/*Serial No.30     [swami150.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(k=(n-1);k>=i;k--)
{printf(" ");}
for(j=1;j<=i;j++)
{printf("%d",j);
}
printf("\n");}
getch();
}

Program to print stars in Traingular pattern {Type 7}


            *
         *   *
      *    *   *
   *    *   *   *
*    *    *   *   *

-------------------------------------------------------------------------------------
/*Serial No.29     [swami149.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(k=(n-1);k>=i;k--)
{printf(" ");}
for(j=1;j<=i;j++)
{printf("*");
}
printf("\n");}
getch();
}

Program to print numbers in Traingular pattern {Type 6}


1  2  3  4  5
1  2  3  4
1  2  3
1  2
----------------------------------------------------------------------------------------------------
/*Serial No.28     [swami148.cpp]*/ 

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no.:");
scanf("%d",&n);
printf("\n");
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{printf("%d",j);
}
printf("\n");}
getch();
}

Program to print stars in Traingular pattern {Type 5}


*  *  *  *  *
*  *  *  *
*  *  *
*  *
*
 -----------------------------------------------------------------------------
/*Serial No.27     [swami147.cpp]*/ 

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
printf("\n");
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{printf("*");
}
printf("\n");}
getch();
}

Program to print numbers in Traingular pattern {Type 4}


            1
         1   2
       1   2  3
    1   2   3   4
 1    2   3   4   5
----------------------------------------------------------------------------------------------------

/*Serial No.26     [swami146.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=(n-1);k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{printf(" %d",j);}
printf("\n");
}
getch();
}

Program to print stars in Traingular pattern {Type 3}

            *
         *    *
      *    *    *
    *    *   *   *
 *    *    *   *   *
------------------------------------------------------------------
/*Serial No.25     [swami145.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=(n-1);k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{printf(" *");}
printf("\n");
}
getch();
}

Program to print stars in Traingular pattern {Type 2}


*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
-----------------------------------------------------------------------
/*Serial No.23     [swami144.cpp]*/

/* this program prints traingle as big as u want.*/
#include<stdio.h>
#include<conio.h>
int i,j,n;
void main()
{
clrscr();
printf("Enter the no. of lines of traingle:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{printf("*\n");
if(j==n)
break;
for(j=1;j<=i;j++)
printf("*");
}
getch();
}

Program to print numbers in Traingular pattern {Type 1}


1
1  2
1  2  3
1  2  3  4
1  2  3  4  5

----------------------------------------------------
/*Serial No.22     [swami143.cpp]*/

/* this program prints numbers in traingular fashion.(up to which no. u want)*/
#include<stdio.h>
#include<conio.h>
int i,j,n;
void main()
{
clrscr();
printf("Enter the no. upto which u want to print numbers.:");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{printf("%d\n",i);
if(j==n)
break;
for(j=1;j<=i;j++)
printf("%d ",j);
}
getch();
}

Progrma for quick sort

/*Serial No.160     [swami141.cpp]*/

#include<stdio.h>
#include<conio.h>
#define MAX 10
    
int arr[MAX],num,n;
    
void quicksort(int,int);
void main()
{
int i,first,last;
clrscr();
printf("Enter the number of elements in the list: ");
scanf("%d",&n);
printf("Enter the elements: \n");
for(i=0;i<n;i++)
        {
        scanf("%d",&arr[i]);
        }
quicksort(0,n-1);// pass the first and last index
printf("The sorted elements: \n");
for(i=0;i<n;i++)
        {
         printf("%d..\n",arr[i]);
        }
getch();
}
    
void quicksort(int first,int last)
       {
        int temp,low,high,midval;
        low=first;//array index
        high=last;//array index
        midval=arr[(first+last)/2]; // mid element
    
        do
        {
            while(arr[low]<midval)
            {
                low++;
            }
            while(arr[high]>midval)
            {
                high--;
            }
            if(low<=high)
            {
                temp=arr[low];
                arr[low]=arr[high];
                arr[high]=temp;
                low++;
                high--;
            }
        }while(low<=high);
        if(first<high)
        {
            quicksort(first,high);
        }
        if(low<last)
        {
            quicksort(low,last);
        }
    }

Program to implement Heap Sort

/*Serial No.151     [swami133.cpp]*/

#include<stdio.h>
#include<conio.h>

void restoreHup(int*,int);
void restoreHdown(int*,int,int);

void main()
{
    int a[20],n,i,j,k;
    clrscr();
    printf("Enter the number of elements to sort : ");
    scanf("%d",&n);

    printf("Enter the elements :");
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        restoreHup(a,i);
    }
    j=n;
    for(i=1;i<=j;i++)
    {
        int temp;
        temp=a[1];
        a[1]=a[n];
        a[n]=temp;
        n--;
        restoreHdown(a,1,n);
    }

    n=j;
    printf("Here is it...");
    for(i=1;i<=n;i++)
        printf("%4d",a[i]);
    getch();
}

void restoreHup(int *a,int i)
{
    int v=a[i];
    while((i>1)&&(a[i/2]<v))
    {
        a[i]=a[i/2];
        i=i/2;
    }
    a[i]=v;
}

void restoreHdown(int *a,int i,int n)
{
    int v=a[i];
    int j=i*2;
    while(j<=n)
    {
        if((j<n)&&(a[j]<a[j+1]))
            j++;
        if(a[j]<a[j/2]) break;
        a[j/2]=a[j];
        j=j*2;
    }
    a[j/2]=v;
}

Thursday, July 21, 2011

Program to make employee payment record using structure

/*Serial No.114     [swami91.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
    {
    int bs,att;
    char name[10];
    };
struct employee emp[10];
void main()
{
int i;
float ded,da,hra,net,gross,sal;
clrscr();
for(i=1;i<4;i++)
    {
    printf("enter the name of %d employee: ",i);
    scanf("%s",emp[i].name);
    printf("enter the attendence of preset month = ");
    scanf("%d",&emp[i].att);
    printf("enter the basic salary =");
    scanf("%d",&emp[i].bs);
    }

printf("\n\ndata stored are :\n");
for(i=1;i<4;i++)
    {
    printf("Name of %d employee: %s\n",i,emp[i].name);
    sal=(emp[i].bs/30)*emp[i].att;
    da=sal*0.1;
    hra=sal*0.2;
    gross=sal+da+hra;
    ded=gross*0.1;
    net=gross-ded;
    printf("Gross salary =%f\n",gross);
    printf("Net salary =%f\n",net);
    }
getch();
}

Program to count occourence of each character in a given string

/*Serial No.112     [swami89.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],comp;    //here in place of str[50] if I write *str then
int n,i,j,count;        //program also works. [TO CHECK WHY ???]
clrscr();
printf("Enter the string==>");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
    {
    comp=str[i];
    if(comp!=' ')
      {
      count=0;
      for(j=0;j<n;j++)
        {
        if(comp==str[j])
          {
          count+=1;
          str[j]=' ';
          }
        }
      }
    else
      continue;
    printf("%c occurs %d times\n",comp,count);
    }
getch();
}

Program to print string in formatted output form


/*Serial No.111     [swami88.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i;
char name[10]="math",a[]="aaaaaaaaaaaaaaa",c='y';
clrscr();
puts(name);
for(i=1;i<5;i++)
    {
    printf("%10.*s\n",i,name);
    }
printf("\n-----------------\n");
for(i=0;i<5;i++)
    {
    printf("%*c\n",i,c);
    }
for(i=5;i>0;i--)
    {
    printf("%*c\n",i,c);
    }
getch();
}


OUTPUT :
math
            m
          ma
        mat
      math

-----------------
y
  y
    y
      y
         y
       y
      y
    y
  y

y

Program to display contents of a file

/*Serial No.110     [swami87.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char ch;
clrscr();
FILE *fp1;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("Contents of file are:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    printf("%c",ch);
    }
getch();
}

Program to copy two files into single another file

/*Serial No.109     [swami86.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i;
char ch,name[10];
clrscr();
FILE *fp1,*fp2,*fp3;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
fp3=fopen("swami3.dat","w");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("file is being copied.\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp1);
fclose(fp3);
fp2=fopen("swami2.dat","r");
if(fp2==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp2);
    exit;
    }
fp3=fopen("swami3.dat","a+");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("second file is being copied.\n");
while(1)
    {
    ch=fgetc(fp2);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp2);
fclose(fp3);
getch();
}

Program to sort an array of names in alphabetical and reverse order

/*Serial No.108     [swami85.cpp]*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
    {
    int i,j,k;
    char name[10][10],tname[10][10],temp[10];
    clrscr();
    printf("How many numbers you want to store:=");
    scanf("%d",&k);
    printf("Enter numbers for in an array:=");
       fflush(stdin);

    for(i=0;i<k;i++)
       {
       scanf("%s",name[i]);
       strcpy(tname[i],name[i]);
       }

    for(i=1;i<k;i++)
       {
       for(j=1;j<k;j++)
          {
          if(strcmpi(name[j],name[j-1])<0)
          {
          strcpy(temp,name[j-1]);
          strcpy(name[j-1],name[j]);
          strcpy(name[j],temp);
          }
          }
       }
    printf("The sorted names in alphabetical order [A to Z Format] are:\n");
    printf("\tOldlist\t\tNew list\n\n");
    for(i=0;i<k;i++)
        {
        printf("\t%s\t\t%s\n",tname[i],name[i]);
        }
    printf("\n\nThe sorted names in [Z to A Format] are:\n");
    for(i=k-1;i>=0;i--)
        {
        printf("\t%s\n",name[i]);
        }
    getch();
    }

Program to find LCM and HCF of two no using recursion {Version 3}

/*Serial No.106     [swami83.cpp]*/ .

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,old_rem,cur_rem,lcm,hcf;
int hccf(int, int);
clrscr();
printf("Enter the two no:");
scanf("%d%d",&a,&b);
if(a<b)
  {
  old_rem=b;
  cur_rem=a;
  }
  else
  {
  old_rem=a;
  cur_rem=b;
  }
hcf=hccf(old_rem,cur_rem);
lcm=(a*b)/hcf;
printf("LCM=%d\tHCF=%d",lcm,hcf);
getch();
}

hccf(int old_rem,int cur_rem)
    {
    int new_rem,hcf;
    new_rem=old_rem%cur_rem;
    old_rem=cur_rem;
    cur_rem=new_rem;
    if(new_rem==0)
      {
      return(old_rem);
      }
    else
      {
      hcf=hccf(old_rem,cur_rem);
      }
      return(hcf);
    }

Program to find LCM and HCF of two no {Version 2}

/*Serial No.105     [swami82.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,old_rem,cur_rem,new_rem,lcm,hcf;
clrscr();
printf("Enter the two no:");
scanf("%d%d",&a,&b);
if(a<b)
  {
  old_rem=b;
  cur_rem=a;
  }
  else
  {
  old_rem=a;
  cur_rem=b;
  }
do{
  new_rem=old_rem%cur_rem;
  old_rem=cur_rem;
  cur_rem=new_rem;
  }while(new_rem!=0);
hcf=old_rem;
lcm=(a*b)/hcf;
printf("LCM=%d\tHCF=%d",lcm,hcf);
getch();
}

Program to count no of characters, blank spaces, lines in a file

/*Serial No.104     [swami81.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i;
char ch,c=0,nl=1,name[20],bs=0;
clrscr();
FILE *fp1;
printf("Enter the name of file=%s\n",name);
gets(name);       //name is FILE NAME.
fp1=fopen(name,"r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("file data are as follows:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
       break;
    else
       c++;          // c is no. of characters.
       if(ch=='\n')
         {
         nl++;       // nl is no. of lines.
         }
       if(ch==' ')
         {
         bs++;       // bs is no. of blank spaces.
         }
    }
printf("No of characters in file=%d\n\n",c);
printf("No of blank spaces in file=%d\n\n",bs-1);
printf("No of characters in file includes blank spaces,tabs,new line characters,\nevery charater\n\n");
printf("No of lines in file=%d\n",nl);
fclose(fp1);
getch();
}

Program to count no of blank spaces and no of words in a string

/*Serial No.103     [swami80.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char *);
void main()
{
char s1[20];
int len;
clrscr();
printf("\nEnter the string:");
gets(s1);
len=xstrlen(s1);
printf("\nlength of the string =%d",len);
getch();
}
int xstrlen(char *s)
    {
    int l=0,m=0;
    while(*s)
      {
      if(*s==' ')
        {
        m++;
        }
      l++;
      s++;
      }
      printf("\nNo. of the blank spaces=%d",m);
      printf("\nNo. of the words=%d",m+1);
      printf("\n[--only if words are saparated by single space--]");
    return (l);
    }

Program to input & print more than one words in single line

/*Serial No.102     [swami79.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[50];
int len;
clrscr();
printf("\nEnter the string:");
scanf("%[^\n]",s1);
printf("Output string:%-50s",s1);
len=strlen(s1);
printf("\nlength of the string =%d",len);
getch();
}

Program to check for palindrome

/*Serial No.101     [swami77.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,len,flag;
char str[10],rev[10];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
len=strlen(str);
printf("\nLength of %s is ==>%d\n",str,len);
for(i=0;str[i]!='\0';i++)
    {
    if(str[i]!=str[len-1-i])
      {
      flag=3;
      break;
      }
    }
if(flag==3)
    printf("\nString is not polyndrom");
     else
    printf("\nString is polyndrom");
getch();
}

Wednesday, July 20, 2011

Program to find length of a string including blank spaces,tabs, and other special characters

/*Serial No.100     [swami75.cpp]*/ 

/*New line character will be taken as a string terminating character.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char *);
void main()
{
char s1[20];
int len;
clrscr();
printf("\nEnter the string:");
gets(s1);
len=xstrlen(s1);
printf("\nlength of the string =%d",len);
getch();
}
int xstrlen(char *s)
    {
    int l=0;
    while(*s)
      {
      l++;
      s++;
      }
    return (l);
    }

Program to find product of two no using MACRO

/*Serial No.99     [swami74.cpp]*/

#include<stdio.h>
#include<conio.h>
#define prod(t,r) t*r
void main()
{
int a,b,c;
printf("enter two no. for finding product\n");
scanf("%d%d",&a,&b);
c=prod(a,b);
printf("%d",c);
getch();
}

Program to accept a string and print the rightmost 'n' characters using function {Version 2}

/*Serial No.98     [swami73.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
char s1[20];
int n,l,i;

void pri();
void main()
{
clrscr();
printf("enter the string s1:");
gets(s1);
printf("enter the no of characters to extract=");
scanf("%d",&n);
printf("\n\noutput:\n");
pri();
getch();
}
void pri()
    {
    l=strlen(s1);
    i=l-n-1;
    while(s1[i]!='\0')
        {
        i++;
        printf("%c",s1[i]);
        }
    }

Program to accept a string and print the rightmost 'n' characters using function {Version 1}

/*Serial No.97     [swami72.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
char *rightsub(char *,int n);
void main()
{
char s1[20],s2[20],ch,*s;
int n;
clrscr();
printf("enter the string s1:");
gets(s1);
printf("enter the no of characters to extract=");
scanf("%d",&n);
s=rightsub(s1,n);
printf("\nright sub string: %s",s);
free(s);
getch();
}
char *rightsub(char *str,int n)
    {
    char *t=(char *)malloc(n+1);
    int l=strlen(str);
    char *s=str+(l-n);
    int i=0;
    while(i<n)
        {
        t[i]=*s;
        s++;
        i++;
        }
    t[i]='\0';
    return t;
    }

Program to check palindrome using recursive function

/*Serial No.96     [swami71.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void reverse(char num[],int a,int b)
    {
    if(a<b)
       {
       if(num[a++]==num[b--])
       reverse(num,a,b);
       else
         {
         printf("not a palindrome.\n");
         getch();
         exit;
         }
       }
    else
      {
      printf("palindrome");
      getch();
      exit(0);
      }
      }
void main()
{
int len;
char num[6];
printf("\n enter the no.");
scanf("%s",num);
len=strlen(num);
len--;
reverse(num,0,len);
}

Program to sort an array in ascending and descending order

/*Serial No.95     [swami70.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
    {
    int a[5],i,j,k,temp=0;
    clrscr();
    printf("How many numbers you want to store:=");
    scanf("%d",&k);
    printf("Enter numbers for in an array:=");
    for(i=0;i<k;i++)
       {
       scanf("%d",&a[i]);
       }
    for(i=1;i<k;i++)
       {
       for(j=1;j<k;j++)
          {
          if(a[j]>a[j-1])  //NOTE: we can simply replace '<' by '>' for ascending order.
         {
         temp=a[j-1];
         a[j-1]=a[j];
         a[j]=temp;
         }
          }
       }
    printf("The sorted array in descending order is:=\n");
    for(i=0;i<k;i++)
        {
        printf("\t%d",a[i]);
        }
    printf("\n\nThe sorted array in ascending order is:=\n");
    for(i=k-1;i>=0;i--)
        {
        printf("\t%d",a[i]);
        }
    getch();
    }