Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

28 Mar 2013

STAR PATTERN5

Q) Write a Program to Print The Following Star Pattern in c++
     *
   ***
  *****
 *******
*********

Sol)


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j,n,k;
 cout<<"Enter number of rows"<<endl;
 cin>>n;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n-i;j++)
  {
   cout<<" ";
  }
   for(k=1;k<=2*i-1;k++)
    {
     cout<<"*";
    }
    cout<<endl;
 }
 getch();
}

                                               ===OUTPUT===

STAR PATTERN1

Q) Write a Program to Print The Following Star Pattern in c++
      *
      **
      ***
      ****
      *****

Sol)

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int i,j,n;

   cout<<"Enter number of rows"<<endl;
  cin>>n;
 
  for(i=1;i<=n;i++)
     {
         for(j=1;j<=i;j++)
           {
               cout<<"*";
           }
        cout<<endl;
     }
 getch();
}

                                     ===OUTPUT===

19 Mar 2013

SUBTRACTION OF TWO MATRICES IN C++

Write a program for subtraction of two matrices
                      OR
Write a program to subtract two matrices

#include<iostream.h>
#include<conio.h>
void main()
{  
clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of columns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of column 2nd matrix\n";
 cin>>q;
 if(m==p && n==q)
 {
  cout<<"\n\nenter the elements of 1st matrix:=\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\n1st matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix:=\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]-b[i][j];
    }
  }
  cout<<"\n\nsubtraction of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be subtracted";
     }

getch();
}

==OUTPUT==
enter no of rows of  1st matrix
2
enter no of columns of  1st matrix
2
enter no of rows of 2nd matrix
2
enter no of columns of 2nd matrix
2
enter the elements of 1st matrix:=
a[1][1]:=1
a[1][2]:=2
a[2][1]:=3
a[2][2]:=1

1st matrix you have entered is:=
    1    2
    3    1

enter the elements of 2nd matrix:=
b[1][1]:=2
b[1][2]:=5
b[2][1]:=0
b[2][2]:=5

2nd matrix you have entered is:=
    2    5
    0    5

subtraction of two matrices is:=
    -1   -3
     3   -4

ADDITION OF MATRIX IN C++

Write a program for addition of two matrices
                      OR
Write a program to add two matrices

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of columns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of column 2nd matrix\n";
 cin>>q;
 if(m==p && n==q)
 {
  cout<<"\n\nenter the elements of 1st matrix:=\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\n1st matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix:=\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]+b[i][j];
    }
  }
  cout<<"\n\nsum of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be added";
     }

getch();
}

==OUTPUT==
enter no of rows of  1st matrix
2
enter no of columns of  1st matrix
2
enter no of rows of 2nd matrix
2
enter no of columns of 1st matrix
2
enter the elements of 1st matrix:=
a[1][1]:=1
a[1][2]:=2
a[2][1]:=3
a[2][2]:=1

1st matrix you have entered is:=
    1    2
    3    1

enter the elements of 2nd matrix:=
b[1][1]:=2
b[1][2]:=5
b[2][1]:=0
b[2][2]:=5

2nd matrix you have entered is:=
    2    5
    0    5

sum of two matrices is:=
    3    7
    3    6

10 Mar 2013

STAR PATTERN4

Q.) Write a Program to Print The Following Star Pattern in c++
          *****
          ****
          ***
          **
          *

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

void main()
{

    clrscr();
    int i,j,n;
    cout<<"Enter number of rows"<<endl;
    cin>>n;
    for(i=n;i>=1;i--)
     {
         for(j=1;j<=i;j++)
             {
                  cout<<"*";
             }
        cout<<endl;
     }

   getch();
}

                                              =OUTPUT=

Enter number of rows 5
*****
****
***
**
*

PRINTING A MATRIX IN C++

Q.) Write a C++ Program to input the elements of  a matrix and then print them in matrix form.

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

void main()
{
   clrscr();
   int a[10][10];
   int i, j, n, m;
  
   cout<<"enter no of rows  \n";
   cin>>n;
   cout<<"enter no of colomn  \n";
   cin>>m;
   cout<<"enter the values of matrix  \n";
  
   for(i=0;i<n;i++)
     {
         for(j=0;j<m;j++)
           {
               cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
               cin>>a[i][j];
           }
     }
   cout<<"matrix u have entered is \n ";
  
   for(i=0;i<n;i++)
      {
          for(j=0;j<m;j++)
            {
                  cout<<"\t"<<a[i][j];
            }
         cout<<"\n";
     }

  getch();
}


                                        =OUTPUT=

1 Mar 2013

Simple Linked List

Q) A C program of simple Linked List without any user defined function.


SOl)

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

void main()
{
   struct node
     {
          int data;
          struct node*next;
     };

   typedef struct node node ;
   int d ;
   node *start=NULL, temp, *save ;
   int option ;
     
   printf("Enter the data for the node\n ");
   scanf("%d",&d) ;
   
   start=(node*)malloc(sizeof (node)) ;
   start->data ;
   start->next= NULL ;
   save = start ;
   
   printf("Do you want to enter more data \n" ) ;
   printf("Press 1 for yes otherwise 0 \n ");
   scanf("%d",&option) ;

   while(option==1)
     {
         printf("Enter the data for the node\n ") ;
         scanf("%d", &d) ;

         temp = (node*)malloc(sizeof (node)) ;
         temp->data=d;
         temp->next= NULL ;
         save->next= temp ;
         save=temp;

         printf("Do you want to enter more data \n") ;
         printf("Press 1 for yes otherwise 0 \n ");
         scanf("%d", &option) ;
 
    }

  printf("The Linked List is \n") ;
  
  while(start!= NULL)
      {
           printf("%d->", start->data);
           start = start->next ;
      }
   getch();
}