You are on page 1of 7

// 1.Demonstrates recursion the nth Fibonacci number #include <iostream.h> #include <conio.

h> int fib(int n); int main() { int n, answer; cout << "Enter number to find: "; cin >> n; cout << "\n\n"; answer = fib; cout << answer << " is the " << n << "th Fibonacci number\n"; getch(); return 0; } int fib (int n) { if (n < 3 ) { return (1); } else { return( fib(n-2) + fib(n-1)); } }

/* 2 Program to find x power y using recursive function */ #include<iostream.h> #include<conio.h> int power(int x, int y); int main() { int r, b,i; cout<<"\nEnter base and index "; cin>>b>>i; r=power(b,i); cout<<b<<" to the power "<<i<<" = "<<r; getch(); return 0; } int power(int x, int y) { int res; if(y==0) return 1; res= x*power(x,y-1); return res; }

/* 3. classes and objects - Problem 2 bank account */ #include<iostream.h> #include<conio.h> #include<string.h> class Account { char name[20]; int accNo; char accType[10]; float bal; public: void setData(char *n, int aNo,char *aType, float b); void deposit(int amt); void withdraw(int amt); void display(); /* Note - here not a single function is defined inside the class hence, there is no inline function */ }; void Account::setData(char *n,int aNo,char *aType, float b) { strcpy(name,n); accNo=aNo; strcpy(accType,aType); bal=b; } void Account::deposit(int amt) { bal = bal + amt; cout<<"\nAmount Deposited"; } void Account::withdraw(int amt) { if( bal < 500 || amt > bal ) cout<<"\n Insufficient Fund , can't complete the transaction"; else

{ bal = bal - amt ; cout<<"\nTransaction Successful"; } } void Account::display() { cout<<"\n\tName\tAccNo\tAcc Type\tBalance "; cout<<"\n\t"<<name<<"\t"<<accNo<<"\t"<<accType<<"\t"<<bal; } int main() { Account a; char nm[20],at[10]; int aNo , ch,am; float bl; cout<<"\nCreate New Account"; cout<<"\nEnter Account Details"; cout<<"\nName : "; cin>>nm; cout<<"\nAcc number"; cin>>aNo; cout<<"\nAcc Type"; cin>>at; cout<<"\nEnter Opening Balance "; cin>>bl; a.setData(nm,aNo,at,bl); cout<<"\nSelect Transaction"; cout<<"\n1. Deposit"; cout<<"\n2. Withdraw"; cout<<"\n3. Display"; cout<<"\n4. Exit"; cin>>ch; switch (ch) { case 1: cout<<"\n Enter amount to be deposited"; cin>>am; a.deposit(am); break;

case 2: cout<<"\n Enter amount to be withdrawn"; cin>>am; a.withdraw(am); break; case 3: a.display(); break; } getch(); return 0; }

/*4. Program to demonstrate copy constructor */ #include<iostream.h> #include<conio.h> class Car { float enggSize; char style; int colorCode; public: Car() { enggSize = 0.0; style = 'X'; colorCode = 0; } Car(float es,char s, int color); Car(Car &c); void display(); }; Car::Car(float es,char s, int color) { enggSize = es; style = s;

colorCode = color; } Car::Car(Car &c) { enggSize = c.enggSize; style = c.style; colorCode = c.colorCode; } void Car::display() { cout<<"\nEngine Size :\t"<<enggSize; cout<<"\nStyle :\t"<<style; cout<<"\nColor Code :\t"<<colorCode; } int main() { float s; char stl; int code; cout<<"\nEngine size : "; cin>>s; cout<<"\nStyle"; cin>>stl; cout<<"Color Code"; cin>>code; Car c1; cout<<"\nDetails of car 1 created with default constructor"; c1.display(); Car c2(s,stl,code); cout<<"\nDetails of car 2 created with parameterized constructor"; c2.display(); Car c3(c2); cout<<"\nDetails of car 3 created using copy constructor"; c3.display(); Car c4 = c2; cout<<"\nDetails of car 3 created using copy constructor with assignment operator"; c4.display();

getch(); return 0; }

You might also like