Wednesday, July 11, 2012

A program to print febonacci series upto first 20 trems

#include <stdio.h>
#include <conio.h>

int main()
{
    int a = 0;
    int b = 1;
    int i,sum;
    
    for(i = 0;i<20;i++)
    {
        sum = a+b;
        printf("%d\n",sum);
         
        a = b;
        b = sum;
    }
}

A program to print the FIBONACCI series


#include<iostream>
using namespace std;

main()
{
   int n, c, first = 0, second = 1, next;

   cout << "Enter the number of terms of Fibonacci series you want" << endl;
   cin >> n;

   cout << "First " << n << " terms of Fibonacci series are :- " << endl;

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;


      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout << next << endl;
   }
}

Monday, July 2, 2012

Relation between class_Size and inheritance

#include <iostream>
using namespace std;

class Base
{
    int first_int;           //size of int is 4 byte
    int second_int;       //so size of Base class is 8 byte
};

class derived:public Base
{
    int derived_member;
    int another_derived_member;
    int new_member;
};

class home
{
    Base base_1;
    Base base_2;
};

int main()
{
    cout << sizeof(derived) << endl;     //the size of derived class is (8+12=20) byte
    cout << sizeof(Base)    << endl;
    cout << sizeof(home)   << endl;      //the size of home class is (8+8)=16 byte
}


OUTPUT:
20
8
16