Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Friend Class Practice

Sunday, June 12, 2011


#include<iostream>
using namespace std;
#include<conio.h>
//class d;
class a
{private:
int c,b;
public:
a()
{
c=b=4;
}
friend class d;
};
class d
{public:
void sum(a ob)
{
int summ=ob.c+ob.b;
cout<<"sum is"<<summ;

}
};
void main()
{
a ob;
d ob1;
ob1.sum(ob);
_getch();
}

Two-Dimensional-Arrays to input two m x n Matrices

Tuesday, June 7, 2011

#include<iostream.h>

int main()
{
    int i,j,choice;
   int mat1[3][3];
   int mat2[3][3];
   int sum[3][3];

    cout<<"Enter elements for Matrices 1:" <<endl;
      for(i=0;i<=(3-1);i++)
      {
          for(j=0;j<=(3-1);j++)
          {
             cin>>mat1[i][j];
          }
      }

    cout<<"Enter elements for Matrices 2:" <<endl;
      for(i=0;i<=(3-1);i++)
      {
          for(j=0;j<=(3-1);j++)
          {
             cin>>mat2[i][j];
          }
      }

    cout<<"What do you want to do ? \n\n";
    cout<<"1.Addition\n2.Multiplication\n";
    cin>>choice;

    switch(choice)
      {
        case 1:

        for(i=0;i<=(3-1);i++)
        {
          for(j=0;j<=(3-1);j++)
          {
             sum[i][j]=mat1[i][j]+mat2[i][j];
          }
        }

        break;

        case 2:

        for(i=0;i<=(3-1);i++)
        {
          for(j=0;j<=(3-1);j++)
          {
             sum[i][j]=mat1[i][j]*mat2[i][j];
          }
        }

        break;
      }


    for(i=0;i<=(3-1);i++)
    {
      for(j=0;j<=(3-1);j++)
      {
         cout<<"\n"<<sum[i][j];
      }
    }
    cout<<endl;

return 0;
}

A Pascal triangle of any Size

#include <iostream.h>
#include <math.h>

void constructPascal(int numRows);

int main(void)
{
  int numRows;

  cout <<"Enter the number of rows of the Pascal Triangle: ";
  cin >> numRows;
  constructPascal(numRows);

return 0;
}


void constructPascal(int numRows)
{
   int n,k;
   int simple[23][23] = {1};
   
    for(n=1; n<=(numRows-1); n++)
    {
         for(k=1; k<=(numRows-1); k++)
         {
             simple[n][k]=simple[n-1][k-1]+simple[n-1][k];
             cout << "\t"<<simple[n][k];
         }
    cout <<"\n";
    }
}

Array in Ascending & Descending order

#include <iostream.h>

int main()
{

  int array[10];
  int temp;

  for(int x=0;x<10;x++)
  {
  cout << "Enter Integer No. " << x+1 << " : " ;
  cin>>array[x];
  }

  for (x=0;x<10;x++)
  {
      for(int y=0;y<9;y++)
      {
          if(array[y]>array[y+1])
          {
          temp=array[y];
          array[y]=array[y+1];
          array[y+1]=temp;
          }
      }
  }


  cout << "Array in Ascending order is : ";

  for (x=0;x<10;x++)
  {
  cout << endl << array[x];
  }


  for (x=0;x<10;x++)
  {
      for(int y=0;y<9;y++)
      {
          if(array[y]<array[y+1])
          {
          temp=array[y];
          array[y]=array[y+1];
          array[y+1]=temp;
          }
      }
  }

  cout << endl;
  cout << "Array in Descending order is : ";

  for (x=0;x<10;x++)
  {
  cout << endl << array[x];
  }

return 0;
}

0’s-diagonal is in reverse order(Two dimension ARRAYS)

#include <iostream.h>
#include <iomanip.h>
int main(){

    const int rowSize=6;
    const int colSize=6;
    int table [rowSize][colSize];

      for (int row=0; row < rowSize; row++)
      {
          for (int col=0; col < colSize; col++)
          {
          if (row + col ==5)
          table[row][col] = 0;
          else if (row + col < 5)
          table[row][col] = -1;
          else
          table[row][col] = 1;
          } // end for - col
      } // end for - row


      for (row = 0; row < rowSize; row++)
      {
          for (int col =0; col < colSize; col++)
          cout << setw(4) << table[row][col];
          cout << endl;
      } // end for - row - (print)


return 0;
} // end main

0’s-diagonal order(Two dimension ARRAYS)

#include <iostream.h>
#include <iomanip.h>
int main(){

    const int rowSize=6;
    const int colSize=6;
    int table [rowSize][colSize];

      for (int row=0; row < rowSize; row++)
      {
          for (int col=0; col < colSize; col++)
          {
          if (row == col)
          table[row][col] = 0;
          else if (row > col)
          table[row][col] = -1;
          else
          table[row][col] = 1;
          } // end for - col
      } // end for - row


      for (row = 0; row < rowSize; row++)
      {
          for (int col =0; col < colSize; col++)
          cout << setw(4) << table[row][col];
          cout << endl;
      } // end for - row - (print)


return 0;
} // end main

Two character array out of which one array will take your Name & other its copy.

#include <stdio.h>     /* stdin, printf, and fgets */
#include <string.h>   /* for all the new-fangled string functions */
#include <iostream.h>

void strip_newline( char *str, int size )
{
     int i;

     /* remove the null terminator */
     for (  i = 0; i < size; ++i )
     {
          if ( str[i] == '\n' )
          {
                str[i] = '\0';

                /* we're done, so just exit the function by returning */
                return;
          }
     }
     /* if we get all the way to here, there must not have been a newline! */
}

int main()
{
     char name[50];
     char lastname[50];
     char fullname[100]; /* Big enough to hold both name and lastname */

     cout << "Please enter your Name : " ;
     cin >> name, 50, stdin ;

     /* see definition above */
     strip_newline( name, 50 );

     /* strcmp returns zero when the two strings are equal */
     if ( strcmp ( name, "Mudassir" ) == 0 )
     {
          cout << "That's my Name too.\n" ;
     }
     else
     {
          cout << "That's not my Name.\n" ;
     }
     // Find the length of your name
     cout << "Your Name Is " << strlen ( name ) <<" Letter Long !" <<endl ;
     cout << "Enter your last Name : " ;
     cin >> lastname, 50, stdin ;
     strip_newline( lastname, 50 );
     fullname[0] = '\0';
     /* strcat will look for the \0 and add the second string starting at
         that location */
     strcat( fullname, name );     /* Copy name into full name */
     strcat( fullname, " " );      /* Separate the names by a space */
     strcat( fullname, lastname ); /* Copy lastname onto the end of fullname */
     cout << "Your full Name Is : "<<fullname ;

     getchar();

return 0;
}

Checking Prime Number(By Arrays)

#include <iostream.h>
void prime(int x[]);
int main()
{
    int a[10];
    for (int b=0;b<10;b++)
    {
        cout <<"Enter the Number " << b << ": ";
        cin>>a[b];
        cout <<"\n\n";
    }
    prime(a);
    return 0;
}
void prime(int x[])
{
    int check=0;
    for (int c=0;c<10;c++)
    {
        check=0;
        for (int a=2;a<x[c];a++)
        {
            if (x[c]%a==0)
            {
                check++;
            }
            else
            {
                check=check;
            }
        }
        if (check>0)
        {
            cout <<"\n\n"<<x[c]<<" is not prime";
        }
        else
        {
            cout <<"\n\n"<<x[c]<<" is prime ";
        }
    }
    return;
}

Deep Practice of Arrays

/* Declare two char arrays A and B and input the values in it . Declare another
   Array C as int array and fill it in such way that it contains 1 for the index
   where values of "A" & "B" are same and 2 otherwise . Output on the screen the
   values of all three arrays */

#include<iostream.h>
int main()
{      
    char a[10];
    char b[10];
    int c[10] = {0};
    cin>>a;
    cin>>b;
    for (int i=0;a[i] !=0 ;i++)
    {
        if (a[i]==b[i])
        {
            c[i]=1;
        }
        else
        {
            c[i]=2;
        }
    }
    cout << endl;

    for(int h = 0 ; a[h] !=0 ; h++)
    {
       cout << a[h];
    }



    cout << endl;

    for(int lo = 0 ; c[lo] ; lo++ )
    {
       cout << c[lo];
    }

    cout << endl;
for(int l = 0 ; b[l] !=0 ; l++)
    {
       cout << b[l];
    }
    return 0;
}

Even , Odd , Positive and Negative(By Arrays)

#include <iostream.h>
void e_o(int x[]);
void p_n(int y[]);
int main()
{
    int a[10];
    for (int b=0;b<10;b++)
    {
        cout <<"Enter Number " << b << ": ";
        cin>>a[b];
        cout <<endl;
    }
    e_o(a);
    p_n(a);
    return 0;
}
void e_o(int x[])
{
    for (int a=0;a<10;a++)
    {
        if (x[a]%2==0)
        {
            cout <<x[a]<<" is Even "<<endl;
        }
        else
        {
            cout <<x[a]<<" is Odd "<<endl;
        }
    }
    return;
}
void p_n(int x[])
{
    for (int a=0;a<10;a++)
    {
        if (x[a]<0)
        {
            cout <<x[a]<<" is Negetive "<<endl;
        }
        else
        {
            cout <<x[a]<<" is Positive "<<endl;
        }
    }
    return;


}

Bar Chart printing program By Students Numbers(By Arrays)

#include <iostream.h>
int main()
{
    const int arraysize=10;
    int a[arraysize];
    for (int b=0;b<arraysize;b++)
    {
        cout<<"Enter the number of students " << b <<": " ;
        cin>>a[b];
        cout<<endl;
    }
    for (int i=0;i<arraysize;i++)
    {
        if (i==0)
            cout <<" 0 - 9 : ";
        else if (i==100)
            cout <<"  100 : ";
        else
            cout <<i*10<<" - "<<(i*10)+9<<" : ";
        for (int stars=0;stars<a[i];stars++)
        cout <<"*";
        cout <<endl;
    }
    return 0;
}

Bar Chart printing program(By Arrays)

#include <iostream.h>
int main()
{
    const int arraysize=10;
    int a[arraysize]={0,0,0,0,0,1,2,4,2,1};
    for (int i=0;i<arraysize;i++)
    {
        if (i==0)
            cout <<" 0 - 9 : ";
        else if (i==100)
            cout <<"  100 : ";
        else
            cout <<i*10<<" - "<<(i*10)+9<<" : ";
        for (int stars=0;stars<a[i];stars++)
        cout <<"*";
        cout <<endl;
    }
    return 0;
}

Guess your answer(Of adding 5 numbers)

Thursday, June 2, 2011

/*Three numbers will be entered by user and the rest of two will be added automatically by Computer.Your answer will be shown after the input of First Number*/
#include<iostream>
using namespace std;
#include<conio.h>
void main()
{
int a,b,c,d,e,sum;
cout<<"All integers should be from 100 to 999"<<endl<<"you will enter 3 integers and computer will 2 extra number automatically to it"<<endl;
cout<<"As you enter first integer the computer will tell you the sum of all 5 integers"<<endl<<"Enter first integer:"<<endl;
cin >> a;
a-=2;
if(a+2==100||a+2==101)
{
cout<<endl<<"20"<<a<<" will be your answer:"<<endl<<endl;
}
else
{
cout<<endl<<"2"<<a<<" will be your answer:"<<endl<<endl;
}
a+=2;
cout<<"Enter second integer:"<<endl;
cin>>b;
c=999-b;
sum=a+b+c;
cout<<"Enter third integer:"<<endl;
cin>>d;
e=999-d;
sum=sum+d+e;
cout<<a<<"+"<<b<<"+"<<c<<"+"<<d<<"+"<<e<<"="<<sum<<endl;
getch();
}