Make Your Search Easy ! :) Use me

Monday, October 16, 2017

Tic Tac Toe

C++ program to create game of Tic-Tac-Toe ( X-0 game).

#include<iostream>
#include<conio.h>
using namespace std;

int grid[9]={49,50,51,52,53,54,55,56,57},turn=1,gp=-1,total=0;

int board()
{
      clrscr();
      std::cout<<"\n\t\t\tTic Tac Toe\n\n";
std::cout<< "\t"<<(char)grid[0]<<"  |  "<<(char)grid[1]<<"  |  "<<(char)grid[2]<<endl;
std::cout<<"\t"<<"----------------"<< "\n";
std::cout<<"\t"<<(char)grid[3]<<"  |  "<<(char)grid[4]<<"  |  "<<(char)grid[5]<<endl;
std::cout <<"\t"<<"----------------"<< "\n";
std::cout<< "\t"<<(char)grid[6]<<"  |  "<<(char)grid[7]<<"  |  "<<(char)grid[8]<<endl;
return 0;
}

int input()
{
int option,mark;
if (turn)
{
cout<<"PLAYER 1 ";
mark=88;
turn=0;
}
else
{
cout<<"PLAYER 2 ";
mark=79;
turn=1;
}
inp:
cout<<"Enter number grid number :";
cin>>option;
switch (option) {
case 1:
if(grid[0]==88 || grid[0]==79)
goto inp;
grid[0]=mark; break;
case 2:
if(grid[1]==88 || grid[1]==79)
goto inp;
grid[1]=mark; break;
case 3:
if(grid[2]==88 || grid[2]==79)
goto inp;
grid[2]=mark; break;
case 4:
if(grid[3]==88 || grid[3]==79)
goto inp;
grid[3]=mark; break;
case 5:
if(grid[4]==88 || grid[4]==79)
goto inp;
grid[4]=mark; break;
case 6:
if(grid[5]==88 || grid[5]==79)
goto inp;
grid[5]=mark; break;
case 7:
if(grid[6]==88 || grid[6]==79)
goto inp;
grid[6]=mark; break;
case 8:
if(grid[7]==88 || grid[7]==79)
goto inp;
grid[7]=mark; break;
case 9:
if(grid[8]==88 || grid[8]==79)
goto inp;
grid[8]=mark; break;
default:
goto inp;
}
total++;
return(0);
}

int check()
{
if((grid[0]==grid[1]&&grid[1]==grid[2]) || (grid[3]==grid[4]&&grid[4]==grid[5]) || (grid[6]==grid[7]&&grid[7]==grid[8]))
{
cout<<(char)grid[0]<<" WON THE GAME";
return(1);
}
else if((grid[0]==grid[3]&&grid[3]==grid[6]) || (grid[1]==grid[4]&&grid[4]==grid[7]) || (grid[2]==grid[5]&&grid[5]==grid[8]))
{
cout<<(char)grid[0]<<" WON THE GAME";
return(1);
}
else if((grid[0]==grid[4]&&grid[4]==grid[8]) || (grid[2]==grid[4]&&grid[4]==grid[6]))
{
cout<<(char)grid[0]<<" WON THE GAME ";
return(1);
}
if(total==9)
{
cout<<"GAME DRAWN";
return(0);
}
return(-1);
}
int main()
{
while(check()<0)
{
board();
input();
}
  }

Common Multiple

C++ program to find the nth common multiple of two given numbers.


#include <iostream>
#include <conio.h>
using namespace std;

int main()
  {
    int a,b,n=0,num;
    std::cout << "Enter two numbers to find common multiple : "<< "\n";
    std::cin>>a>>b;
    std::cout<<"Enter the the common multiple required : ";
    std::cin>>num;
    for (size_t i = 4; ; i++)
    {
      if(i%a==0 && i%b==0)
          n++;
      if (n==num) {
        std::cout<<i;
        break;
      }
    }
    getch();
    return 0;
  }

Matrix

C++ program to find the sum of diagonals of a matrix.



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

using namespace std;
std::vector<vector<int>> matrix;
int r;

int getarray()
  {
    cout<<"Enter the number of rows and colums of array : ";
    cin>>r;
    for (int i = 0; i < r; i++)
    {
      std::vector<int> row;
      for (int j = 0; j < r; j++)
      {
        int value;
        std::cout <<"enter row "<<i+1<<" element "<<j+1<<endl;
        std::cin >>value;
        row.push_back(value);
      }
      matrix.push_back(row);
    }
    return 0;
  }

int showarray()
  {
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < r; j++) {
          std::cout <<matrix[i][j]<<'\t';
          }
        std::cout << '\n';
      }
    return 0;
  }

int diagsum()
  {
      int sum1=0,sum2=0;
      for(int i=0;i<r;i++)
        {
          sum1+=matrix[i][i];
          sum2+=matrix[i][(r-1)-i];
        }
    std::cout<<"\n\nSum of 1st diagonal : "<<sum1;
    std::cout<<"\nSum of 2nd diagonal : "<<sum2;
    return 0;
  }
int main()
  {
    std::cout << "Enter the array :" << '\n';
    getarray();
    std::cout << "\n\nThe array is :\n\n" << '\n';
    showarray();
    diagsum();
    getch();
    return 0;
  }

Tuesday, August 29, 2017

Triangle related problem.

Program to accept 3 sides and check if is it of a triangle. If yes, also find type of the triangle.


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

void main()
  {
    float a, b, c, s, tmp, base, h, max;
    cout<<"Enter 1st side : ";
    cin>>a;
    cout<<"Enter 2nd side : ";
    cin>>b;
    cout<<"Enter 3rd side : ";
    cin>>c;
    max=a;
    base=b;
    h=c;
    if(b>max)
     {
          max=b;
  base=a;
  h=c;
}
    else if(c>max)
        { 
   max=c;
   base=a;
       h=b;
      }
    s=(a+b+c)/2;
    tmp=(s-a)*(s-b)*(s-c)*s;
    if(tmp > 0)
        {
  if(a==b && b==c)
    {
      cout<<"\nThe given triangle is a equilateral triangle";
            }
          else if(pow(max,2)==pow(base,2)+pow(h,2))
            {
      if(a==b || b==c || a==c)
        {
  cout<<"\nThe given triangle is a isosceles right angled triangle";
}
      else if(a!=b  && b!=c)
{
  cout<<"\nThe given triangle is a scalene right angled triangle";
}
           }
 else if(a==b || b==c || a==c)
   {
     cout<<"\nThe given triangle is a isoceles triangle";
   }
 else if(a!=b  && b!=c)
   {
     cout<<"\nThe given triangle is a scalene triangle";
   }
      }
    else
      {
        cout<<"\nThe gives sides donot form a triangle.";
      }
    getch();
  }

Sunday, August 27, 2017

Fibonacci Series

Program to find the Fibonacci value of any given number from 0 to infinity.


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

void main()
 {
    double num,f0,f1,f2;
      cout<<"Enter a number to find Fibonacci value : ";
      cin>>num;
      if(num==0)
      {
          cout<<"0";
          getch();
          return;
         }
      if(num==1)
      {
          cout<<"1";
          getch();
          return;
         }
      f0=0;
      f1=1;
      for(int i=2;i<=num;i++)
       { 
         f2=f0+f1;
         f0=f1;
         f1=f2;
       }
      cout<<f2;
      getch();
   }

Tuesday, August 22, 2017

Infix to Postfix

Program to convert an expression from infix notation into postfix.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>


void main()
{
    char exp[100],inf[100],op[50];
      int len,j=0,k=-1;
      cout<<"Enter the equation : \n";
      gets(exp);
      len=strlen(exp);
      for(int i=0;i<len;i++)
      {
          if(isalnum(exp[i]))
            {
                inf[j]=exp[i];
                  j++;
               }
            else if(!isdigit(exp[i])&&exp[i]!=')')
            {
                  if(exp[i]=='+' || exp[i]=='-')
                  {
                      while(k!=-1 && op[k]!='(')
                        {
                      inf[j]=op[k];
                        j++;
                        k--;
                      }
                        if(op[k]=='(')
                        {
                            k--;
                           }
                     }
                  if(exp[i]=='*'&&op[k]=='/' || exp[i]=='/'&&op[k]=='*')
                  {
                      inf[j]=op[k];
                        j++;
                        k--;
                     }
                  k++;
                op[k]=exp[i];

               }
            else if(exp[i]==')')
            {
                while(op[k]!='(')
                  {

                      inf[j]=op[k];
                        j++;
                        k--;

                     }
                  k--;
               }
         }
      inf[j]='\0';
      cout<<"PostFix Expression is : \t";
      cout<<inf;
      getch();

   }


Thursday, August 17, 2017

Number to Roman Numberals

Program to take numbers (up to 4 digits) as input and give equivalent Roman Numerals as output.


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

void main()
{
      char roman['z'];
    int year,temp;
      cout<<"Enter a year to convert into Roman Numeral: ";
      cin>>year;
      strcpy(roman,"Roman Numeral: ");
      temp=year/1000;
      year%=1000;
      switch(temp)
      {
          case 1: strcat(roman,"M");break;
            case 2: strcat(roman,"MM");break;
            case 3: strcat(roman,"MMM");break;
            case 4: strcat(roman,"MMMM");break;
            case 5: strcat(roman,"MMMMM");break;
            case 6: strcat(roman,"MMMMMM");break;
            case 7: strcat(roman,"MMMMMMM");break;
            case 8: strcat(roman,"MMMMMMMM");break;
            case 9: strcat(roman,"MMMMMMMMM");break;
         }
      temp=year/100;
      year%=100;
      switch(temp)
      {
          case 1: strcat(roman,"C");break;
            case 2: strcat(roman,"CC");break;
            case 3: strcat(roman,"CCC");break;
            case 4: strcat(roman,"CD");break;
            case 5: strcat(roman,"D");break;
            case 6: strcat(roman,"DC");break;
            case 7: strcat(roman,"DCC");break;
            case 8: strcat(roman,"DCCC");break;
            case 9: strcat(roman,"CM");break;
         }
      temp=year/10;
      year%=10;
      switch(temp)
      {
          case 1: strcat(roman,"X");break;
            case 2: strcat(roman,"XX");break;
            case 3: strcat(roman,"XXX");break;
            case 4: strcat(roman,"XL");break;
            case 5: strcat(roman,"L");break;
            case 6: strcat(roman,"LX");break;
            case 7: strcat(roman,"LXX");break;
            case 8: strcat(roman,"LXXX");break;
            case 9: strcat(roman,"XC");break;
         }
      temp=year/1;
      year%=1;
      switch(temp)
      {
          case 1: strcat(roman,"I");break;
            case 2: strcat(roman,"II");break;
            case 3: strcat(roman,"III");break;
            case 4: strcat(roman,"IV");break;
            case 5: strcat(roman,"V");break;
            case 6: strcat(roman,"VI");break;
            case 7: strcat(roman,"VII");break;
            case 8: strcat(roman,"VIII");break;
            case 9: strcat(roman,"IX");break;
         }
      cout<<endl<<roman;
      getch();

   }

Tuesday, August 15, 2017

Open Source Concepts

  1. Free software:

    It is really accessible and can be freely used, changed, improved, copied and distributed by all who wish to do so.
    Eg:
    • Linux Kernel
    • My SQL Relational Database
    • Libre Office

  2. Open Source Software:

    It can be used freely in terms of making modifications, constructing business models around it and so on but it may or may not be free of charge. In this type source code is made available to the user.
    Eg:
    • Apacha HTTP Sever
    • Mozilla Firefox
    • Chromium
    • osCommerce

  3. Proprietary Software:

    This software is neither open nor freely available. Its use is regulated and further distribution and modification are either forbidden or requires special permission by the supplier or vendor.
    Eg:
    • MS Office
    • Adobe Photoshop
    • Microsoft OS
    • Mac OS X

  4. Freeware:

     In spite of having no clear definition of freeware, it is commonly used for software which is available free of cost and which allows copying and further distribution but the source code is not available for modification.
    Eg:
    • Team Viewer
    • Skype
    • μTorrent

  5. Shareware:

     These softwares are made available with the right to redistribute copies, but license fee has to be paid for the unrestricted use, often after a certain period of time.
    Eg:
    • WinRAR
    • PC Write
    • Linux XP

  6. Freeware and Open Source Software (FOSS):

    It is computer software that can be classified as both free software and open-source software. Anyone is freely licensed to use, copy, study, and change the software in any way, and the source code is openly shared so that people are encouraged to voluntarily improve the design of the software.

Friday, August 11, 2017

ASCII Values and Equivalent Characters

Program to print all ASCII values and equivalent characters.


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

void main()
{
      int i=0;
      cout<<"List of all ASCII values and equivalent characters\n";
      while(i<256)
      {
          cout<<i<<"  ->  "<<(char)i<<endl;
          i++;
         }
    getch();

   }



A complete table of ASCII values and its equivalent characters is given below for reference.




ASCII Table 1
ASCII values and equivalent characters

ASCII Table 2
Extended ASCII values and equivalent characters

Perfect Number

Program to check given input is a perfect number or not.


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

void main()
{
      int num,sum=0,i;
      cout<<"Enter a number to check perfect or not : ";
      cin>>num;
      for(i=1;i<num;i++)
      {
          if(num%i==0)
            { sum+=i; }
         }
      if(num==sum)
      { cout<<endl<<num<<" is a perfect number."; }
      else
      { cout<<endl<<num<<" is not a perfect number."; }
    getch();

   }



OR


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

bool perfect(int a)
{
    int sum=0;
    for(int i=1;i<a;i++)
      {
          if(a%i==0)
            {
                sum+=i;
               }
         }
      if(sum==a)
      {return(1);}
      else
      {return(0);}
   }


void main()
{
    int num;
      cout<<"Enter a number to check is it Perfect or not :";
      cin>>num;
      if(perfect(num)==1)
      {
          cout<<"\nEntered Number is a Perfect Number";
         }
      else
      {
          cout<<"\nEntered Number is not a Perfect Number";
         }
      getch();
   }

Number to Number Name

Program to accept a number as input and print number in words as output. 

(The program has limit from 1 to 9999, which can be edited and increased)

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

void main()
{
      char num_name['z'];
    int num,temp;
      cout<<"Enter the number : ";
      cin>>num;
      strcpy(num_name,"Number Name : ");
if(num>=100)
      {
          temp=num/1000;
            switch(temp)
            {
                case 1: strcat(num_name," One Thousand");break;
                case 2: strcat(num_name," Two Thousand");break;
                case 3: strcat(num_name," Three Thousand");break;
                case 4: strcat(num_name," Four Thousand");break;
                case 5: strcat(num_name," Five Thousand");break;
                case 6: strcat(num_name," Six Thousand");break;
                case 7: strcat(num_name," Seven Thousand");break;
                case 8: strcat(num_name," Eight Thousand");break;
                case 9: strcat(num_name," Nine Thousand");break;
               }
            num%=1000;
         }
if(num>=100)
        {
          temp=num/100;
             switch(temp)
              {
                 case 1: strcat(num_name," One Hundred");break;
                 case 2: strcat(num_name," Two Hundred");break;
                 case 3: strcat(num_name," Three Hundred");break;
                 case 4: strcat(num_name," Four Hundred");break;
                 case 5: strcat(num_name," Five Hundred");break;
                 case 6: strcat(num_name," Six Hundred");break;
                 case 7: strcat(num_name," Seven Hundred");break;
                 case 8: strcat(num_name," Eight Hundred");break;
                 case 9: strcat(num_name," Nine Hundred");break;
                }
             num%=100;
          }
      if(num>=20)
      {
          temp=num/10;
            switch(temp)
            {
                case 2: strcat(num_name," Twenty");break;
                  case 3: strcat(num_name," Thirty");break;
                  case 4: strcat(num_name," Forty");break;
                  case 5: strcat(num_name," Fifty");break;
                  case 6: strcat(num_name," Sixty");break;
                  case 7: strcat(num_name," Seventy");break;
                  case 8: strcat(num_name," Eighty");break;
                  case 9: strcat(num_name," Ninety");break;
               }
            num%=10;
         }
      if(num>=10)
      {
          temp=num%10;
            switch(temp)
            {
                case 1: strcat(num_name," Eleven");break;
                case 2: strcat(num_name," Twelve");break;
                  case 3: strcat(num_name," Thirteen");break;
                  case 4: strcat(num_name," Forteen");break;
                  case 5: strcat(num_name," Fifteen");break;
                  case 6: strcat(num_name," Sixteen");break;
                  case 7: strcat(num_name," Seventeen");break;
                  case 8: strcat(num_name," Eighteen");break;
                  case 9: strcat(num_name," Nineteen");break;
               }
             goto show;
         }
      if(num<10)
      {
          temp=num/1;
            switch(temp)
            {
                case 1: strcat(num_name," One");break;
                case 2: strcat(num_name," Two");break;
                  case 3: strcat(num_name," Three");break;
                  case 4: strcat(num_name," Four");break;
                  case 5: strcat(num_name," Five");break;
                  case 6: strcat(num_name," Six");break;
                  case 7: strcat(num_name," Seven");break;
                  case 8: strcat(num_name," Eight");break;
                  case 9: strcat(num_name," Nine");break;
               }
            num%=1;
         }
      show:
      cout<<num_name;
    getch();

   }

Finding LCM and GCD (HCF)

Menu driven program to obtain LCM or HCF of a given number of inputs.


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


void lcm()
{
    int i,j,n,lcm,lo,hi=1,arg['z'];     //lo= loop operator
      cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
      cin>>n;
      cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
      for(i=0;i<n;++i)
        {
            cin>>arg[i];//take input of all numbers
         }
      for(i=0;i<n;++i)
        {
            hi=hi*arg[i]; //find the product of all the inputs (highest possibility for LCM)
         }
      for(i=1,lo=arg[0];i<n;++i)
        {
        if(lo<arg[i])
        {
                lo=arg[i];//to find the lowest possibility for LCM
               }
         }
      for(lo;lo<=hi;++lo)
         {
          for(j=0;j<n;++j)
          {
      if(lo%arg[j]!=0) //to check divisibility of each number starting form lowest possibility to highest to confirm LCM
        {
        break;  //even a single no. is indivisible process stops and restarts
          }
               }
    if(j==n)   // complete cyle of j loop means the no. is divisible by all hence LCM.
        {
        cout<<"The LCM is : "<<lo;
            getch();
            exit(0);
        }
      }
 }

/*----------------------------------------------------*/


void gcd()
{
      int i,j,n,gcd,hi,arg['z'];     //lo= loop operator
      cout<<"\nEnter No. Terms of Which LCM is Desired....(2 or more):\n";
      cin>>n;
      cout<<"\nEnter the Nos. of Which LCM is to be Found:\n";
      for(i=0;i<n;++i)
        {
            cin>>arg[i];//take input of all numbers
         }
      for(i=1,hi=arg[0];i<n;++i)
        {
        if(hi>arg[i])
        {
           hi=arg[i];//to find the highest possibility for GCD
           }
         }
      for(i=hi;i>=hi;i--)
        {
           for(j=0;j<n;j++)
              {
                if(arg[j]%i!=0)
                  {
                      break;
                   }
              }
           if(j==n)
              {
                cout<<"The HCF is : "<<i;
                getch();
                exit(0);
              }
         }
   }
/*----------------------------------------------------*/


void main()
{
    int opt;
      menu:
      clrscr();
      cout<<"Choose from one of the option:-\n1.Find LCM <Press 1>\n2.Find GCD <Press 2>\n";
      cin>>opt;
      switch(opt)
      {
          case 1: lcm();break;
            case 2: gcd();break;
          default: goto menu;
         }
      getch();

   }