Wednesday, May 8, 2013

Comparing 4 number in c++


#include<iostream>
using namespace std;
void main()
{
int n1,n2,n3,n4;
cout<<"enter four number\n";
cin>>n1>>n2>>n3>>n4;

if (n1 >n2 && n1 > n3 && n1 > n4)
cout<<"number 1 is greatest\n";

else if(n1 >n2 && n1 >n3 ||n1 >n3 && n1 >n4||n1 >n2 && n1 >n4 )
cout<<"number 1 is is at second mid\n";

else if(n1 >n2 || n1 >n3 ||n1 >n4 )
cout<<"number 1 is is at first mid\n";

else
cout<<"number 1 is is least\n";



if (n2 >n1 && n2 > n3 && n2 > n4)
cout<<"number 2 is greatest\n";

else if(n2 >n1 && n2 >n3 ||n2 >n3 && n2 >n4||n2 >n1 && n2 >n4 )
cout<<"number 2 is  at second mid\n";

else if(n2 >n1 || n2 >n3 ||n2 >n4 )
cout<<"number 2 is at first mid\n";

else
cout<<"number 2 is least\n";




if (n3 >n1 && n3 > n2 && n3 > n4)
cout<<"number 3 is greatest\n";

else if(n3 >n1 && n3 >n2 ||n3 >n2 && n3 >n4||n3 >n1 && n3 >n4 )
cout<<"number 3 is at second mid\n";

else if(n3 >n1 || n3 >n2 ||n3 >n4 )
cout<<"number 3 is at first mid\n";

else
cout<<"number 3 is least\n";



if (n4 >n1 && n4 > n2 && n4 > n3)
cout<<"number 4 is greatest\n";

else if(n4 >n1 && n4 >n2 ||n4 >n2 && n4 >n3||n4 >n1 && n4 >n3 )
cout<<"number 4 is at second mid\n";

else if(n4 >n1 || n4 >n2 ||n4 >n3 )
cout<<"number 4 is at first mid\n";

else
cout<<"number 4 is least\n";
}

Wednesday, March 27, 2013

Huge integer multiplication

#include<iostream>
#include<conio.h>
#include <cstdlib>
#include<string>
using namespace std;
void main()
{
int *a,*b,**c,n1,n2,cr=0,z,i=0,j=0,l,s=0,k,m,n=0;
cout<<"Enter large digit\n";
string name,name1;
cin>>name;

n1=name.length();
a=new int[n1];

for(i=0;i<n1;i++)
    {
        a[i]=((int)name[i]-48) ;
    }

cout<<"Enter large digit\n";
cin>>name1;
n2=name1.length();
b=new int [n2];
for(i=0;i<n2;i++)
    {
        b[i]=((int)name1[i]-48);
    }
m=k=l=n1+n2;
if(n1>n2)
{
c=new int *[n1];
for(i=0;i<n1;i++)
c[i]=new int[k];
for(i=0;i<n1;i++)
{
for(j=0;j<k;j++)
c[i][j]=0;
}
}
else
{
    c=new int *[n2];
for(i=0;i<n2;i++)
c[i]=new int[k];
for(i=0;i<n2;i++)
{
for(j=0;j<k;j++)
c[i][j]=0;
}
}
for(j=n2-1;j>=0;j--)
{
k=--l;
for(i=n1-1;i>=0;i--)
{
if(i!=0)
{
z=(a[i]*b[j])+cr;
c[n][k]=z%10;
cr=z/10;
}
else
{
z=(a[i]*b[j])+cr;
c[n][k]=z%10;
cr=z/10;
c[n][k-1]=cr;
cr=0;
}
k--;
}
n++;
}

k=0;
cout<<"multiplication of number is\n";
cr=0;
int *d;
k=m;
d=new int[k];
for(i=0;i<m;i++)
d[i]=0;
cr=0;
for(j=m-1;j>=0;j--)
{
z=0;
for(i=n1-1;i>=0;i--)
{
z=z+c[i][j];
}
if(j!=m-1)
{
   z=z+cr;
d[k-1]=z%10;
cr=z/10;
}
else
{
z=z+cr;
d[k-1]=z%10;
cr=z/10;
d[k-2]=cr/10;
cr=0;
}
k--;
}
for(i=0;i<m;i++)
cout<<d[i];
cout<<endl;
getch();
}

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;
}