Wednesday, 20 August 2014

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();

}

C Program to create a linear linked list interactively and print out the list and the total number of items in the list



#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Assign Value Of Pointer In C

#include<stdio.h>
int main()
{
int num[2] = {10,20};
const int *ptr;

ptr = &num[0];

*ptr = 30;

printf("%d",*ptr);

return(0);
}