Tuesday, 21 March 2017

Insertion Sort in C++.

C++ CODE
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int a[10], i, j, k, temp;
    cout<<"enter the elements\n";
    for (i = 0; i < 10; i++)
    {
        cin>>a[i];
    }
    for (i = 1; i < 10; i++)
    {
        for (j = i; j >= 1; j--)
        {
            if (a[j] < a[j-1])
            {
                temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
            else
                break;
        }
    }
    cout<<"sorted array\n"<<endl;
    for (k = 0; k < 10; k++)
    {
cout<<a[k]<<" ";
    }
    getch();

}

Selection Sorting in C++.

C++ Code
#include <iostream>
using namespace std;

void print (int [], int);
void selection_sort (int [], int);

//Driver Function
int main ()
{
  int ar[] = {10, 2, 45, 18, 16, 30, 29, 1, 1, 100};
  cout << "Array initially : ";
  print (ar, 10);
  selection_sort (ar, 10);
  cout << "Array after selection sort : ";
  print (ar, 10);
  return 0;
}

// Selection Sort
void selection_sort (int ar[], int size)
{
  int min_ele_loc;
  for (int i = 0; i < 9; ++i)
  {
    //Find minimum element in the unsorted array
    //Assume it's the first element
    min_ele_loc = i;

    //Loop through the array to find it
    for (int j = i + 1; j < 10; ++j)
    {
      if (ar[j] < ar[min_ele_loc])
      {
        //Found new minimum position, if present
        min_ele_loc = j;
      }
    }

    //Swap the values
    swap (ar[i], ar[min_ele_loc]);
  }
}

//Print the array
void print (int temp_ar[], int size)
{
  for (int i = 0; i < size; ++i)
  {
    cout << temp_ar[i] << " ";
  }
  cout << endl;
}

If you want to take values from the user then....!

int main ()
{
  int n=10;
  int ar[n];
  for (int i = 0,j=1; i < n,j<=10; i++,j++)
  {
  cout<<"Enter your number "<<j<<"=";
    cin>>ar[i];
  }
  cout << "Array initially : ";
  print (ar, n);
  selection_sort (ar, n);
  cout << "Array after selection sort : ";
  print (ar, n);
  return 0;
}

Bubble Sorting in C++.

C++ CODE


#include <iostream>
using namespace std;

void bubble_sort (int arr[], int n)
 {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - i - 1;j++)
      if (arr[j] > arr[j + 1])
     {
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
  }

int main()
{
  int n = 10;
  int input_ar[n] = {10, 50, 21, 2, 6, 66, 802, 75, 24, 170};
  bubble_sort (input_ar, n);
  cout << "Sorted Array : " << endl; 
  for (int i = 0; i < n; i++)
    cout << input_ar[i] << " ";
  return 0;
}

If you want to take values from the user then......!

int main()
{
  int n = 10;
  int input_ar[n];
  for(int i=0,j=1;i<10,j<=10;i++,j++)
  {
  cout<<"Enter your number "<<j<<"=";
  cin>>input_ar[i];
  }
  bubble_sort (input_ar, n);
  cout << "Sorted Array : " << endl; 
  for (int i = 0; i < n; i++)
    cout << input_ar[i] << " ";
  return 0;
}

Wednesday, 15 June 2016

A Program on Cafe billing.

C++ Code





#include<fstream>
#include<iostream>
using namespace std;
int starter ();
int continental ();
int chinese ();
int coffee ();
int desert ();
  
  


  int main ()

{
//***Variabel Declaration***//
int optp=1, item, bill=0,total=0;
 string  username ;
 string password ;

cout<<"Enter your admin details \n\nUser Name : ";
cin>>username;
cout<<"Password : ";
cin>>password;

if(username == "admin" ||  password=="*******")
{

while (optp!=6)
{
bill=bill+total;


cout<<"\n\n\nWelcome to Friends Cafe\n\n\n\t\t*********************MENU**********************\n\nCategories"; 
cout<<"\nPress:\n 1.     Starters\n 2.   Continental\n 3. Chinese\n 4. Coffee & Tea\n 5. Deserts\n 6. Exit \n\n" ;
cin>>optp;

if (optp==1  )
total=starter ();




if (optp==2)
  total=continental ();

if(optp==3)
total=chinese ();


if(optp==4)
total=coffee ();


if (optp==5)
total=desert ();

    
if (optp==6)
break;
else 
cout<<"Wrong Selection, Please select again\n";

}


//************asking details of customer*****************//
//if (bill!=0)
{



char name[100]={0};
string name1;
cout<<"\n\nKindly enter customer's Name & ID = ";
cin>>name1; 

//converting string into char array


for (int i=0 ; i<name1.length()  ; i++ )
{

name[i]=name1[i];

}
//********************************



//********Customer Matching from previous database**********//

int check=0,i=0;
char arr[100];
char  counter;

ifstream b;
b.open("imp_file",ios::in );

while(!b.eof())
{

b>>counter;

arr[i]=counter;

if (counter=='.')
{

for(int k=0; k<name1.length(); k++)
{

        if (arr[k]==name[k])

check=1;


else
{
check=0;
break;
}
}
i=-1;
}

if (check==1)
{
cout<<"\n-> Customer matches our database";
break;
}

i++;
}
b.close();
//******************************************


if (check==0)
{

//****Putting Customer data into file******
ofstream a;
a.open("imp_file",ios::app );
a<<name1<<".\n";
a.close();
//*****************************************
}


//*****Billing***********
float net =bill+ bill*0.16 ;
float discount= net - net*0.10;

if (check==1)
{
cout<<"\n\n\n\n\t\t\t-------------------------------\n\t\t\tYour bill is   "<<bill<<"\n\t\t\t16% GST included "<<bill*0.16;
cout<<"\n\t\t\t_______________________________\n\t\t\tTotal Bill = "<<net<<"\n\t\t\t10% Old Customer Discount "<<net*0.10<<"\n\t\t\t_______________________________";
cout<<"\n\t\t\tDiscounted bill is "<<discount<<"\n\n\t\t\t-------------------------------\n\n";
}
else
{

cout<<"\n\n\n\n\t\t\t------------------------\n\t\t\tYour bill is   "<<bill<<"\n\t\t\t16% GST included "<<bill*0.16;
cout<<"\n\t\t\t________________________\n\t\t\tTotal Bill = "<<net<<"\n\t\t\t________________________";
}

}

cout<<"\n\n\n                     \"THANKYOU FOR VISITING |FRIENDS CAFE|\"\n\n\n                       ";

}
else 
cout<<"\n incorrect password ";

return 0;
}







int starter (){

int item;
cout<<"\nPress:\n\n";
cout<<" 1.  Pineapple Juice       Rs-/120\n";
cout<<" 2.  Garlic Bread (4 pcs)  Rs-/250\n";
cout<<" 3.  Crunches              Rs-/100\n";
cout<<" 4.  Finger Fish           Rs-/150\n";
cin>>item;
if(item==1)
return 120;

if(item==2)
return 250;

if(item==3)
return 100;

if(item==4)
return 150;
        }

int continental ()
{
int item;
cout<<"\nPress:\n\n";
cout<<" 1.  Chicken Karahi          Rs-/300\n";
cout<<" 2.  Mutton Karahi           Rs-/600\n";
cout<<" 3.  Chicken Sajji           Rs-/550\n";
cout<<" 4.  Cheese Kabab            Rs-/400\n";
cin>>item;
if(item==1)
return 300;

if(item==2)
return 600;

if(item==3)
return 550;

if(item==4)
return 400;

        }


int chinese ()
{
int item;
cout<<"\nPress:\n\n";
cout<<" 1.  Chicken Manchurian           Rs-/700\n";
cout<<" 2.  Chicken Chouwmin             Rs-/550\n";
cout<<" 3.  Chicken Jalfrazi             Rs-/500\n";
cout<<" 4.  Pasta                        Rs-/350\n";
cin>>item;

if(item==1)
  return 700;

if(item==2)
return 550;

if(item==3)
return 500;

if(item==4)
return 350;


}



int coffee ()
{
int item;
cout<<"\nPress:\n\n";
cout<<" 1.  Coffee Latte               Rs-/250\n";
cout<<" 2.  Cappacinno                 Rs-/200\n";
cout<<" 3.  Espresso                   Rs-/300\n";
cout<<" 4.  Tea                        Rs-/120\n";
cin>>item;

if(item==1)
  return 250;

if(item==2)
return 200;

if(item==3)
return 300;

if(item==4)
return 120;


}
int desert ()
{
int item;
cout<<"/nPress:\n\n";
cout<<" 1.  Molten Lava    Rs-/270\n";
cout<<" 2.  Choco Pie      Rs-/130\n";
cout<<" 3.  Creamy Donut   Rs-/120\n";
cout<<" 4.  Tutti Frutti   Rs-/350\n";
cin>>item;
if(item==1)
return 270;

if(item==2)
return 130;

            if(item==3)
return 120;

if(item==4)
return 350;


}


Write a C++ Program to check Armstrong number.

C++ Code



#include<iostream>
using namespace std;
int main()
{
int num,s,d,n,rem=0;
        cout<<"Enter three digit num=";
cin>>num;
s=num;
    while (s!=0)
    {
    n=s%10;
    rem=rem+n*n*n;
    s=s/10;
}
if(rem==num)
{
cout<<"It is an armstrong number";
}
else
cout<<"It is not an armstrong number";
}

Fibonacci Series.

C++ Code



#include<iostream>
using namespace std;
int main()
{
int a=0,b=1,r,i,c;
cout<<"Enter range=";
cin>>r;

if (r>0)
{
cout<<a<<" "<<b<<" ";
}

    i=3;
    while (i<=r)
{
    c=a+b;
    cout<<c<<" ";
    a=b;
    b=c;
    i++;
}
}

Tuesday, 14 June 2016