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
1 
----------------------------------------------------------------------------------------------------
/*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();
}