Tuesday, 20 October 2015

C program to print total number of words in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],ch=0;
int i=0,wcnt=1;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch==' ')
                      wcnt++;
                putchar(ch);
                i++;

        }
printf("\n total no. of words in string=%d",wcnt);
getch();
}

C program to implement circular doubly linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next,*pre;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created.....error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created....error");
            break;
        }
        printf("\n enter the data for selection");
        scanf("%d",&m);
        selection(m);
    break;


    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
    break;

    case 8:
        exit(1);
    break;

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    if(start==NULL)
    {
        start=tmp;
        start->next=start;
        start->pre=start;
        return;
    }
    p=start;
    while(p->next!=start)
        p=p->next;
    p->next=tmp;
    tmp->pre=p;
    tmp->next=start;
    start->pre=tmp;
}

void display()
{
    struct node *p;
    p=start;
    do
    {
        printf("\n data= %d",p->data);
        p=p->next;
    } while(p!=start);
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    p=start;
    while(p->next!=start)
    {
        p=p->next;
    }
    p->next=tmp;
    tmp->pre=p;
    tmp->next=start;
    start=tmp;
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    p=start;
        while(p->next!=start)
        {
            p=p->next;
        }
        p->next=tmp;
        tmp->pre=p;
        start->pre=tmp;
}

void count()
{
    int i=1,j;
    struct node *p;
    p=start;
    while(p->next!=start)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node*)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        insert_at_beg(item);
        return;
    }
    p=start;
    for(i=1;i<pos;i++)
    {
        p=p->next;
    }
    tmp->next=p->next;
    p->next->pre=tmp;
    tmp->pre=p;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
}

void selection(int item)
{
    int i;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        start=start->next;
        start->pre->next=p->next;
        start->next->pre=start->pre;
        free(p);
        p=start;

        while(p->next!=start)
        {
            p=p->next;
        }
        start->pre=p;
        p->next=start;
        printf("\n node freed");
        return;
    }
    q=start;
    while(q->next->data!=item)
    {
        q=q->next;
        i++;
    }
    p=q->next;
    q->next=q->next->next;
    q->next->next->pre=q;
    free(p);
    if(q==NULL)
    {
        printf("\n wrong value");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
}

Monday, 19 October 2015

C program to transpose a matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],t[3][3],i,j;
clrscr();
printf("\n enter d values of d array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n d transpose of matrix A is :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        t[i][j]=a[j][i];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
        printf("\t %d",t[i][j]);
    }
}
getch();
}

C program to perform multiplication of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,k,res[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n the product of above 2 matrices are:");

for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        res[i][j]=0;

            for(k=0;k<=2;k++)
            {
            res[i][j]+=a[i][k]*b[k][j];
            }

    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
    printf("\t %d",res[i][j]);
    }
}
getch();
}

C program to perform subtraction of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,diff[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n the difference of corresponding values are :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        diff[i][j]=a[i][j]-b[i][j];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
    printf("\t %d",diff[i][j]);
    }
}
getch();
}

C program to perform addition of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,sum[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n d sum of corresponding values r :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        sum[i][j]=a[i][j]+b[i][j];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
    printf("\t %d",sum[i][j]);
    }
}
getch();
}

C program to implement doubly linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next,*pre;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created.....error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created....error");
            break;
        }
        printf("\n enter the data for selection");
        scanf("%d",&m);
        selection(m);
        break;


    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
        break;

    case 8:
        exit(1);

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    if(start==NULL)
    {
        start=tmp;
        start->pre=NULL;
        return;
    }
    p=start;
    while(p->next!=NULL)
        p=p->next;
        p->next=tmp;
        tmp->pre=p;
}

void display()
{
    struct node *p;
    p=start;
    while(p!=NULL)
    {
        printf("\n data= %d",p->data);
        p=p->next;
    }
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    tmp->pre=NULL;
    start=tmp;
    getch();
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    tmp->pre=p;
    getch();
}

void count()
{
    int i=1;
    struct node *p;
    p=start;
    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node*)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        tmp->next=start;
        tmp->pre=NULL;
        start=tmp;
        printf("\n node inserted at 1st position");
        return;
    }
    p=start;
    for(i=1;i<pos;i++)
    {
        p=p->next;
    }
    tmp->next=p->next;
    tmp->pre=p;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
}

void selection(int item)
{
    int i;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        start=start->next;
        start->pre=NULL;
        free(p);
        printf("\n node free");
        return;
    }
    q=start;
    while(q->next->data!=item)
    {
        q=q->next;
        i++;
    }
    p=q->next;
    q->next=q->next->next;
    q->next->next->pre=q;
    free(p);
    if(q==NULL)
    {
        printf("\n wrong value");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
}

C program to change the case of a string


  •  When 1 string is in lower case & another is in upper case

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char st[20],st1[20];
int l;
clrscr();
printf("enter 1st string: ");
gets(st);
printf("\n enter 2nd string:");
gets(st1);
printf("\n string 1 in lower case is:");
strlwr(st);
puts(st);
printf("\n string 2 in upper case is:");
strupr(st1);
puts(st1);
getch();
}

C program to calculate the length of a string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[20];
int l;
clrscr();
printf("enter a string: ");
gets(st);
l= strlen(st);
printf("length of the string :%d ",l);
getch();
}

C program to compare 2 strings

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15],st2[30];
int l;
clrscr();
printf("enter 2 strings: ");
gets(st1);
gets(st2);
l=stricmp(st2,st1);
 if(l==0)
 printf("same strings");
 else
 printf("different strings");
getch();
}


                                                                         OR



#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100];
int i=0;
clrscr();
printf("\n enter 1st string :");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);

    while(st1[i]==st2[i]&&st1[i]!='\0')
    i++;

        if(st1[i]>st2[i])
            printf("\n string 1 is greatest");

        else if(st1[i]<st2[i])
            printf("\n string 2 is greatest ");

        else
            printf("\n both the strings are equal");
getch();
}

C program to implement queue

#include<stdio.h>
#include<conio.h>
#define MAX 10

int queue[MAX],front=-1,rear=-1;

void push(int[],int);
int pop(int []);
int peep(int[]);

void display(int []);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                push(queue,value);
            break;

            case 2:
                printf(" REar %d\n",rear);
                printf(" FRont %d\n",front);
                if(front==-1||front>rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(queue);
                printf("%d value is deleted",m);
            break;

            case 3:

                printf(" FRont %d\n",front);
                printf(" REar %d\n",rear);

                if(front==-1||front>=rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(queue);
                printf("%d",m);
            break;

            case 4:
                display(queue);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int queue[],int val)
{
        printf("\n enter data to be inserted:");
        scanf("%d",&val);
        if(rear==MAX-1)
              printf("\n queue overflow");
        if(front==-1&&rear==-1)
             front=rear=0;
        else
        {
             rear++;
        }

        queue[rear]=val;

}

int pop(int queue[])
{
    int val;
    val=queue[front];
    front++;
    return val;
}

int peep(int queue[])
{
     return queue[front];
}

void display()
{
    int i;
    for(i=front;i<=rear;i++)
    {
        printf("%d\n",queue[i]);
    }
}





C program to implement circular queue

#include<stdio.h>
#include<conio.h>
#define MAX 10
int dqueue[MAX];
int left=-1,right=-1;
int insertright();
int insertleft();
void deleteleft();
void deleteright();
void display();
void main()
{
 int choice,m;
 clrscr();
 while(1)
 {
  clrscr();
  printf("\n\n\t\t\t*****MAIN  MENU******\n\n");
  printf("PRESS 1 : TO INSERT RIGHT\n");
  printf("PRESS 2 : TO DELETE RIGHT\n");
  printf("PRESS 3 : TO DELETE LEFT\n");
  printf("PRESS 4 : TO DISPLAY\n");
  printf("PRESS 5 : TO EXIT\n");
  printf("\n\nenter your choice : ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    m=insertright();
    printf("\nthe value %d inserted successfully\n",m);
   break;
   case 2:
    deleteright();
   break;
   case 3:
    deleteleft();
   break;
   case 4:
    display();
   break;
   case 5:
    exit(1);
   break;
   default:
    printf("\nERROR.......WRONG CHOICE\n");
  }
 getch();
 }
}
int insertright()
{
 int data;
 printf("\n enter the data to be inserted : ");
 scanf("%d",&data);
 if(left==0 && right==MAX-1 || left==right+1)
 {
  printf("\n overflow ");
  return;
 }
 if(left==-1)
 {
  left=0;
  right=0;
 }
       else if(right==MAX-1 && left>0)
 {
  right=0;
 }
 else
  right++;
 dqueue[right]=data;
 return data;
}
void deleteleft()
{
 if(left==-1)
 {
  printf("\n underflow");
  return;
 }
 else if(left==right)
 {
  left=-1;
  right=-1;
  printf("\n data deleted.....");
  return;
 }
 else
 {
  printf("\n data deleted");
  left++;
 }
}
void deleteright()
{
  if(left==-1)
 {
  printf("\n underflow");
  return;
 }
 if(left==right)
 {
  left=-1;
  right=-1;
  printf("\n data deleted");
  return;
 }
 else
 {
  printf("\n data deleted");
  right--;
 }
}
void display()
{
 int i;
 if(left==-1)
 {
  printf("\n underflow ");
  return;
 }
 if(left<right)
 {
  for(i=left;i<=right;i++)
  {
   printf("\n data : %d",dqueue[i]);
  }
 return;
 }
 else if(left>right)
 {
  for(i=left;i<=MAX-1;i++)
   printf("\n %d",dqueue[i]);
  for(i=0;i<=right;i++)
   printf("\n %d",dqueue[i]);
 }
 else
  printf("\n %d",dqueue[left]);
}

C program to implement stack

#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
void display(int st[]);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                if(top==MAX-1)
                {
                    printf("\n stack overflow");
                    break;

                }
                printf("\n enter data to be inserted:");
                scanf("%d",&value);
                push(st,value);
            break;

            case 2:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(st);
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(st);
                printf("%d",m);
            break;

            case 4:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                display(st);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int st[],int val)
{
    top++;
    st[top]=val;
}

int pop(int st[])
{       int val;
    val=st[top];
    top--;
    return val;
}

int peep(int st[])
{
     return st[top];
}

void display()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("%d\n",st[i]);
    }
}



                                                                               OR


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
int m,n;

struct node
{
    int data;
    struct node *next;
}*top;

void push(int val);
int pop();
void display();

void main()
{
    int ch,i;
    top=NULL;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for display");
        printf("\n press 4 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                printf("\n enter total no. of nodes to create:");
                scanf("%d",&m);
                for(i=0;i<m;i++)
                {

                    printf("\n enter data to be inserted:");
                    scanf("%d",&n);
                    push(n);
                }
            break;

            case 2:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop();
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                display();
            break;

            case 4:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int val)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=val;
    if(top==NULL)
    {
        top=tmp;
        top->next=NULL;
        return;
    }
    else
    {
        tmp->next=top;
        top=tmp;
    }
}

void display()
{
    struct node *q;
    q=top;
    while(q!=NULL)
    {
        printf("\n data=%d",q->data);
        q=q->next;
    }
}

int pop()
{
    struct node *tmp,*p;

    if(p!=top)
    {
        p=top;
        tmp=p;
        top=top->next;

        free(p);
    }
    return tmp->data;

}





C program to implement single linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
            if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created till now....error");
            break;
        }
        printf("\n enter data for selection:");
        scanf("%d",&m);
        selection(m);
        break;

    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
        break;

    case 8:
        exit(1);

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    if(start==NULL)
    {
        start=tmp;
        return;
    }
    p=start;
    while(p->next!=NULL)
        p=p->next;
     p->next=tmp;
    getch();
}

void display()
{
    struct node *p;
    p=start;
    while(p!=NULL)
    {
        printf("\n data= %d",p->data);
        p=p->next;
    }
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    start=tmp;
    getch();
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        tmp->next=start;
        start=tmp;
        printf("\n node inserted at 1st position");
        getch();
    }
    p=start;
    for(i=1;i<pos;i++)
        p=p->next;
    tmp->next=p->next;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
    getch();
}



void selection(int item)
{
    int i=1;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        printf("\n data found at 1st position");
        start=start->next;
        free(p);
        printf("\n node free");
        getch();
    }

    q=start;
    while(q->next->data!=item)
    {
        i++;
        q=q->next;
    }
    p=q->next;
    q->next=q->next->next;
    free(p);

    if(q==NULL)
    {
        printf("\n error");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
    getch();
}

void count()
{
    int i=1;
    struct node *p;
    p=start;
    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}


C program to copy one string into another string



 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
clrscr();
printf("\n enter 1st string:");
gets(str1);
printf("\n enter 2nd string:");
gets(str2);
printf("\n string after copying is :");
strcpy(str2,str1);
puts(str2);
getch();
}


                                                                     OR

#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100];
int i=0;
clrscr();
printf("\n enter 1st string :");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);

    while(st2[i]!='\0')
    {
        st1[i]=st2[i];
        i++;
    }

printf("\n copied string is :");
puts(st2);
getch();
}

C program to convert farhenheit into degree celcius

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float f,c;
clrscr();
printf("enter value in farhenheit= ");
scanf("%f",&f);
c=0.59*(f-32);
printf("value in celcius=%f",c);
getch();
}

C program to concatnate 2 strings

#include<stdio.h>
#include<conio.h>
void main()
{
char st1[15],st2[30];
clrscr();
printf("\n enter 2 strings :");
gets(st1);
gets(st2);
strcat(st2,st1);
printf("\n concentated string is :");
puts(st2);
getch();
}


                                                                                  OR       


#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100],st3[100];
int i=0,j=0;
clrscr();
printf("\n enter 1st string:");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);
while(st1[i]!='\0')
{
        st3[j]=st1[i];
        i++;
        j++;

 }
i=0;
while(st2[i]!='\0')
{
        st3[j]=st2[i];
        i++;
        j++;
 }
printf("\n concatnated string is :");
puts(st3);
getch();
}


C program to print the table of a no.


                                                         USING  FOR  LOOP
#include<stdio.h>
#include<conio.h>
void main()
{
int i,t,r;
clrscr();
printf("\n enter any no. :");
scanf("%d",&t);
for(i=1;i<=10;i++)
{
         r=t*i;
         printf("%d\n",r);
}
getch();
}

                                                     USING  GOTO  STATEMENT


#include<stdio.h>
#include<conio.h>
void main()
{
int a,i=1;
clrscr();
printf("\n enter a value:");
scanf("%d",&a);
table:
printf("\n%d",a*i);
i=i+1;
if(i<=10)
goto table;
getch();
}

C program to print whether the inputted character is vowel or consonant


                                                     USING  SWITCH  CASE

#include<stdio.h>
#include<conio.h>
void main()
{
char letter;
clrscr();
printf("\n enter letter:");
scanf("%c",&letter);
switch(letter)
{
          case'a':
          case'e':
          case'i':
          case'o':
          case'u':
          printf("%c is vowel",letter);
          break;
          default:
          printf("%c is consonant",letter);
}
getch();
}

                                                                       OR   ( USING IF-ELSE)     


#include<stdio.h>
void main()
{
char letter;
clrscr();
printf("\n enter letter:");
scanf("%c",&letter);
if(letter=='a')
{
         print("vowel");
}
else if(letter=='e')
{
          printf("vowel");
}
else if(letter=='i')
{
           printf("vowel");
}
else if(letter=='o')
{
           printf("vowel");
}
else if(letter=='u')
{
          printf("vowel");
}
else
{
           printf("consonant");
}
getch();
}
 

C program to calculate factorial of a no.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,f=1,num;
clrscr();
printf("\n enter any no. :");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
          f=f*i;
}
printf("\n result is=%d",f);
getch();
}


                                                         OR ( USING  POINTER) 


#include<stdio.h>
#include<conio.h>
void fact()
{
        int a,i,f=1,*x,*y;
        x=&a,y=&f;
        printf("\n enter the no. :");
        scanf("%d",x);
        for(i=1;i<=(*x);i++)
        {
                *y=(*y)*i;          
         }
        printf("\n factorial of %d = %d",*x,*y);
}

void main() 
{
        clrscr();
        fact();
        getch();
 }

                                                                     OR     


#include<stdio.h>
#include<conio.h>
float fact(float x)
{
    float i, f=1;
    for(i=1;i<=x;i++)
    {
        f=f*i;
    }
    return f;
}

void main()
{
    float z,a;
    clrscr();
    printf("\n enter a no.:");
    scanf("%f",&a);
    z=fact(a);
    printf("\n factorial of %f =%f",a,z);
    getch();
}



C program to check whether a no. is prime or not

#include<stdio.h>
#include<conio.h>
void main()
{
int x,i,y=0;
clrscr();
printf("\n enter any no.:");
scanf("%d",&x);
for(i=2;i<=x/2;i++)
{
if(x%i==0)
          y=1;
}
if(y==0)
          printf("\n %d is prime no.",x);
else
          printf("\n %d isn't prime no.",x);
getch();
}

C program to print fibonacci series

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,n1=0,n2=1;
clrscr();
printf("\n enter the value:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i<=1)
n=i;
else
{
n=n1+n2;
n1=n2;
n2=n;
}
printf("\n %d",n);
printf(" , ");
}
getch();
}

C program to check whether the no. is armstrong or not

#include<stdio.h>
#include<conio.h>
void main()
{
int y,x,r,sum=0;
clrscr();
printf("enter a no :");
scanf("%d",&y);
x=y;
while(x!=0)
{
r=x%10;
sum=sum+(r*r*r);
x=x/10;
}
if(y==sum)
printf("\n no is armstrong no");
else
printf("\n no is not armstrong");
getch();
}

C program to reverse a number

#include<stdio.h>
#include<conio.h>
void main()
{
int x,r;
clrscr();
printf("enter a no :");
scanf("%d",&x);
while(x!=0)
{
r=x%10;
printf("%d",r);
x=x/10;
}
getch();
}

C program to check whether a no. is palindrome or not

#include<stdio.h>
#include<conio.h>
void main()
{
int x,r,y;
clrscr();
printf("enter a no :");
scanf("%d",&y);
x=y;
while(x!=0)
{
r=x%10;
printf("%d",r);
x=x/10;
}
getch();
if(y==r)
printf("\n no.is palindrome");
else
printf("\n no. isn't palindrome");
getch();
}