Thursday 30 May 2013

program f matrix without class

#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10],row,col,num,c[10][10],m,n,p,q,i,j,k;
      cout<<"\n\t\t\t***********************";
      cout<<"\n\t\t\t* PROGRAM OF MATRICES *";
      cout<<"\n\t\t\t***********************";
      cout<<"\n\n\n\n\t\tMade By:";
      cout<<"\tzeeshan";
      cout<<"\n\n\n\n\n\n\n\t\t\t\t\tPress any key to continue....!!";
      getch();
do{
      system("cls");
      cout<<"\t\t\t"<<(char)2<<" MAIN MENU "<<(char)2<<endl<<endl;
      cout<<"\t\t1.Addition of matrix"<<endl;
      cout<<"\t\t2.Substraction of matrix"<<endl;
      cout<<"\t\t3.Transpose of a matrix"<<endl;
      cout<<"\t\t4.Multiplication of  matrices\n"<<endl;
      cout<<"\t\t5.Exit program\n\n";
      cout<<"\n enter your choice :\n";
      cin>>num;
switch(num)
{
           case 1:
                {
                          system("cls");
                          cout<<" enter number of rows :"<<endl;
                          cin>>row;
                          cout<<" enter number of columns :"<<endl;
                          cin>>col;
                          cout<<" enter values of first matrix :"<<endl;
                          for(i=0;i<col;i++) //to enter values of first matrix
                          {
                          for(j=0;j<row;j++)
                          {
                          cin>>a[i][j];
                          }
                          cout<<endl;
                          }
                          cout<<" enter values of 2nd matrix :"<<endl;          
                          for(i=0;i<col;i++) //to enter values of 2nd matrix
                          {
                          for(j=0;j<row;j++)
                          {
                          cin>>b[i][j];
                          }
                          cout<<endl;
                          }
                          system("cls");    
                          cout<<"\nfirst matrix:\n";
                          for(i=0;i<row;++i)
                          {for(j=0;j<col;++j)
                          {cout<<a[i][j]<<" ";}
                          cout<<endl;}
                          cout<<"\nsecond matrix:\n";
                          for(i=0;i<row;++i)
                          {for(j=0;j<col;++j)
                          {cout<<b[i][j]<<" ";}
                          cout<<endl;}  
                          cout<<"the additon of your matices is :\n"<<endl;
                          for(i=0;i<col;i++)
                          {
                          for(j=0;j<row;j++)
                          {
                          a[i][j]=a[i][j]+b[i][j];
                          cout<<a[i][j]<<" ";
                          }
                          cout<<endl<<endl;
                          }
                          getch();
                          break;
                          }
           case 2:
                {
                          system("cls");
                          cout<<" enter number of rows :"<<endl;
                          cin>>row;
                          cout<<" enter number of columns :"<<endl;
                          cin>>col;
                          cout<<" enter values of first matrix :"<<endl;
                          for(i=0;i<col;i++) //to enter values of first matrix
                          {
                          for(j=0;j<row;j++)
                          {
                          cin>>a[i][j];
                          }
                          cout<<endl;
                          }
                          cout<<" enter values of 2nd matrix :"<<endl;          
                          for(i=0;i<col;i++) //to enter values of 2nd matrix
                          {
                          for(j=0;j<row;j++)
                          {
                          cin>>b[i][j];
                          }
                          cout<<endl;
                          }
                          system("cls");
                          cout<<"\nfirst matrix:\n";
                          for(i=0;i<row;++i)
                          {for(j=0;j<col;++j)
                          {cout<<a[i][i]<<" ";}
                          cout<<endl;}
                          cout<<"\nsecond matrix:\n";
                          for(i=0;i<row;++i)
                          {for(j=0;j<col;++j)
                          {cout<<b[i][j]<<" ";}
                          cout<<endl;}        
                          cout<<"the substraction of your matices is :\n"<<endl;
                          for(i=0;i<col;i++)
                          {
                          for(j=0;j<row;j++)
                          {
                          a[i][j]=a[i][j]-b[i][j];
                          cout<<a[i][j]<<" ";
                          }
                          cout<<endl<<endl;
                          }
                          getch();
                          break;
                          }
           case 3:
                {
                          system("cls");
                          cout<<" enter number of rows :"<<endl;
                          cin>>row;
                          cout<<" enter number of columns :"<<endl;
                          cin>>col;
                          cout<<" enter values of matrix :"<<endl;
                          for(i=0;i<col;i++)
                          {
                          for(j=0;j<row;j++)
                          {
                          cin>>a[i][j];
                          }
                          cout<<endl;
                          }
                          system("cls");
                          cout<<"\n your matrix :\n"<<endl;
                          for(i=0;i<col;i++)
                          {
                          for(j=0;j<row;j++)
                          {
                          cout<<a[i][j];
                          cout<<" ";
                          }
                          cout<<endl<<endl;
                          }        
                          cout<<"\n the transpose of your matrix :\n"<<endl;          
                          for(i=0;i<col;i++)
                          {
                          for(j=0;j<row;j++)
                          {
                          cout<<a[j][i];
                          cout<<" ";
                          }
                          cout<<endl<<endl;
                          }
                          getch();
                          break;
                          }
           case 4:
           {
                          system("cls");
                         
                          cout<<(char)2<<" You can multiply two matrices if, and only if,\n";
                          cout<<"  the number of columns in the first matrix equals\n";
                          cout<<"\n the number of rows in the second matrix. "<<endl<<endl;
                          cout<<"Enter rows and columns of first matrix:";
                          cin>>m>>n;
                          cout<<"Enter rows and columns of second matrix:";
                          cin>>p>>q;

                          if(n==p)
                          {
                          cout<<"\nEnter first matrix:\n";
                          for(i=0;i<m;++i)
                          for(j=0;j<n;++j)
                          cin>>a[i][j];
                          cout<<"\nEnter second matrix:\n";
                          for(i=0;i<p;++i)
                          for(j=0;j<q;++j)
                          cin>>b[i][j];
                          system("cls");
                          cout<<"\nfirst matrix:\n";
                          for(i=0;i<m;++i)
                          {for(j=0;j<n;++j)
                          {cout<<a[i][i]<<" ";}
                          cout<<endl;}
                          cout<<"\nsecond matrix:\n";
                          for(i=0;i<p;++i)
                          {for(j=0;j<q;++j)
                          {cout<<b[i][j]<<" ";}
                          cout<<endl;}
                          cout<<"\nThe new matrix is:\n";
                          for(i=0;i<m;++i)
                          {
                          for(j=0;j<q;++j)
                          {
                          c[i][j]=0;
                          for(k=0;k<p;++k)
                          { c[i][j]=c[i][j]+(a[i][k]*b[k][j]);}
                          cout<<c[i][j]<<" ";
                          }
                          cout<<"\n";
                          }
                          }
                          else
                          cout<<"\nSorry!!!! Matrix multiplication can't be done";
                          getch();
                          break;
                          }
                         
            case 5:
                 {
                          cout<<"\n\n\t\t\t\t\t\tbye...!!!";
                          }
                          }
                          }while(num!=5);
getch();
}

program of matrix

#include<iostream.h>
#include<conio.h>
class matrix
{
int a[10][10];
int b[10][10];
int row,col,num,i,j;
public:
void start();
void add();
void sub();
void trans();
void mul();

};
void matrix::start()
{
     do{
     system("cls");
cout<<"\t\t\t"<<(char)2<<" MAIN MENU "<<(char)2<<endl<<endl;
cout<<"\t\t1.Addition of matrix"<<endl;
cout<<"\t\t2.Substraction of matrix"<<endl;
cout<<"\t\t3.Transpose of a matrix"<<endl;
cout<<"\t\t4.Multiplication of  matrices\n"<<endl;
cout<<"\t\t5.Exit program\n\n";
cout<<"\n enter your choice :\n";
cin>>num;
switch(num)
{
           case 1:
                {
                          system("cls");
                          add();
                          break;
                          }
           case 2:
                {
                          system("cls");
                          sub();
                          break;
                          }
           case 3:
                {
                          system("cls");
                          trans();
                          break;
                          }
           case 4:
           {
                          system("cls");
                          mul();
                          break;
                          }
            case 5:
                 {
                          cout<<"\n\n\t\t\t\t\t\tbye...!!!";
                          }
                          }
                          }while(num!=5);
getch();
}
void matrix::add()
{
     system("cls");
     cout<<" enter number of rows :"<<endl;
     cin>>row;
     cout<<" enter number of columns :"<<endl;
     cin>>col;
     cout<<" enter values of first matrix :"<<endl;
     for(i=0;i<col;i++) //to enter values of first matrix
     {
        for(j=0;j<row;j++)
        {
                cin>>a[i][j];
                cout<<" ";
                }
                cout<<endl;
                }
     cout<<" enter values of 2nd matrix :"<<endl;           
     for(i=0;i<col;i++) //to enter values of 2nd matrix
     {
       for(j=0;j<row;j++)
        {
                cin>>b[i][j];
                cout<<" ";
                }
                cout<<endl;
                }
     system("cls");     
     cout<<"\nfirst matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<a[i][j]<<" ";}
     cout<<endl;}
     cout<<"\nsecond matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<b[i][j]<<" ";}
     cout<<endl;}    
     cout<<"the additon of your matices is :\n"<<endl;
     for(i=0;i<col;i++)
     {
        for(j=0;j<row;j++)
        {
                a[i][j]=a[i][j]+b[i][j];
                cout<<a[i][j]<<" ";
                }
                cout<<endl<<endl;
                }
                }
void matrix::sub()
{
     system("cls");
     cout<<" enter number of rows :"<<endl;
     cin>>row;
     cout<<" enter number of columns :"<<endl;
     cin>>col;
     cout<<" enter values of first matrix :"<<endl;
     for(i=0;i<col;i++) //to enter values of first matrix
     {
        for(j=0;j<row;j++)
        {
                cin>>a[i][j];
                cout<<" ";
                }
                cout<<endl;
                }
     cout<<" enter values of 2nd matrix :"<<endl;           
     for(i=0;i<col;i++) //to enter values of 2nd matrix
     {
       for(j=0;j<row;j++)
        {
                cin>>b[i][j];
                cout<<" ";
                }
                cout<<endl;
                }
     system("cls");
     cout<<"\nfirst matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<a[i][i]<<" ";}
     cout<<endl;}
     cout<<"\nsecond matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<b[i][j]<<" ";}
     cout<<endl;}         
     cout<<"the substraction of your matices is :\n"<<endl;
     for(i=0;i<col;i++)
     {
        for(j=0;j<row;j++)
        {
                a[i][j]=a[i][j]-b[i][j];
                cout<<a[i][j]<<" ";
                }
                cout<<endl<<endl;
                }
     }
void matrix::trans()
{
     system("cls");
     cout<<" enter number of rows :"<<endl;
     cin>>row;
     cout<<" enter number of columns :"<<endl;
     cin>>col;
     cout<<" enter values of matrix :"<<endl;
     for(i=0;i<col;i++) //to enter values of matrix
     {
        for(j=0;j<row;j++)
        {
                cin>>a[i][j];
                cout<<" ";
                }
                cout<<endl;
                }
     system("cls");
     cout<<"\nfirst matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<a[i][i]<<" ";}
     cout<<endl;}
     cout<<"\nsecond matrix:\n";
     for(i=0;i<row;++i)
     {for(j=0;j<col;++j)
     {cout<<b[i][j]<<" ";}
     cout<<endl;}
     cout<<" your matrix :"<<endl;  
     for(i=0;i<col;i++) 
     {
        for(j=0;j<row;j++)
        {
                cout<<a[i][j];
                cout<<" ";
                }
                cout<<endl<<endl;
                }         
     cout<<"\n the transpose of your matrix :\n"<<endl;           
     for(i=0;i<col;i++) 
     {
        for(j=0;j<row;j++)
        {
                cout<<a[j][i];
                cout<<" ";
                }
                cout<<endl<<endl;
                }
     }
void matrix::mul()
{
     int c[5][5],m,n,p,q,i,j,k;
 cout<<(char)2<<" You can multiply two matrices if, and only if,\n";
 cout<<"  the number of columns in the first matrix equals\n";
 cout<<"\n the number of rows in the second matrix. "<<endl<<endl;
 cout<<"Enter rows and columns of first matrix:";
 cin>>m>>n;
 cout<<"Enter rows and columns of second matrix:";
 cin>>p>>q;

 if(n==p)
 {
   cout<<"\nEnter first matrix:\n";
   for(i=0;i<m;++i)
   for(j=0;j<n;++j)
   cin>>a[i][j];
   cout<<"\nEnter second matrix:\n";
   for(i=0;i<p;++i)
   for(j=0;j<q;++j)
   cin>>b[i][j];
   system("cls");
   cout<<"\nfirst matrix:\n";
   for(i=0;i<m;++i)
   {for(j=0;j<n;++j)
   {cout<<a[i][i]<<" ";}
   cout<<endl;}
   cout<<"\nsecond matrix:\n";
   for(i=0;i<p;++i)
   {for(j=0;j<q;++j)
   {cout<<b[i][j]<<" ";}
   cout<<endl;}
   cout<<"\nThe new matrix is:\n";  
   for(i=0;i<m;++i)
   {
   for(j=0;j<q;++j)
   {
   c[i][j]=0;
   for(k=0;k<p;++k)
   { c[i][j]=c[i][j]+(a[i][k]*b[k][j]);}
   cout<<c[i][j]<<" ";
   }
   cout<<"\n";
   }
 }
 else
 cout<<"\nSorry!!!! Matrix multiplication can't be done";
}
main()
{
      system("cls");
      matrix h;
      cout<<"\n\t\t\t***********************";
      cout<<"\n\t\t\t* PROGRAM OF MATRICES *";
      cout<<"\n\t\t\t***********************";
      cout<<"\n\n\n\n\t\tMade By:";
      cout<<"\tzeeshan";
      cout<<"\n\n\n\n\n\n\n\t\t\t\t\tPress any key to continue....!!";
      getch();
 h.start();
}

Wednesday 10 April 2013

program using functions to ask user enter to number and choice its option of mathematical operation to perform in a loop and loop only breaks if user enter exit option


#include<iostream.h>
#include<conio.h>
using namespace std;
int add(int,int);
int sub(int,int);
int mul(int,int);
int divi(int,int);
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. ADD NUMBERS"<<endl;
      cout<<"2. SUBSTRAT NUMBERS"<<endl;
      cout<<"3. MULIPLY NUMBERS"<<endl;
      cout<<"4. DIVIDE NUMBERS"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 cout<<"\nADDITION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=add(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 cout<<"\nSUBSTRACTIN"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=sub(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 3:
                         {
                                 cout<<"\nMULTIPLICATION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=mul(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 4:
                         {
                                 cout<<"\nDIVISION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=divi(num1,num2);
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  
int add(int a, int b)
{
    int d;
    d=a+b;
    return d;
}
int sub(int e, int f)
{  
    int g;
    g=e-f;
    return g;
}
int mul(int h, int i)
{  
    int j;
    j=h*i;
    return j;
}
int divi(int k, int l)
{  
    int m;
    m=k/l;
    return m;
}    

program to find factorial of number,number prime or not and number even or odd through functions


#include<iostream.h>
#include<conio.h>
using namespace std;
void fact(int);
void prime(int);
void even_odd(int);
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {        
      cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
      cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
      cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
      cout<<"4. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 system("cls");
                                 cout<<"\nfactorial of a number"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 fact(num1);
                                 break;
                                 }
                     case 2:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 prime(num1);
                                 break;
                                 }
                     case 3:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 even_odd(num1);
                                 break;
                                 }
                      case 4:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  
void fact(int a)
{
    int f=1,i=1;
    while(i<=a)
    {
          f=f*i;
          i++;    
    }
    cout<<"\n"<<a<<"! = "<<f<<endl;
}
void prime(int num)
{  
     int i=2;
     while(i<=num-1)
     {
   
     if(num%i==0)
     {
     cout << "\n" << num << " is not a prime number.\n";
     break;
     }
   
     i++;
     }
     if(num==1)
     {cout << "\n" << num << " is a prime number.\n";}
     if(i==num)
     cout << "\n" << num << " is a prime number.\n";
}
void even_odd(int e)
{  
    if(e%2==0)
    {cout<<e<<" is even number"<<endl;}
    else
    {cout<<e<<" is odd number"<<endl;}
}

Write a menu driven program using switch case statement (without functions) that has the following options: 1. Factorial of a number 2. Prime or not 3. Odd or even 4. Power ( x raise to power y) 5. Exit Input should be taken inside the particular case. For example ifthe user enters choice is 4. Then in case 4 your program can prompt the user to input value of x and then value of y. The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);


#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
      cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
      cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
      cout<<"4. POWER(x raise to power y)"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 system("cls");
                                 cout<<"\nfactorial of a number"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int f=1,i=1;
                                 while(i<=num1)
                                 {
                                 f=f*i;
                                 i++;    
                                 }
                                 cout<<"\n"<<num1<<"! = "<<f<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int j=2;
                                 while(j<=num1-1)
                                 {
                                 if(num1%j==0)
                                 {
                                 cout << "\n" << num1 << " is not a prime number.";
                                 break;
                                 }
                                 i++;
                                 }
                                 if(j==num1)
                                 cout << "\n" << num1 << " is a prime number.";
                                 break;
                                 }
                     case 3:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 if(num1%2==0)
                                 {cout<<num1<<" is even number"<<endl;}
                                 else
                                 {cout<<num1<<" is odd number"<<endl;}
                                 break;
                                 }
                     case 4:
                         {
                                 system("cls");
                                 cout<<"\nPOWER(x raise to power y)"<<endl;
                                 cout<<"\nEnter value of x :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter value of y :"<<endl;
                                 cin>>num2;
                                 sum=pow(num1,num2);
                                 cout<<"\n"<<num1<<" raise to power "<<num2<<" = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  

program to ask user enter to number and choice its option of mathematical operation to perform in a loop and loop only breaks if user enter exit option


#include<iostream.h>
#include<conio.h>
using namespace std;
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. ADD NUMBERS"<<endl;
      cout<<"2. SUBSTRAT NUMBERS"<<endl;
      cout<<"3. MULIPLY NUMBERS"<<endl;
      cout<<"4. DIVIDE NUMBERS"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 cout<<"\nADDITION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1+num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 cout<<"\nSUBSTRACTIN"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1-num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 3:
                         {
                                 cout<<"\NMULTIPLICATION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1*num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                     case 4:
                         {
                                 cout<<"\nDIVISION"<<endl;
                                 cout<<"\nEnter 1st Number :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter 2nd Number :"<<endl;
                                 cin>>num2;
                                 sum=num1/num2;
                                 cout<<"\nAnswer = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  

TicTacToe gme in c++


#include<iostream.h>
#include<conio.h>
using namespace std;
main()
{
char a[9]={'0','1','2','3','4','5','6','7','8'};
int n=1,m=0,num;
do
{
system("cls");
cout<<(char)4<<" Welcome into tic tac toe game "<<(char)4<<"\n"<<endl;      
cout<<"  "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";
if(n==m%2)
{
cout<<"\n2nd Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
 for(int k=0;k<9;k++)
 {
         if(num==k)
         {                                                                        
          a[k]='X';
         }
 }
}
}
if(m%2==0){
cout<<"\n1st Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
for(int k=0;k<9;k++)
 {
         if(num==k)
         {                                                                        
          a[k]='Y';
         }
 }
}
}
if((a[0]==a[1]&&a[1]==a[2]&&a[3]==a[0])||(a[0]==a[3]&&a[3]==a[6]&&a[0]==a[6])||(a[0]==a[4]&&a[4]==a[8]&&a[0]==a[8])||(a[1]==a[4]&&a[4]==a[7]&&a[1]==a[7])||(a[2]==a[5]&&a[5]==a[8]&&a[2]==a[8])||(a[2]==a[4]&&a[4]==a[6]&&a[2]==a[6])||(a[3]==a[4]&&a[4]==a[5]&&a[3]==a[5])||(a[6]==a[7]&&a[7]==a[8]&&a[6]==a[8]))
{
system("cls");                                                                                                                                                                                                                                                                                                                
cout<<"  "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<"  "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
cout<<"\nYOU Won!!\n"<<(char)2<<(char)2<<endl;
break;
}
else{cout<<"you lose\n"; }
m++;
}
while((a[0]!=a[1]&&a[1]!=a[2]&&a[3]!=a[0])||(a[0]!=a[3]&&a[3]!=a[6]&&a[0]!=a[6])||(a[0]!=a[4]&&a[4]!=a[8]&&a[0]!=a[8])||(a[1]!=a[4]&&a[4]!=a[7]&&a[1]!=a[7])||(a[2]!=a[5]&&a[5]!=a[8]&&a[2]!=a[8])||(a[2]!=a[4]&&a[4]!=a[6]&&a[2]!=a[6])||(a[3]!=a[4]&&a[4]!=a[5]&&a[3]!=a[5])||(a[6]!=a[7]&&a[7]!=a[8]&&a[6]!=a[8]));
cout<<"\nbye";
getch();
}

Tuesday 5 March 2013

calculate factorial and double factorial of a given number-


#include <iostream.h>
using namespace std;
//factorial function
int factorial(int n)
{
int res = 1,i;
for (i=1;i<=n;i++)
{
 res *= i;
}
return res;
}
//double factorial function
int dfact(int n)
{
int i;double res=1.0;
for(i=n;i>=1;i-=2)
{
res *=i;
}
return res;
}
int main( )
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
  system("pause");
}

1+(1+2)+(1+2+3)+..........


#include<iostream.h>
#include<conio.h>
main()
{
      int a=1;
      cout<<a;
      for(int i=1;i<5;i++)
      {
              cout<<"+(";
              for(int j=1;j<=i;j++)
              {
              cout<<j<<"+";
              }
              cout<<i+1<<")";
       }
              getch();
}