Thursday, 21 August 2014

Example Of XOR In C


#include
#include

struct xnode
{
int data;
unsigned long direction;
};
struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);
int main(void)
{
struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);
printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);
return 0;
}
struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));
assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;
if(list != NULL)
list->direction ^= (unsigned long)newxnode;
return newxnode;
}
void walk_list(struct xnode *list)
{
unsigned long prev = 0;
while(list != NULL) {
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}
printf("\n");
}

Wednesday, 20 August 2014

Using C Program Print Fibonacci sequence 1 1 2 3 5 8 13 21 34 55 89 ...

1 1 2 3 5 8 13 21 34 55 89 ...
#include
void main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);

}


fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;

if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}

Program to calculate the factorial of a number. Use the concept of recursion instead of using loops.


#include

void main()

{

int a, fact;


printf("\nEnter any number: ");

scanf ("%d", &a);

fact=rec (a);

printf("\nFactorial Value = %d", fact);

}

rec (int x)

{

int f;


if (x==1)

return (1);

else

f=x*rec(x-1);


return (f);

}

Tuesday, 19 August 2014

C Program For DFA End With 11

#include
#include
void CreateDFA();
int StateMoveInDFA(int,char);
char Letters[10];
int NoOfInputLetter,NoOfStates,NoOfFinalState;
int FinalStates[5];
int DFA[10][10];
void main()
{
int s,i,Accepted;
char Line[10];
CreateDFA();
do
{
s=0;
i=0;
Accepted= 0;
printf("\n\nEnter Input String.. ");
scanf("%s",Line);
while(Line[i]!='\0')
if((s=StateMoveInDFA(s,Line[i++]))<0 p="">break;
for(i=0 ;iif(FinalStates[i] ==s )
Accepted= 1;
(Accepted)?printf("\nInputted String is Valid...") : printf("\nInputed String is Not Valid...");
getch();
printf("\n\nDo you want to continue..??(y/n)");
}
while(getch()=='y' || getch()=='Y');
}
void CreateDFA()
{
int i,j;
char c;
printf("Enter the no. of states.. ");
scanf("%d",&NoOfStates);
printf("Enter the no. of input letters.. ");
scanf("%d",&NoOfInputLetter);
printf("Enter the no. of final states.. ");
scanf("%d",&NoOfFinalState);
for(i=0;iprintf("Final state %d : ",i+1);
scanf("%d",&FinalStates[i]);
}
for(i=0;iprintf("\nInput Letter %d :",i+1);
printf("%c",Letters[i]=getch());
}
printf("\nEnter the State Movements In DFA...\n");
for(i=0;ifor(j=0;jprintf("Move(%d,%c) : ",i,Letters[j]);
scanf("%d",&DFA[i][j]);
}
}
}
int StateMoveInDFA(int s,char c)
{
int j;
for(j=0;jif(c==Letters[j])
return(DFA[s][j]);
return -1;
}

C++ Using Calculator Using Function

#include
#include
#include
void Input (float &x, float &y);

float a=1.0, b=1.0, result;
char operation;


int main ()
{
cout << "Program which performs addition, subtraction, multiplication and subtraction. \n\n";
cout << "Please input calculation operation (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);

cout << "The answer is: " << result << endl;
system ("pause");
return 0;
}


void Input (float &x, float &y)
{
a = x;
b = y;

switch (operation)
{
case '+':
result = x + y;
break;

case '-':
result = x - y;
break;

case '*':
result = x * y;
break;

case '/':
result = x / y;
break;

default:
cout << "Improper operation. Please input a correct calculation operation: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}

Factorial off a number using do-while-loop

#include
#include
void main(){int n,f=1;clrscr();printf("enter any number=");scanf("%d",&n);do{f=f*n;n--;}while(n>0);printf("factorial number is=%d",f);getch();}

C program to find the sum of individual digits of a positive integer


#include

#include

void main()

{

int num, k=1, sum=0;

clrscr();

printf(“Enter the number whose digits are to be added:”);

scanf(“%d”,&num);

while(num!=0)

{

k=num%10;

sum=sum+k;

k=num/10;

num=k;

}

printf(“Sum of the digits:%d”,sum);

getch();

}