Sunday, November 11, 2012

vending matchine code in c++


#include<iostream>
#include<iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
void main()
{
int coin,pepsi=5,fanta=5,water=5,cock=5,sprite=5,choice,timer=0,counter;
char re;
bool over=0;
    timer = clock();
do

{
timer=0;
counter=0;
do
{
system("cls");system("COLOR 2a");
cout<<setw(30)<<"\n\nInsert coin of 5 in Vender Matchine"<<endl;
cin>>coin;
if(coin!=5)
cout<<setw(30)<<"pleas insert 5 ruppes coin only\n";
}
while(coin!=5);

cout<<setw(40)<<"__________________________________________________\n";
cout<<setw(40)<<"|***************--Select your drink--**************|\n";
cout<<setw(34)<<"|Choice NO        Drink            Bottles|        |\n";
cout<<setw(40)<<"|__________________________________________________|\n";
cout<<setw(40)<<"|--------------------------------------------------|"<<endl;
cout<<setw(30)<<"|1:>            Pepsi Bottle        :   "<<pepsi<<setw(11)<<"|"<<endl;
cout<<setw(40)<<"|--------------------------------------------------|"<<endl;
cout<<setw(30)<<"|2:>            Fanta Bottle        :   "<<fanta<<setw(11)<<"|"<<endl;
cout<<setw(40)<<"|--------------------------------------------------"<<"|"<<endl;
cout<<setw(30)<<"|3:>            Sprite Bottle       : "<<sprite<<setw(13)<<"|"<<endl;
cout<<setw(40)<<"|--------------------------------------------------"<<"|"<<endl;
cout<<setw(30)<<"|4:>            Water Bottle        : "<<water<<setw(13)<<"|"<<endl;
cout<<setw(40)<<"|--------------------------------------------------"<<"|"<<endl;
cout<<setw(30)<<"|5:>            Cock Bottle         : "<<cock<<setw(13)<<"|"<<endl;
cout<<setw(40)<<"|--------------------------------------------------|"<<endl;
cout<<setw(40)<<"|__________________________________________________|\n";
//do
//{
cin>>choice;
timer = clock();
if((timer/1000)>30)
{
cout<<setw(30)<<"your time is over 20 second please pick up your coine"<<endl;
coin=0;
}
else
{
if(counter>2)
{
cout<<setw(30)<<"you press invelid key at  two time please pick up your coine\n";
    
coin=0;
break;
}
switch(choice)
{
case 1:
if(pepsi==0)
{
cout<<setw(30)<<"pepsi is not avalibel in stock\n";
cout<<setw(30)<<"enter r if you want return coin\n";
cin>>re;
if(re=='r')
{
cout<<setw(30)<<"pick your coin :"<<5<<"$\n";
coin=0;
}
else
{
cout<<setw(30)<<"Enter your choice for any other bottle\n";
cin>>choice;
}
}
else
{
cout<<setw(30)<<"pick up the pepsi bottle\n";
pepsi--;
}
break;
case 2:
if(fanta==0)
{
cout<<setw(30)<<"fanta is not avalibel in stock\n";
cout<<setw(30)<<"enter r if you want return coin\n";
cin>>re;
if(re=='r')
{
cout<<setw(30)<<"pick your coin :"<<5<<"$\n";
coin=0;
}
else
{
cout<<setw(30)<<"Enter your choice for any other bottle\n";
cin>>choice;
}

}
else
{
cout<<setw(30)<<"pick up the fanta bottle \n";
fanta--;
}
break;
case 3:
if(sprite==0)
{
cout<<setw(30)<<"Sprite is not avalibel in stock\n";
cout<<setw(30)<<"enter r if you want return coin\n";
cin>>re;
if(re=='r')

{
cout<<setw(30)<<"pick your coin :"<<5<<"$\n";
coin=0;
}
else
{
cout<<setw(30)<<"Enter your choice for any other bottle\n";
cin>>choice;

}
}
else
{
cout<<setw(30)<<"pick up the Sprite bottle \n";
sprite--;
}
break;
case 4:
if(re==0)
{
cout<<setw(30)<<"Water buttol is not avalibel in stock\n";
cout<<setw(30)<<"enter r if you want return coin\n";
cin>>re;
if(choice=='r')
{
cout<<setw(30)<<"pick your coin :"<<5<<"$\n";
coin=0;
}
else
{
cout<<setw(30)<<"Enter your choice for any other bottle\n";
cin>>choice;
}
}
else
{
cout<<setw(30)<<"pick up the water bottle \n";
water--;

}
break;
case 5:
if(re==0)
{
cout<<setw(30)<<"Cock is not avalibel in stock\n";

cout<<setw(30)<<"enter r if you want return coin\n";
cin>>re;
if(choice=='r')
{
cout<<setw(30)<<"pick your coin :"<<5<<"$\n";

coin=0;
}
else
{
cout<<setw(30)<<"Enter your choice for any other bottle\n";
cin>>choice;
}
}
else
{
cout<<setw(30)<<"pick up the cock bottle \n";
cock--;
}
break;
default :
{
counter++;
continue;
cout<<setw(30)<<"Invelid selection\n";
}
}
//}while(choice==1 || choice==2 || choice==3 || choice==4 || choice==5);
cout<<setw(30)<<"DO you want turnOff the matchin if yes press y\n";
cin>>re;
if(re=='y' || re=='Y')
break;
}
}while(pepsi!=0 || fanta!=0 || water!=0 || cock!=0 ||sprite!=0);
      system("COLOR 4a");
      
cout<<setw(30)<<"GOOD BYE"<<endl<<endl<<endl;

}

Wednesday, October 17, 2012

Bubble sort of Array in c++


#include<iostream>


using namespace std;

void main()
{
//creat dynamic array
int *ptr,size,value,i,j,tmp;
cout<<"enter size of array\n";
cin>>size;
ptr=new int[size];
//taking input from user
for(i=0;i<size;i++)
{
cout<<"enter value in index "<<i<<endl;
cin>>value;
ptr[i]=value;
}
//start boubble sort
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
if(ptr[j]>ptr[j+1])
{
tmp=ptr[j];
ptr[j]=ptr[j+1];
ptr[j+1]=tmp;
}
}
//output of sorted array
for(i=0;i<size;i++)
cout<<ptr[i]<<"   ";
cout<<endl;
}

Selection sort of Array in c++


#include<iostream>


using namespace std;

void main()
{
 //creat dynamic array
 int *ptr,size,value,i,j,min,tmp;
 cout<<"enter size of array\n";
 cin>>size;
 ptr=new int[size];
 //taking input from user
 for(i=0;i<size;i++)
 {
  cout<<"enter value in index "<<i<<endl;
  cin>>value;
  ptr[i]=value;
 }
 //start selection sort
    for(i=0;i<size-1;i++)
 {
  min=i;
  for(j=i+1;j<size;j++)
  {
   if(ptr[j]<ptr[min])
   min=j;
  }
   if(min!=i)
   {
    tmp=ptr[i];
    ptr[i]=ptr[min];
    ptr[min]=tmp;
   }
 }
 //output of sorted array
 for(i=0;i<size;i++)
  cout<<ptr[i]<<"   ";
 cout<<endl;
}

Monday, September 24, 2012

Insertion sort in c++

#include<iostream.h>

void main()
{
    int i,j,k,tmp;
    //take input from user
    cout<<"Enter size of array\n";
    cin>>k;
    int a[100];
    cout<<"Enter integer in array \n";
    for(i=0;i<k;i++)
    cin>>a[i];
   
    //sorting program start
    for(i=1;i<k;i++)
    {
        tmp=a[i];
        j=i-1;


        while(j>=0)
        {
            if(a[j]<tmp)
                break;
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=tmp;
    }
    //output of sorted array
    for(i=0;i<k;i++)
        cout<<a[i]<<"   ";
    cout<<endl;
}

Thursday, September 13, 2012

Guss game in c++

#include<iostream.h>
#include<time.h>
#include<stdlib.h>
int main ()
{
    int guess , secret , i;
    char ans;
    bool flag;
    do
    {
        flag=true;
        i=0;
        srand(time(0));
        secret=rand()%11;

  
        cout<<secret;
        while(i<3 && flag==true)
    {
    cout<<"Enter your guess from 0 to 10:";
    cin>>guess;

    {
    if(guess>secret)
  
        cout<<"Your guess is too big "<<endl;


    else if (guess<secret)
  
        cout<<"Your guess is too samll  "<<endl;

  
    else
    {
        cout<<"Congratulations !! Your guess is right "<<endl;
        flag=false ;
    }
    }
    i++;
  

    }
      
    cout<<"Do you want to play again [y/n] ";
    cin>>ans ;
    }
    while (ans=='y' || ans=='Y');
    return 0;
}

calculator code in c++ without using + , - , x , / and % operator

#include<iostream.h>
int main ()
{
    char ch;
    do
    {
int a,b,result=0,count=0,num,c=0,d=0;
char op;
do
{
cout<<"\t      \"Enter an operator from following options\" "<<endl;
cout<<"\t\t    Enter '!' to Invert sign"<<endl;
cout<<"\t\t    Enter '+' for Addition"<<endl;
cout<<"\t\t    Enter '-' for Subtraction"<<endl;
cout<<"\t\t    Enter '*' for Multiplication"<<endl;
cout<<"\t\t    Enter '/' for Division"<<endl;
cout<<"\t\t    Enter '%' for Remainder"<<endl;
cout<<endl<<"Please enter your option from list given above : ";
cin>>op;
if(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%')
cout<<endl<<endl<<"\a\"Please enter a valid character\" "<<endl<<endl;
}
while(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%');
switch (op)
{
case '!':
    cout<<endl<<"Enter a number to invert the sign :";
    cin>>a;
    if(a>0)
    {
        while(a)
        {
            result--;
            a--;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    else if (a<0)
    {
        while(a)
        {
            result++;
            a++;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    break ;
case '+':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The sum is "<<a<<endl;
    /*else if (a==0)
        cout<<endl<<"The sum is "<<b<<endl;*/
    if(b>0)
    {
        while (b)
        {
            a++;
            b--;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a--;
            b++;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    break;
case '-':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The difference is "<<a<<endl;
    if( b>0)
    {
        while (b)
        {
            a--;
            b--;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a++;
            b++;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    break;
case '*':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(a>=0 && b>=0)
    {
        for(a;a>0;a--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a<0 && b<0)
    {
        while(a)
        {
            c++;
            a++;
        }
        while(b)
        {
            d++;
            b++;
        }
        for(c;c>0;c--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a>=0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        for(a;a>0;a--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            c--;
        }
        cout<<endl<<"The product is "<<c<<endl;
    }
    else if (a<0 && b>=0)
    {
        while(a)
        {
            c++;
            a++;
        }
        for(c;c>0;c--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            d--;
        }
        cout<<endl<<"The product is "<<d<<endl;
    }
    break;
case '/':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The quotient is 0 "<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
    }
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    else if (a<0 && b>0)
    {
       
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=b;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            d--;
        }
    cout<<endl<<"The quotient is "<<d<<endl;
    }
    else if (a>0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)
        {
        num=d;   
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            c--;
        }
    cout<<endl<<"The quotient is "<<c<<endl;
    }
    else if (a<0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=d;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
       
       
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    break;
case '%':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The remainder is "<<b<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<b)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
    if(a<0 && b>0)
    {
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=b;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<b)
            break;
    }
    while(c)
        {
            d++;
            c++;
        }
        cout<<endl<<"The remainder is "<<d<<endl;
    }
    if(a>0 && b<0)
    {
        while(b)
        {
            c++;
            b++;
        }
    while (a)
    {
        num=c;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<c)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
        if(a<0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=d;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<d)
            break;
    }    while(c)
        {
            result--;
            c--;
        }
        cout<<endl<<"The remainder is "<<result<<endl;
    }
    break;
}
cout<<endl<<"Do you want to calculate again [y/n] ";
cin>>ch;
}
while(ch=='y' || ch=='Y');
cout<<endl<<endl<<"Take Care ... Goodbye :)"<<endl;
return 0;
}