Thursday, March 31, 2011

Operator Overloding sample program in C++

/* Write a function, fn(), to increment the values of two variables of type int that are passed by reference.
*/

# include "stdio.h"

# include "iostream"
using namespace std;

class Reference
{
private:
int nVar1;
int nVar2;

public:
// constructor

Reference()
{
nVar1=0;
nVar2=0;
}

//to display the content
void Assign()
{


printf("%d\t%d\n", nVar1,nVar2);

}

//Operator overloading
void operator++()
{

nVar1++;
nVar2++;
nVar2++;

}


};

int main()
{

Reference Obj;

Obj.Assign();

Obj++;

Obj.Assign();

return 0;

}

OUTPUT:

0 0
1 2
Press any key to continue . . .


Wednesday, March 30, 2011

What is Friend Function in C++?

A non_member function that is allowed access to the private variable f a class is called a FRIEND of the class.

Features of the friend function


1. A friend function can access the private member of a class.

2. Friend functions need not have a "this" pointer, as it does not belong to any object.

3. The friend declaration is unaffected by its location in the class.

4. the definition of a friend function does not require the class name with the scope resolution operator prefixed to it, as it is required for a class member function.


Unix Shell Programming




Let Us C

What is Static Function in C++?

Declaring a member STATIC function restricts its scope and makes it independent of the individual objects of the class. This feature is useful for member functions as for data members. The static data type variable can be manipulated only by the static member of its class without affecting the rest of the program.


1. This function i not having the this pointer.

2. Object.function() is not used as SYNTAX.

3. If we want to access the static function, Then we go for class_Specifier::function();

Tuesday, March 29, 2011

Compare unsinged long int and int in MSVC

# include "stdio.h"
int main()
{
int a;
unsigned long int b=0;
printf("Enter the integer vlue\n");
scanf("%d",&a);
printf("%d\t",a);

//printf("%d\t",b);
b = a;
printf("%u\t\n",b);


scanf("%d",&b);
printf("\n%u\t",b);

//printf("%d\t",a);
a = b;
printf("%d\t",a);



return 0;
}

OUTPUT
Enter the integer value
55555555555555555555555555555555555555555
a= -477218589 b= 3817748707
55555555555555555555555555555555555555555

a= -477218589 b= 3817748707 Press any key to continue . . .

Friday, March 25, 2011

Macro Program

# include "stdio.h"
#define MAX(a, b) a>b ? a:b
int main()
{
int m, n;
m =20+ MAX(2, 3);
n = 2 * MAX(3, 2);
printf("m = %d, n = %d\n", m, n);
return 0;
}

OUTPUT:

m = 2, n = 3
Press any key to continue . . .

Wednesday, March 23, 2011

What is Quine?

/*A program that produces its complete source code as its only output is called a quine.*/

# include "stdio.h"
# include "windows.h"
int main()
{
FILE *fRead;
char ch;
fRead = fopen("\Quine.c","r");
if(fRead == NULL)
MessageBox(0,"Karthik","Please Check the Source ile path",16);

while((ch=fgetc(fRead) )!= NULL)
{
printf("%c",ch);
}

return 0;
}


OUTPUT:

# include "stdio.h"
# include "windows.h"
int main()
{
FILE *fRead;
char ch;
fRead = fopen("\Quine.c","r");
if(fRead == NULL)
MessageBox(0,"Karthik","Please Check the Source ile path",16);

while((ch=fgetc(fRead) )!= NULL)
{
printf("%c",ch);
}

return 0;
}                                                                       
                                                                        
                                                                        
                                                                        
                                                                        
                                                                        

A power B calculation

# include "stdio.h"

double what( double , int );
int main()
{
int nNumber1,nNumber2;
int nLoop1,nLoop2;

printf("Enter the two number you want to display\n");
scanf("%d%d",&nNumber1,&nNumber2);

//Type casting from the double to integer

printf("A power B : %d \n",(int)what(nNumber1,nNumber2));

return 0;
}

double what( double z, int y)
{
double answer = 1;

while( y > 0 )
{
if( y%2 == 1)
answer = answer * z;
y=y/2;
z=z*z;
}
return answer;
}

OUTPUT:

Enter the two number you want to display
10 3
A power B : 1000
Press any key to continue . . .

Control Satement in C language

/*
Write a program to print
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5.

*/
# include "stdio.h"
int main()
{
int nNumber;
int nLoop1,nLoop2;
printf("Enter the last number you want to display\n");
scanf("%d",&nNumber);

for(nLoop1=1;nLoop1<=nNumber;nLoop1++)
{
for(nLoop2 = nLoop1;nLoop2 > 0; nLoop2--)
{
printf("%d \t",nLoop1);
}

printf("\n");
}
return 0;
}

OUTPUT:


Enter the last number you want to display
10
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
Press any key to continue . . .

Number of bits required to rpresents the integer value

# include "stdio.h"
int fnIntegerbit(int);
int main()
{
int nNumber;
printf("Enter the number which you want to know the number of bit representation \n");
scanf("%d",&nNumber);

printf("Required number of Bits: %d\n",fnIntegerbit(nNumber));

return 0;
}

int fnIntegerbit(int nNumber)
{

int nSum=1;
int nLoop=0;
while(nSum <= nNumber)
{
nSum += nSum;
nLoop++;

}
return nLoop;



}


OUTPUT:

Enter the number which you want to know the number of bit representation
128
Required number of Bits: 8
Press any key to continue . . .

Smallest Number using C++ Object

/*
Write a function called small_test() that is passed two arguments of type int as reference. the function should find the smaller
of the two numbers and change it to -1;
*/
# include "iostream"
using namespace std;

# include "stdio.h"
class SmallNumber
{

public:

int small_test(int *nOperand1, int *nOperand2)
{
if(*nOperand1 < *nOperand2)
{
return *nOperand1=-1;
}
else
{
return *nOperand2=-1;

}
}


};

int main()
{
SmallNumber Obj1;

int nOperand1 = 40;
int nOperand2 = 5;

printf(" Given number :: %d\t%d\n",nOperand1,nOperand2);
Obj1.small_test(&nOperand1,&nOperand2);

printf("Smallest number value changed as -1\n");
printf("%d\t%d\n",nOperand1,nOperand2);


return 0;
}

OUTPUT:

Given number :: 40 5
Smallest number value changed as -1
40 -1
Press any key to continue . . .

Operator Oveloading

The ability to associate an existing operator with a member operator function and use it with objects of its class of its as its operands is called overloading.

Operator that cannot be overloaded

the sizeof() operator
the dot operator (.)
the scope resolution operator (::)
the conditional operator (?:)
the pointer-to-member operator (.*)

Function Overloading

Function overloading is used to define a set of functions that are given the same name and same operations, but use different arguments lists.


Polymorphism is essentially one thing having many forms.

Friend function

A non-member function that is allowed access to the private variable of a class is called a friend of the class.

Inline Function

Implementation hiding can result in several small functions that manipulate the data in an object. Functions save memory space because all the calls to the function cause the same code to be executed.

When a compiler sees a function call, it usually jumps to the function. At the end of function it goes back to the instruction following the function call.

This series of events may save memory space but it takes some extra time. To save execution time in short function you may choose to put the code in the function body directly in line with the code in the calling program (ex. main function) .


An inline function is written just like a normal function in the source file but compiles into inline code instead of into a function.

However, when the program is compiled, the function body is actually inserted into the program wherever a function call occurs.

Write a program to add three numbers using inline function

# include "iostream"
using namespace std;

# include "stdio.h"

class addition
{


public:

inline int fnAddition(int nOperand1,int nOperand2,int nOperand3)
{
return nOperand1 + nOperand2 + nOperand3;

}


};

int main()
{
// Object created for three number addition
addition Three;

printf("%d\n",Three.fnAddition(2,3,4));

return 0;
}

OUTPUT:

9
Press any key to continue . . .

Tuesday, March 22, 2011

Selection sort algorithm

# include "stdio.h"
# define MAX 100
int main()
{
int szArray[MAX],nLoop,nLoop2,nCount;
//To read the array input
printf("Enter the array element count \n");
scanf("%d",&nCount);
printf("Enter the Array Element\n");
for(nLoop=0;nLoop <nCount;nLoop++ )
scanf("%d",&szArray[nLoop]);

// Selection sort algorithm
for(nLoop=0;nLoop<nCount;nLoop++)
{
int nIndex = nLoop;

for(nLoop2=nIndex;nLoop2<nCount;nLoop2++)
{
if(szArray[nIndex] < szArray[nLoop2])
{
nIndex = nLoop2;

}
}
if(nLoop != nIndex)
{
int temp;
temp = szArray[nIndex];
szArray[nIndex] = szArray[nLoop];
szArray[nLoop] = temp;
}
}

// To display the details

for(nLoop = 0; nLoop < nCount;nLoop++)
{
printf("%d\t",szArray[nLoop]);
}

printf("\n");



return 0;
}


OUTPUT:

Enter the array element count
5
Enter the Array Element
1
2
3
4
5
5 4 3 2 1
Press any key to continue . . .

Sunday, March 20, 2011

Cpp program 6

/*Q6. Write a program for the String class in which you can define a constant string, which holds the value "Hello". The program should ask the user to input a name.
The name should be concatented with the constan string and displayed as "Hell My_Name".
Use new and delete operators to allocate and deallocate memory for strings.

Hint : Assign the constant to another operator and deallocate memory for strings.*/

# include "iostream"
using namespace std;
# include "stdio.h"
# include "string.h"

class String
{
public:
char *cStr ;
public:



void display(String SS)
{
String Temp;
char ch[100];

this->cStr = new char[100];
this->cStr = "Hello ";

strcpy(ch,this->cStr);
strcat(ch,SS.cStr);


Temp.cStr = ch;

puts(Temp.cStr);



}



};





int main()
{




printf("Enter the Name\n");
String Object;
Object.cStr = new char[100];
gets(Object.cStr);
Object.display(Object);






return 0;
}

Saturday, March 19, 2011

If condition operation

# include "stdio.h"

int main()
{
int a=10,b=5,c=3,d=3;
if((a<b)&&(c=d++))
{
printf("%d %d %d %d\n" ,a,b,c,d);
}
else
{
printf("%d %d %d %d\n" ,a,b,c,d);
}
return 0;
}

OUTPUT:

10 5 3 3
Press any key to continue . . .

Note: In if function first condition itself fail. it wont checking the second condition for && operation.

Friday, March 18, 2011

Findout Machine little endian or big endian

/*
* Endian.c
*
* Created on: Mar 9, 2011
* Author: Karthikeyan.D
*/

# include "stdio.h"
# include "conio.h"
int main()
{
int num = 1;
if(*(char *)&num == 1)
{
printf("\nLittle-Endian\n");
}
else
{
printf("Big-Endian\n");
}

return 0;
}

Thursday, March 17, 2011

Cpp program5

/*

Q5. Write a program to use a String class with a member function that converts a string from lower case to upper case.
You can make use of standard library functions like toupper() to convert single characters to upper case.
You can find theis function in the header file ctype.h.

*/

# include "iostream"

# include "stdio.h"
using namespace std;
# include "ctype.h"

# define MAX 100
class String
{
public:
char *cString;

public:
char* fnString(char *cStr)
{
char szArray[MAX]={0};
int nloop=0;

while (*cStr != NULL)
{
szArray[nloop++] = toupper(*cStr);
cStr++;
}
return szArray;
}
};

int main()
{
String Obj;

Obj.cString=(char *)calloc(sizeof(char)*100,1);

printf("Enter the String which to be upper case\n");
gets(Obj.cString);

strcpy(Obj.cString, Obj.fnString(Obj.cString));
printf("%s\n",Obj.cString);

return 0;
}

OUTPUT:

Enter the String which to be upper case
this program is used to convert from lower case to upper case letter
THIS PROGRAM IS USED TO CONVERT FROM LOWER CASE TO UPPER CASE LETTER
Press any key to continue . . .

Cpp program4

/*
Intrduce class specifier and their constructor and destructor
*/

# include "iostream"
using namespace std;
# include "stdio.h"

class Check
{
public:
void print()
{
printf("A program with a difference\n");
}
Check()
{
printf("Starting up \n");
}

~Check()
{
printf("close program\n");

}
};

int main()
{

Check First;
First.print();

return 0;


}


OUTPUT:
Starting up
A program with a difference
close program
Press any key to continue . . .

Cpp program3

/* Q1.Assume a member function , get_num(), with return vlue of type int is declared inside a class called Calcuator.
Write a typical class specifier and declare the member function. Also write the definition of the function outside the class
specifier.
*/
/*
Add a private data member called number of type int to the class Calculator in Q1.
Write a constructor that initialies number to 0.
Assume the constructor is defined within the class spcifier.

*/
# include "iostream"
using namespace std;
# include "stdio.h"

class Calculator
{
protected:
int nData;
private :
int nNumber;
public:
int get_num();

Calculator() // Constructor
{
nNumber=0;
printf("%d\n",nNumber);


}
~Calculator()
{

printf("I am in destructor\n");

}

};


int Calculator ::get_num()
{
/* the member whoes declaration is within the calculator class spcifier consider to be access variabl*/

nNumber = 10000;

printf("%d\n",nNumber);

return nNumber;
}

int main()
{
Calculator obj;
obj.get_num();


return 0;
}


OUTPUT:

0
10000
I am in destructor
Press any key to continue . . .

Cpp program2

/* Assume a member function , get_num(), with return vlue of type int is declared inside a class called Calcuator.
Write a typical class specifier and declare the member function. Also write the definition of the function outside the class
specifier.
*/

# include "iostream"
using namespace std;
# include "stdio.h"

class Calculator
{
protected:
int nData;

public:
int get_num();
};



int Calculator ::get_num()
{
/* the member whoes declaration is within the calculator class spcifier consider to be access variabl*/

nData = 10000;

printf("%d\n",nData);

return nData;
}

int main()
{

Calculator Obj;
Obj.get_num();

return 0;
}


OUTPUT:

10000
Press any key to continue . . .

Cpp Constructor

# include "iostream"
using namespace std;

class Application
{
public:

int nDataInformation;
public:
void fnApplication()
{

cout << nDataInformation << endl;
}

Application();//default constructor
Application(int);//Second constructor
~Application();//Destructor

}Object;

Application::Application()
{
nDataInformation = 1000;
}

Application::Application(int ndata)
{
cout << ndata<< endl;
}

Application::~Application()
{
nDataInformation = 0;
cout << "I am in Destructor \n";

}


void main()
{
Application *Constructor=NULL;// Constructor Varible intialization
Constructor = new Application();
Object.fnApplication();//Member function calling
Application Application(9999);//Second constructor with argument


}

OUTPUT:

1000
9999
I am in Destructor
I am in Destructor
Press any key to continue . . .

Rotational Operation

# include "stdio.h"
# include "string.h"
# include "conio.h"
int main()
{
int nloop,nCount=0;
char a[100];
int length;

printf("enter the word\n");
gets(a);

length=(int)strlen(a);

while(nCount<length)
{
for(nloop=0+nCount;nloop<length;nloop++)
{
printf("%c",a[nloop]);
}
for(nloop=0;nloop<nCount;nloop++)
{
printf("%c",a[nloop]);
}
printf("\t");
nCount++;
}


return 0;
}


OUTPUT:

enter the word
INDIA
INDIA NDIAI DIAIN IAIND AINDI Press any key to continue . . .

Wednesday, March 16, 2011

Echo command operation in c code

# include "stdio.h"
int main(int argc, char *argv[])
{
int nloop;
for(nloop=1;nloop<argc;nloop++)
{
printf("%s",argv[nloop]);
}

return 0;
}

OUTPUT:
NOTE: Please execute from CMD window like below

C:\KARTHI\VSC\Echo\debug>Echo.exe "Welcome to www.ctargets.blogspot.com"
Welcome to www.ctargets.blogspot.com
C:\KARTHI\VSC\Echo\debug>

How to convert string to integer value without using atoi function.

# include "stdio.h"
# include "string.h"
# define MAX 10
int main()
{
int fnStringtoInt(char*);
char cString[MAX];
printf("Enter the integer value to String variable\n");
gets(cString);
printf("The integer value is :%d\n",fnStringtoInt(cString));
return 0;
}

int fnStringtoInt(char *cString)
{
int nValue=0;
while(*cString != NULL)
{
nValue = (nValue*10)+(*cString-'0');
cString++;
}
return nValue;
}


OUTPUT:

Enter the integer value to String variable
1234456
The integer value is :1234456
Press any key to continue . . .

Pointer concept

# include "stdio.h"
int main()
{

int *p;
int a=5;
p=&a;

printf("%d, %d\n",*(&p),&*p);
return 0;
}

OUTPUT:
1245012, 1245012
Press any key to continue . . .




variable a p
value 5 1245012
Address 1245012 1245016

Given two integers A and B. Determine how many bits required to convert A to B. Write a function int BitSwapReqd(int A, int B);

#include "stdio.h"
# define MaxBit 8

int i,nCount=0;

int szA[MaxBit]={0};
int szB[MaxBit]={0};

void main()
{



int nBitSwapReq(int,int);

nBitSwapReq(128,10);

}

int nBitSwapReq(int a,int b)
{

int binaryConversion(int,int *);
binaryConversion(a,szA);
binaryConversion(b,szB);

for(i=0;i<MaxBit;i++)
{
if(szA[i] ^ szB[i])
nCount++;
}
printf("\nnCount == %d\n",nCount);

return 0;
}
int binaryConversion(int xValue,int *szAa)
{
for(i = MaxBit-1; i>=0; i--)
{
if((1<<i)&xValue)
{
printf("1");
*szAa++ = 1;
}
else
{
printf("0");
*szAa++ = 0;
}
}
printf("\n");
return 0;
}

OUTPUT:
10000000 --> 128
00001010 --> 10

nCount == 3
Press any key to continue . . .

Tuesday, March 15, 2011

binary format display

#include "stdio.h"
int a, b, c, d, e;
void main()
{
int i;
a = 65;
b = 0x41;
c = 'A';
d = 'A'+3;

printf("a = %d b = %d c = %d\n", a, b, c);

printf("Ascii %c, decimal %d, hex %02x\n", d, d, d);

printf("Here is d in binary: ");
for(i = 7; i>=0; i--) {
if((1<<i)&d)
printf("1");
else
printf("0");
}
printf("\n");
}

if function result

# include "stdio.h"
int x=0;
i= 65;
j = 100;
int y,z;

void main()
{
if(x==0)
y=i;
else
y=j;

if(x)
z=i;
else
z=j;

printf("y=%d z=%d\n",y,z);

}

OUTPUT:

y=65 z=100
Press any key to continue . . .

Without type declaration program

# include "stdio.h"

i = 65;
j = 100;


void main()
{

printf("I am in windows platform\n",printf("%c %d ",i,j));

}

OUTPUT:

A 100 I am in windows platform
Press any key to continue . . .

#ifdef usage1

# include "stdio.h"
# define some 10

# ifdef some
int i=10;
#endif



int main()
{

printf("I am in windows platform\n",printf("%d ",i));
return 0;
}


OUTPUT:

10 I am in windows platform
Press any key to continue . . .

Find the output of this program

# include "stdio.h"
int i=1;
# ifdef i
int i=10;
#endif


int main()
{

printf("I am in windows platform\n",printf("%d ",i));
return 0;
}

OUTPUT:
1 I am in windows platform
Press any key to continue . . .

# ifdef usage

# ifdef _Win32
# include "stdio.h"
#endif

int main()
{
int i=10;
printf("I am in windows platform\n",printf("Hai "));
return 0;
}

OUTPUT:

1>c:\karthi\vsc\macros\macros\macro.c(8) : warning C4013: 'printf' undefined;

Note: 1. _Win32 is a label which is specifying the OS.

#undef usage

# undef _Win32
# include "stdio.h"


int main()
{
int i=10;
printf("I am in windows platform\n",printf("Hai "));
return 0;
}

OUTPUT:

Hai I am in windows platform
Press any key to continue . . .



Note1: 1. #undef is used to avoid the multiple inclusion of header files and source files.
2. multiple header files causes for increasing the output file size.

Note2:
1. printf function executes from right to left operation. First it starts from printf("Hai ") and then first printf() function.

Virtual method memory

# include "stdio.h"


class classname
{

void virtual fn1()
{
}



};

class classname2
{

void virtual fn1()
{
}

void virtual fn11()
{
}


};

int main()
{
printf("%d\n%d\n",sizeof(classname),sizeof(classname2));
return 0;
}

OUTPUT:

4
4
Press any key to continue . . .

Memory for Empty Class and Srtucture

# include "stdio.h"


class classname
{

void fn1()
{
}
void fn11()
{
}

void fn111()
{
}


};

struct structname
{
};

int main()
{
printf("%d\n%d\n",sizeof(classname),sizeof(structname));
return 0;
}


OUTPUT:


1
1
Press any key to continue . . .

Sunday, March 13, 2011

memcpy function sample 1

#include "stdio.h"
# include "string.h"
# include "malloc.h"
int main()
{
char *a= "Karthiken07";
char *b;

b=malloc(10*sizeof(char));

memcpy(b,a,100);

printf("%s\n",b);

return 0;
}

OUTPUT:

Karthiken07
Press any key to continue . . .

What is a static function?

A static function is a function whose scope is limited to the current source file.

Scope refers to the visibility of a function / variable. If the function / variable is visible outside of the current source file, it is said to have global, or external, scope.

If the function / variable is not visible outside of the current source file, it
is said to have local, or static, scope.

Memset sample2

#include "stdio.h"
# include "string.h"
# include "malloc.h"

int main ()
{
char str[] = "Here you can learn memset function";
// memset(pointer,char,no.of byte);
//First 12 bytes replaced by char. here '-'
memset (str,'-',12);
puts (str);
return 0;
}

OUTPUT:

------------ learn memset function
Press any key to continue . . .

memset function

#include "stdio.h"
# include "string.h"
# include "malloc.h"
int main()
{
char *a= "Karthiken07";
char *b;

b=malloc(10*sizeof(char));
memset(b,0,100);

while(*b++=*a++);
printf("%s\n",b);
return 0;


}


OUTPUT:


Press any key to continue . . .


Note: Empty string is printed ......

Add float pointer variable in C

#include "stdio.h"

int main()
{
float myVar=0;
float fVal = 5.5;
float *pFloat;
pFloat = &fVal;

myVar = myVar + *pFloat;

printf("%f\n", myVar);

return 0;
}

OUTPUT:

5.500000
Press any key to continue . . .

Pre and Post increment test

# include <stdio.h>
# include <stdlib.h>

int main()
{
int x=8;
x-=--x-x--;
printf("%d\n",x);


return 0;
}

OUTPUT:
6
Press any key to continue . . .

Find the second largest number in a arrary

// Using single loop for finding the second largest number in the array
# include<stdio.h>
# include<stdlib.h>
# define MAX 5
int main()
{
int nLoop;
int nMaximumFirst,nMaximumSecond;
int szArray[MAX];
// to get input
/* Please ignore this loop by direct initalization*/
for(nLoop=0;nLoop<MAX;nLoop++)
scanf("%d",&szArray[nLoop]);
nMaximumFirst = szArray[0];
nMaximumSecond = szArray[0];
for(nLoop=0;nLoop<MAX;nLoop++)
{
if(nMaximumFirst < szArray[nLoop])
{
nMaximumSecond = nMaximumFirst;
nMaximumFirst = szArray[nLoop];
}else if(nMaximumSecond < szArray[nLoop])
{
nMaximumSecond = szArray[nLoop];
}
else
{
// Do nothing
}

}
printf("Maximum first =%d,\nMaximum second = %d\n",nMaximumFirst,nMaximumSecond);
return 0;

}

OUTPUT:

4
5
3
2
1
Maximum first =5,
Maximum second = 4
Press any key to continue . . .

Saturday, March 12, 2011

Find if a number is divisible by 3, without using %,/ or *. You can use atoi().

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int fnSum(int);

char ch;
int main()
{
char cNumber[100];
int nsum=0,i=0;
printf("Enter the number\n");
scanf("%s",&cNumber);


nsum = fnSum(cNumber);


if(nsum == 3 || nsum == 6||nsum == 9 )
printf("Given number is divisible by 3\n");
else
printf("Given number is not divisible by 3\n");


return 0;
}

int fnSum(char *cNumber)
{
int nsum=0,i=0;

while (*(cNumber+i) != NULL)
{
ch=(*(cNumber+i));

nsum +=atoi(&ch);

i++;

}

if(nsum > 10)
{
sprintf(cNumber,"%d",nsum);
nsum = fnSum(cNumber);
}
return nsum;
}

OUTPUT:


Enter the number
366
Given number is divisible by 3
Press any key to continue . . .

Linked list using recursion

//Untested
Q: How to print the linked list in reverse order

Data * TempData ;
TempData = HeadData;

Data * ReverseOrder(Data * TempData)
{

if(TempData)
{
ReverseOrder(TempData->next);
printf("%d",TempData->data);
}
else
{
printf("NULL \t");

}


}

Thursday, March 10, 2011

Pointer = Call By Value

# include <stdio.h>
int fnCall(char *);
int main()
{
char *PTR="HELLO";
fnCall(PTR);
printf("%s",PTR);
return 0;
}

int fnCall(char *PTR)
{
PTR +=3;
return 0;
}



OUTPUT:

HELLO
Press any key to continue . . .

Wednesday, March 9, 2011

Fibonacci series in Visual C++

# include <stdio.h>
int fnFibonacci(int);
int main()
{
int nNumber;
printf("Enter the number of items limit\n");
fflush(stdin);
scanf("%d",&nNumber);

fnFibonacci(nNumber); // call function

return 0;// for successful execution
}

int fnFibonacci(int nNo) // called function
{
int a=0;
int b=1;
int nLoop,c; // Third variable
if (nNo <= 2)
{
printf("%d\t%d",a,b);
}
else
{
printf("%d\t%d",a,b);
for(nLoop = 3; nLoop<= nNo;nLoop++)
{
c = a + b;
a = b;
b = c;
printf("\t%d",c);
}
}
return 0;// for successful execution
}

OUTPUT

Enter the number of items limit
10
0 1 1 2 3 5 8 13 21 34Press

Tuesday, March 8, 2011

Multiple a number by 7 without using * and + operator.

NewNum = Num << 3; // mulitplied by 2 ^ 3 = 8

NewNum = NewNum - Num; // 8 – 1 = 7

Covert a string to upper case

void ToUpper(char * S)
{
while (*S!=0)
{
*S=(*S >= 'a' && *S <= 'z')?(*S-'a'+'A'):*S;
S++;
}
}

Insert a node a sorted linked list

void sortedInsert(Node * head, Node* newNode)
{
Node *current = head;

// traverse the list until you find item bigger the // new node value
//
while (current!= NULL && current->data < newNode->data)
{
current = current->next);
}
//
// insert the new node before the big item
//
newNode->next = current->next;
current = newNode;
}

Reverse a string

void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';

while (Begin < End)
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}

Sort a linked list

//sorting in descending order
struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
{
node* first,second,temp;
first= Head;
while(first!=null)
{
second=first->NEXT;
while(second!=null)
{
if(first->value < second->value)
{
temp = new node();
temp->value=first->value;
first->value=second->value;
second->value=temp->value;
delete temp;
}
second=second->NEXT;
}

first=first->NEXT;
}
}

Delete a node in double linked list

void deleteNode(node *n)
{
node *npre = n->prev;
node *nnext = n->next;
npre->next = n->next;
nnext->prev = n->prev;
delete n;
}

Reverse a singly linked list

1. Reverse a singly linked list


//
// iterative version
//
Node* ReverseList( Node ** List )
{

Node *temp1 = *List;
Node * temp2 = NULL;
Node * temp3 = NULL;

while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->pNext; // save the next ptr in temp2
temp1->pNext = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}

return *List;
}