Wednesday, October 12, 2011

Program code for calculate the LCM and HCF of any set of positive integers.(Using CPP Languge)

#include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int a,b,i,lcm=1,hcf=1,c,d;

    cout<<"Enter First numbers:>";
    cin>>c;
    cout<<"Enter Secound numbers:>";
    cin>>d;
    a=c;
    b=d;

    for(i=2;i>0;++i)
    {
        A: if(a%i==0)
        lcm*=i;
          if(b%i==0)
          {
            b=b/i;
            if(a%i!=0)
            lcm*=i;
          }

            if(a%i==0)
        a=a/i;
          if(a==1&&b==1)
            break;
          if(a%i==0||b%i==0)
            goto A;
    }

    for(i=2;i>0;++i)
    {
            B: if(c%i==0&&d%i==0)
        hcf*=i;
        if(c%i==0)
            c=c/i;
        if(d%i==0)
            d=d/i;
        if(c%i==1&&d%i==1)
        break;
        if(c%i==0||d%i==0)
        goto B;
    }
    cout<<"\nThe LCM is =:>"<<lcm;
    cout<<"\nThe HCF is =:>"<<hcf;

    getch();
}
         OUTPUT
Enter First numbers:>2
Enter Secound numbers:>8

The LCM is =:>8
The HCF is =:>2

Program code for diplay tringle shape input given by user(Using CPP language)


.Print This Pattern :
                                                 *
                                           *        *
                                        *      *      *
                                   *       *       *      *
                                       *        *      *  
                                            *       *
                                                *
#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,k,no;
    clrscr();
    cout<<"\n Enter the number==>>";
    cin>>no;
    for(i=0;i<no;i++)
    {
        for(k=40-(i*2);k>0;k--)
        {
            cout<<" ";
        }
        for(j=0;j<i;j++)
        {
            cout<<"  * ";
        }
        cout<<"\n\n";
    }
    for(i=no;i>0;i--)
    {
        for(k=40-(i*2);k>0;k--)
        {
            cout<<" ";
        }
        for(j=0;j<i;j++)
        {
            cout<<"  * ";
        }
        cout<<"\n\n";
    }
    getch();
}

Program code for multiplication of two metrices(Using CPP)

#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,mat1[3][3],mat2[3][3],mul[3][3],k;
    clrscr();
    cout<<"\n__________________________________";
    cout<<"\n Enter the value of First matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" Enter the value of matrix1["<<i<<"]["<<j<<"] =>";
            cin>>mat1[i][j];
        }
    }
    cout<<"\n The matrix is\n";
    cout<<"_________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" "<<mat1[i][j];
        }
        cout<<"\n";
    }
    cout<<"\n__________________________________";
    cout<<"\n Enter the value of Secound matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" Enter the value of matrix2["<<i<<"]["<<j<<"]=>";
            cin>>mat2[i][j];
        }
    }
    cout<<"\n The matrix is\n";
    cout<<"_________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            cout<<" "<<mat2[i][j];
        }
        cout<<"\n";
    }
    cout<<"\n__________________________________";
    cout<<"\n Multiplication of the both matrix\n";
    cout<<"__________________________________\n\n";
    for(i=1;i<4;i++)
    {
        for(j=1;j<4;j++)
        {
            mul[i][j]=0;
            for(k=1;k<4;k++)
            {
                mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
            }
            cout<<" "<<mul[i][j];
        }
        cout<<"\n";
    }
    getch();
}
                     OUTPUT
__________________________________
 Enter the value of First matrix
__________________________________

 Enter the value of matrix1[1][1] =>1
 Enter the value of matrix1[1][2] =>2
 Enter the value of matrix1[1][3] =>3
 Enter the value of matrix1[2][1] =>4
 Enter the value of matrix1[2][2] =>5
 Enter the value of matrix1[2][3] =>4
 Enter the value of matrix1[3][1] =>3
 Enter the value of matrix1[3][2] =>2
 Enter the value of matrix1[3][3] =>1

 The matrix is
_________________

 1 2 3
 4 5 4
 3 2 1

__________________________________
 Enter the value of Secound matrix
__________________________________

 Enter the value of matrix2[1][1] =>4
 Enter the value of matrix2[1][2] =>5
 Enter the value of matrix2[1][3] =>3
 Enter the value of matrix2[2][1] =>2
 Enter the value of matrix2[2][2] =>5
 Enter the value of matrix2[2][3] =>4
 Enter the value of matrix2[3][1] =>2
 Enter the value of matrix2[3][2] =>3
 Enter the value of matrix2[3][3] =>2

 The matrix is
_________________

 4 5 3
 2 5 4
 2 3 2

__________________________________
 Multiplication of the both matrix
__________________________________

 14 24 17
 32 54 38
 18 28 19