Sunday, 24 August 2014

C Program to Draw Pascal's triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1


#include
int main()
{
    int rows,coef=1,space,i,j;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=0;i    {
        for(space=1;space<=rows-i;space++)
        printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

Write a C program to display reverse pyramid.

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *


 #include
void  main()
{
    int rows,i,j,space;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(space=0;space           printf("  ");
        for(j=i;j<=2*i-1;++j)
          printf("* ");
        for(j=0;j            printf("* ");
        printf("\n");
    }
    return 0;
}

Friday, 22 August 2014

C Program to print half pyramid as using number

#include
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Thursday, 21 August 2014

Pyramid And Structure Using C

#include 
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}
 

o/p
*
* *
* * *
* * * *
* * * * *

Program Of Template Partial Specialization

template 
class sortedVector
{
    public:
    void insert (T val)
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && val > vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }
    // other functions...
    private:
    T *vec_data;
    int length;
    int size;
};

To declare a partially specialized template that handles any pointer types, we'd add this class declaration


template
class sortedVector
{
    public:
    // same functions as before.  Now the insert function looks like this:
    insert( T *val )
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && *val > *vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }

    private:
    T** vec_data;
    int length;
    int size;
};

typeid, polymorphic class Using C++ Programing


#include
#include
#include
using namespace std;
class CBase 
virtual void f()
{} 
};
class CDerived : public CBase {};
int main () 
{
try 
{
CBase* a = new CBase;
CBase* b = new CDerived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
catch (exception& e) 
{ cout << "Exception: " << e.what() << endl; 
}
return 0;
}

Type Casting In Class Using C++ Programing


#include
using namespace std;

class CDummy
{
    float i,j;
};

class CAddition
{
int x,y;
  public:
CAddition (int a, int b)
{ x=a; y=b; }
int result()
{ return x+y;}
};

int main ()
{
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}

C++ Example Pointer Program


#include 
#include 
void main()
{
int i=10;
int *Ptr;
clrscr();
Ptr=&i;
cout<<"\nValue Of i :"<cout<<"\nAddress Of i :"<cout<<"\nValue Of Ptr :"<cout<<"\nAddress Of Ptr :"<<&Ptr;
cout<<"\nPtr's Pointer Value:"<<*Ptr;
cout<<"\nPtr Equal to &i :"<<*(&i));

Pointer To Functions In C++


#include
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <  return 0;
}

Example Of Pointer In C++

#include
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}

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

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

}

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

Splitting a string into tokens in C

#include 
#include 
#include 

#define MAX_LINE_LEN 256
#define NULL_TERM '\0'

/* A function that will print the content of the token_list */
int printcharlist(char **tok_list)
{
    char **pptr = tok_list;

    while (*pptr) {
        printf ("++ %s\n", *pptr);
        pptr++;
    }

    return 0;
}

/* returns the string between 2 pointers (not forgetting to append NULL_TERM */
char *getStr(char *start, char *end)
{
    int length=end-start;
    int i=0;
    char *result = (char *)malloc (sizeof (char) * (length+1));

    while (i < length) {
    result[i] = *start;
    start++;
    i++;
    }

    result[i] = NULL_TERM;

    return result;
}

Finding Tokens in a String Using Strock

#include
#include
#include

void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;

clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{


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

t = strtok(NULL, s);
;

}

Finding Tokens in a String Using C Language

#include
#include
#include

void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
 for(;a[i]==' ';i++);
 {
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}

Remove Space In String Using C

#include
#include
#include

void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
 for(;a[i]==' ';i++);
 {
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}

Accept 10 numbers and store in an array, print sorted array in descending and ascending order


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("Enter 10 nos\n");

for(i=0;i<10 i="" nbsp="" span="">
 scanf("%d",&a[i]);
 for(i=0;i<10 i="" span="">
 { 
 for(j=i+1;j<10 j="" span="">
 {
 if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<10 i="" span="">
 printf(" %d ",a[i]);
 printf(" Nos sorted in ascending order\n");
 printf("\n");
 for(i=9;i>=0;i--)
printf(" %d ",a[i]);
printf(" Nos sorted in descending order\n");
getch();
}