Thursday 22 March 2012

Print Rational Number


#include <iostream>
using namespace std;
class rational
{
private:
int num;
int denum;
public:
void get()
{
cout<<"enter numerator: "<<endl;
cin>>num;
cout<<"enter denomirator: "<<endl;
cin>>denum;
}
void print()
{
cout<<"you Entered: "<<endl;
cout<<num<<"/"<<denum<<endl;
cout<<endl;
}
};

void main()

{
rational r1;
r1.get();
r1.print();
}

Print time in hours, minutes and seconds


#include<iostream>
using namespace std;
class time
{
private:
 int hour;
 int mint;
 int sec;
public:
 void get()
 {
cout<<"enter hours: "<<endl;
    cin>>hour;
cout<<"enter minutes: "<<endl;
    cin>>mint;
cout<<"enter seconds: "<<endl;
    cin>>sec;
 }
 void print()
 {
cout<<"you entered: "<<endl;
cout<<hour<<":"<<mint<<":"<<sec<<endl;
 }
};

void main()
{
 time t1;
 t1.get();
 t1.print();
}

Print distance in feets and inches


#include<iostream>
using namespace std;
class dist
{
private:
 int feet;
 float inch;
public:
 void get()
 {
  cout<<"enter feets: "<<endl;
  cin>>feet;
  cout<<"enter inches: "<<endl;
  cin>>inch;
 }
 void print()
 {
cout<<"you entered: "<<endl;
cout<<"feets = "<<feet<<endl;
cout<<"and"<<endl;
cout<<"inches = "<<inch;
     cout<<endl;
 }
};


void main()
{
 dist d1;
 d1.get();
 d1.print();
}

Monday 19 March 2012

Addition of real and imaginary part


#include<iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
void set(int a, int b)
{
real=a;
img=b;
}
void get()
{
cout<<"enter real number: "<<endl;
cin>>real;
cout<<"enter imaginary number: "<<endl;
cin>>img;
}
void print()
{
cout<<"you entered:"<<real<<"+"<<img<<"i"<<endl;
cout<<"addition of real and imaginary part is: "<<real+img;
cout<<endl;
}
};

void main()
{
complex C1,C2;
C1.get();
C2.set(10,20);
C1.print();
}

subtraction of two numbers (basic syntax)


#include<iostream>
using namespace std;
class subtract
{
private:
int number1;
int number2;
public:
void set(int a, int b)
{
number1=a;
number2=b;
}
void get()
{
cout<<"enter first number: "<<endl;
cin>>number1;
cout<<"enter second number: "<<endl;
cin>>number2;
}
void print()
{
cout<<"subtraction of two numbers is: "<<number1-number2;
cout<<endl;
}
};

void main()
{
subtract s1,s2;
s1.get();
s2.set(20,10);
s1.print();
}

Sunday 18 March 2012

Factorial Of Number using Recursion


# include <iostream>
using namespace std;
int factorial(int n);
int main()
{
for(int i=1; i<=10; i++)
cout<<i<<"!= "<<factorial(i);
cout<<endl;


return 0;
}


int factorial(int n)
{
if (n==1)
return 1;
else
return n*factorial(n-1);
}

Christmas Song using Switch structure


#include <iostream>
using namespace std;

int main()
{
   for ( int day = 1; day < 13; day++ ) {
      cout << "On the ";
      switch ( day ) {       // switch for current day
         case 1:
            cout << "first";
            break;
         case 2:
            cout << "second";
            break;
         case 3:
            cout << "third";
            break;
         case 4:
            cout << "fourth";
            break;
         case 5:
            cout << "fifth";
            break;
         case 6:
            cout << "sixth";
            break;
         case 7:
            cout << "seventh";
            break;
         case 8:
            cout << "eighth";
            break;
         case 9:
            cout << "nineth";
            break;
         case 10:
            cout << "tenth";
            break;
         case 11:
            cout << "eleventh";
            break;
         case 12:
            cout << "twelfth";
                        break;
      }
      cout << " day of Christmas,\nMy true love sent to me:\n";
      switch ( day ) {     // switch for gifts
         case 12:
            cout << "\tTwelve drummers drumming,\n";
         case 11:
            cout << "\tEleven pipers piping,\n";
         case 10:
            cout << "\tTen lords a-leaping,\n";
         case 9:
            cout << "\tNine ladies dancing,\n";
         case 8:
            cout << "\tEight maids a-milking,\n";
         case 7:
            cout << "\tSeven swans a-swimming,\n";
         case 6:
            cout << "\tSix geese a-laying,\n";
         case 5:
            cout << "\tFive golden rings,\n";
         case 4:
            cout << "\tFour calling birds,\n";
         case 3:
            cout << "\tThree French hens,\n";
         case 2:
            cout << "\tTwo turtle doves, and\n";
         case 1:
            cout << "A partridge in a pear tree.\n\n\n";
      }
   }
   cout << endl;
   return 0;
}