30 September, 2010

FIBONACCI SERIES IN C

#include<iostream.h>
#include<conio.h>
main()
{
const unsigned long limit=4294967295;
unsigned long next=0;
unsigned long last=1;
long sum;
clrscr();
cout<<”\n\nThis program will print the Fibonacci series :\n\n “;
while(next<limit/2)
{
cout<<last <<” “;
sum=next+last;
next=last;
last=sum;
}
getch();
}

29 September, 2010

FIBONACCI SERIES

#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf(“\n Enter n for how many times generate series”);
scanf(“%d”,&n);
printf(“\n FIBONACCI SERIES \n”);
printf(“\t%d\t%d”,a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf(“\t%d”,c);
}
getch();
};

06 August, 2010

ADD WITHOUT ADD OPERATOR

 #include<stdio.h>
int main()
{
int a,b;
int sum;
printf(“Enter any two integers: “);
scanf(“%d%d”,&a,&b);
//sum = a – (-b);
sum = a – ~b -1;
printf(“Sum of two integers:%d”,sum);
return 0;
}

05 August, 2010

ADD COMPLEX NUMBERS

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct complex
{
int real;
int img;
};
int main()
{
struct complex a, b, c;
printf(” Enter a and b where a + ib is the first complex number. “);
printf(“\n a = “);
scanf(” %d “, &a.real);
printf(” b = “);
scanf(” %d “, &a.img);
printf(” Enter c and d where c + id is the second complex number. “);
printf(“\n c = “);
scanf(” %d “, &b.real);
printf(” d = “);
scanf(” %d “, &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf(” Sum of two complex numbers = %d + %di “, c.real, c.img);
else
printf(” Sum of two complex numbers = %d %di “, c.real, c.img);
getch();
return 0;
}

04 August, 2010

ACCESSING STATIC MEMBERS WITHOUT AN OBJECT

#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count–; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int count;
private:
int itsAge;
};
int Cat::count = 0;
void TelepathicFunction();
int main()
{
const int MaxCats = 5; int i;
Cat *CatHouse[MaxCats];
for (i = 0; i< MaxCats; i++)
{
CatHouse[i] = new Cat(i);
TelepathicFunction();
}
for ( i = 0; i< MaxCats; i++)
{
delete CatHouse[i];
TelepathicFunction();
}
return 0;
}
void TelepathicFunction()
{
cout << “There are “;
cout << Cat::count << ” cats alive!\n”;
}

03 August, 2010

ACCESSING DATA IN A FILE

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
template <class T>
void print(T & c)
{
for( typename T::iterator i = c.begin(); i != c.end(); i++ )
{
std::cout << *i << endl;
}
}
int main()
{
vector <int> output_data( 10 );
generate( output_data.begin(), output_data.end(), rand );
transform( output_data.begin(), output_data.end(),output_data.begin(), bind2nd( modulus<int>(), 10 ) );
ofstream out( “data.txt” );
if( !out )
{
cout << “Couldn’t open output file\n”;
return 0;
}
copy( output_data.begin(), output_data.end(),ostream_iterator<int>( out, “\n” ) );
out.close();
ifstream in( “data.txt” );
if( !in )
{
cout << “Couldn’t open input file\n”;
return 0;
}
vector<int> input_data( (istream_iterator<int>( in )),istream_iterator<int>() );
in.close();
print( output_data );
print( input_data );
}