Friday, May 29, 2009

Fibonacci numbers

#include "iostream.h"
#include "conio.h"

// sequence is 0, 1, 1, 2, 3, 5, 8, 13, ...

int fib (int i) {
int pred, result, temp;

pred = 1;
result = 0;

while (i > 0) {
temp = pred + result;
result = pred;
pred = temp;
i = i-1;
}
return(result);
}

void main () {
int n;
cout << "Enter a natural number: ";
cin >> n;
while (n < 0) {
cout << "Please re-enter: ";
cin >> n;
}
cout << "fib(" << n << ") = " << fib(n) << endl;

}

Friday, May 15, 2009

Sum of Digits

To display the Sum of Digits

header files
void main()
{int n;
int n1, sum=0;
cout<<"Enter the Number of N: ";
cin>> n;
while(n>0)
{
n1=n%10;
sum=sum+n1;
n=n/10;
}
cout<< sum;getch();
}
Output will be
Enter the Number of N: 258
15

Friday, May 8, 2009

CONSTRUCTOR - Default Constructor

header files

class num
{
int a,b;
public:
num() // default constructor
{
a=100;
b=200;
}
void display()

};

void num::display()
{
cout<<>
cout<<>
}
void main()
{
num obj;
obj.display();
}

Output will be

100
200

Parameterized Constructor

header files

class num
{
int a,b;
public:
num(int x, int y) // Parameterized constructor
{
a=x;
b=y;
}
void display()

};

void num::display()
{
cout<<>
cout<<>
}
void main()
{
num obj(1000,5000);
obj.display();
}

Output will be

1000
5000

Constructor Overloading

header files

class num
{
int a,b;
float c,d;
char s[5], s1[10];
public:
num(int x, int y) // constructor overloading
{
a=x;
b=y;
}
num(char str[10], char str1[15]) // constructor overloading
{
strcpy(s,s1);
strcpy(s1,str1);
}
num(float p) // constructor overloading
{
c=p;
d=555;

}
void display()

};

void num::display()
{
cout<<>
cout<<>
cout<<>
}
void main()
{
num obj(1000,5000);
num obj("Kumar","Dharsinisamy");
num obj(150.60);
obj.display();
}

Output will be

1000
5000
Kumar
Dharsinisamy
100.50
555

Copy Constructor

class copy
{
int a,b,c;
public:
copy(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
copy(copy & ob) // copy constructor declaration
{
a=ob.a;
b=ob.b;
c=ob.c;
}
void display()
{
cout << a << endl << c << endl;
}
};

void main()
{
copy ob1(5,10,20);
copy pb2(ob1); // object as an argument
clrscr();
ob1.display();
ob2.display();
getch();
}

output will be
5
10
20
5
10
20

Friday, May 1, 2009

Function Overloading - Program

header files

include

iostrem.h
conio.h
string.h

class swap
{
int a,b,c;
float x,y,z;
char p[10];
char ch,ch1;
public:
swap()
{
a=20;
b=40
}
swap(int x, int y)
{
a=x;
b=y;
}
swap(float a, float b, int aa)
{
x=a;
y=b;
c=aa;
}
swap(char s[10])
{
strcpy(p,s);
}
void display1()
{
cout<< a <<"\n" << b <<"\n";
}
void display1()
{
cout<< x <<"\n" << y <<"\n"<< c <<"\n";
}
void display1()
{
cout<< p;
}
};
void main()
{
clrscr();
swap obi();
swap obj1(6,15);
swap obj2(2.4,5.6,6);
swap obj3("god");
obj.display1();
obj1.display2();
obj2.display3();
obj3.display1();

}

Outside declaration of the member function

Outside declaration of the member function

header files
class addition // class declaration
{
int a,b,c; // data member declaration
public:
void get() // function declaration
void put()

};
void addition::get()
{
cin>> a >> b;
c=a+b;
}
void addition::get()
{
cin>> a >> b;
c=a+b;
}

void main()
{
clrscr();
addition obj;
obj.get(); // object creation
obj.put(); // object calling
getch();

}

inside declaration of the member function

inside declaration of the member function

header files
class addition // class declaration
{
int a,b,c; // data member declaration
public:
void get()
{
cin>> a >> b;
c=a+b;
}
void put()
{
cout<<"Result is C=" << c;
}
};
void main()
{
clrscr();
addition obj;
obj.get(); // object creation
obj.put(); // object calling
getch();

}