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
 

A number is said to be lucky, if it satisfies the following conditions: Condition1: Number itself must be a prime Number. Condition2: sum of all digits of given number must be a prime number. Write a program to print all lucky numbers between 1 and 100. Ans : 1 2 3 5 7 11 23 29 41 43 47 61 67 83 89

#include<stdio.h>
#include<conio.h>
main ()
{
  int i, j, prime = 0,sum=0;
  clrscr();
  printf("\n_____________________________\n");
  printf("\nDisplay all Prime numbers\n");
  printf("_____________________________\n");
  for (i = 1; i < 100; i++)
    {
      prime = 0;
      for (j = 2; j < (i / 2) + 1; j++)
    {
      if (i % j == 0)
        {
          prime = 1;
          break;
        }
    }
      if (prime == 0)
    {
      printf ("%d is prime number\n", i);
      sum=sum+i;
    }

    }
    printf("\n\n");
    printf("the sum of prime no is=> %d",sum);
    getch();
}

    OUTPUT
_____________________________

Display all Prime numbers
_____________________________
1 is prime number
2 is prime number
3 is prime number
5 is prime number
7 is prime number
11 is prime number
13 is prime number
17 is prime number
19 is prime number
23 is prime number
29 is prime number
31 is prime number
37 is prime number
41 is prime number
43 is prime number
47 is prime number
53 is prime number
59 is prime number
61 is prime number
67 is prime number
71 is prime number
73 is prime number
79 is prime number
83 is prime number
89 is prime number
97 is prime number


the sum of prime no is=> 1061

2 Write a program for generate a Stack of n number of values; perform Push, Pop, Display, Peep, Change operation for selected position. Using C languge,....


#include<stdio.h>
#include<conio.h>
#define SIZE 5
void PUSH();
void POP();
void PEEP();
void DISPLAY();
void CHANGE();
int a[SIZE],item,top=0;
void main()
{
    int ch;
    clrscr();
    do
    {
       printf("\n1.push");
       printf("\n2.pop");
       printf("\n3.display");
       printf("\n4.peep");
       printf("\n5.change");
       printf("\n6.exit\n\n");
       printf("enter the choice");
       scanf("%d",&ch);
       switch(ch)
       {
           case 1:
              PUSH();
              break;
           case 2:
              POP();
              break;
           case 3:
              DISPLAY();
              break;
           case 4:
              PEEP();
              break;
           case 5:
            CHANGE();
              break;
           case 6:
              exit(0) ;
           default:
              printf("not allowed");
         }
      } while(ch<=6);
 getch();
}

void PUSH()
    {
         if(top>=SIZE)
         {
            printf("\nstack is full");
         }
        else
         {
            top++;
            printf("\nEnter the item");
            scanf("%d",&item);
            a[top]=item;
        }
     }

void POP()
    {
     if(top<=0)
      {
      printf("\nstack is empty");
      }
        else
           {
         item=a[top];
         top--;
         printf("\nitem is deleted %d",item);
          //     count --;
           }
    }
void DISPLAY()
       {
    int i;
    if(top!=0)
    {
      for(i=top;i>0;i--)
      printf("%5d",a[i]);
    }
    else
    {
    printf("\nstack is empty");

    }
       }
void PEEP()
    {
    int loc;
     if(top==0)
      {
      printf("\nstack is empty");
      }
        else
        {
        printf("\nEnter location to search");
        scanf("%d",&loc);
         if(loc<=top)
         printf("\nelement is %d",a[loc]);
         else
         printf("\nelement is not found") ;
        }

    }
void CHANGE()
    {
    int loc,ele;
    if(top==0)
    {
    printf("\nstack is empty");
    }
     else
     {
      printf("\nEnter the location to change");
      scanf("%d",&loc);
      printf("\nEnter element");
      scanf("%d",&ele);
        a[loc]=ele;
      }
     }