Read in your tongue here..

Friday, June 28, 2013

Twin Prime Numbers

void main()
{
     int t=0,x,n=1,i,p=0,n2;
     printf("Enter number of terms:");
     scanf("%d",&x);
     while(t<x)
     {
     for(i=1;i<=n;i++)
           if(n%i==0)
                p++;
     if(p==2)
     {
           p=0;
           n2=n+2;
           for(i=1;i<=n2;i++)
                if(n2%i==0)
                     p++;
           if(p==2)
           {
                printf("(%d,%d) ",n,n2);
                t++;
           }
     }
     p=0;
     n+=2;
     }
     getch();
     clrscr();
}


Storing and Display info using Structure

#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct data{
            char name[45];
            int roll,m1,m2,m3,t;
            float avg;
};
struct stu{
            struct data d;
            struct stu *next;
}*head=NULL,*temp,*node;
void in(char *,int,int,int,int), dis(void);
void main()
{
            char *n;
            int r,x,y,z;
            printf("\nMain Menu\n1.Insert details\n2.Display\n3.Exit\nEnter choice:");
            scanf("%d",&r);
            switch(r)
            {
                        case 1:
                                    getc(stdin);
                                    printf("Enter name of the student:");
                                    gets(n);
                                    printf("Enter roll number:");
                                    scanf("%d",&r);
                                    printf("Enter marks of three subjects:");
                                    scanf("%d%d%d",&x,&y,&z);
                                    in(n,r,x,y,z);
                                    break;
                        case 2:
                                    clrscr();
                                    if(head==NULL)
                                                printf("EMPTY RECORDS!!");
                                    else
                                    {
                                                puts("\nThe records are:\n");
                                                printf("\tName\t\tRoll\tMarks\tMarks\tMarks\tTotal\tAverage\n");
                                                dis();
                                    }
                                    break;
                        case 3:
                                    clrscr();
                                    exit();
                        default:
                                    clrscr();
                                    printf("WRONG INPUT!!");
            }
            main();
}

void in(char *n,int r, int x,int y, int z)
{
            if(head==NULL)
            {
                        head=(struct stu*)malloc(sizeof(struct stu));
                        strcpy(head->d.name,n);
                        head->d.roll=r;
                        head->d.m1=x;
                        head->d.m2=y;
                        head->d.m3=z;
                        head->d.t=x+y+z;
                        head->d.avg=head->d.t/3;
                        head->next=NULL;
            }
            else
            {
                        temp=head;
                        while(temp->next!=NULL)
                                    temp=temp->next;
                        node=(struct stu*)malloc(sizeof(struct stu));
                        strcpy(node->d.name,n);
                        node->d.roll=r;
                        node->d.m1=x;
                        node->d.m2=y;
                        node->d.m3=z;
                        node->d.t=x+y+z;
                        node->d.avg=node->d.t/3;
                        temp->next=node;
            }
}
void dis(void)
{
            int max=head->d.t;
            temp=head;
            while(temp!=NULL)
            {
                        printf("%s\t%d\t%d\t%d\t%d\t%d\t%.2f\n",temp->d.name,
                        temp->d.roll,temp->d.m1,temp->d.m2,temp->d.m3,temp->d.t,temp->d.avg);
                        if(temp->d.t>max)
                                    max=temp->d.t;
                                    temp=temp->next;
            }
            printf("The student to obtain highest marks is:\nName: %s\nRoll: %d\nMarks: %d, %d and %d\nTotal: %d\nAverage: %.2f",temp->d.name,temp->d.roll,temp->d.m1,temp->d.m2,temp->d.m3,temp->d.t,temp->d.avg);

}

Singly Linked List

#include<alloc.h>
struct list{
     int data;
     struct list *next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
     if(head==NULL)
     {
           clrscr();
           printf("The list is empty!!");
           return 1;
     }
     return 0;
}
void main()
{
printf("\n\n\tMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
     scanf("%d",&num);
     switch(num)
     {
           case 1:
                infirst();
                break;
           case 2:
                inpos();
                break;
           case 3:
                inlast();
                break;
           case 4:
                delfront();
                break;
           case 5:
                delpos();
                break;
           case 6:
                dellast();
                break;
           case 7:
                display();
                break;
           case 8:
                count();
                break;
           case 9:
                clrscr();
                exit();
           default:
                clrscr();
                puts("\nWRONG INPUT!!");
     }
     main();
}
void infirst(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=(struct list*)malloc(sizeof(struct list));
           temp->data=num;
           temp->next=head;
           head=temp;
           printf("Continue [y/n]?:");
           con=getche();
     }while(con=='y'||con=='Y');
}
void inpos(void)
{
     do
     {
           num=0;
           temp=head;
           printf("Enter position to insert at:");
           scanf("%d",&pos);
           while(temp->next!=NULL)
           {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
           }
           printf("Enter number:");
           scanf("%d",&num);
           node=(struct list*)malloc(sizeof(struct list));
           node->data=num;
           node->next=temp->next;
           temp->next=node;
           printf("Continue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}
void inlast(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=head;
           while(temp->next!=NULL)
                temp=temp->next;
           node=(struct list *)malloc(sizeof(struct list));
           node->data=num;
           node->next=NULL;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}

void delfront(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                temp=head;
                head=temp->next;
                free(temp);
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void delpos(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                num=0;
                temp=head;
                printf("Enter position:");
                scanf("%d",&pos);
                while(temp->next!=NULL)
                {
                     num++;
                     if(num==pos-1)
                     break;
                     temp=temp->next;
                }
                node=temp->next->next;
                free(temp->next);
                temp->next=node;
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void dellast(void)
{
     do{
           if(empty()==1)
                break;
           else
           {
                temp=head;
                while(temp->next!=NULL)
                {
                     node=temp;
                     temp=temp->next;
                }
                node->next=NULL;
                free(temp);
                printf("Continue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void display(void)
{
     if(empty()==1)
return;
     else
     {
           clrscr();
           printf("The list is: ");
          for(temp=head;temp!=NULL;temp=temp->next)
                printf("%d ",temp->data);
     }
}
void count(void)
{
     num=0;
     temp=head;
     while(temp!=NULL)
     {
           num++;
           temp=temp->next;
     }
     clrscr();
     printf("Total number of nodes=%d",num);
}



Doubly Linked List

#include<alloc.h>
struct list{
     int data;
     struct list *prev,*next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
     if(head==NULL)
     {
           clrscr();
           printf("The list is empty!!");
           return 1;
     }
     return 0;
}

void main()
{
     printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
     scanf("%d",&num);
     switch(num)
     {
           case 1:
                infirst();
                break;
           case 2:
                inpos();
                break;
           case 3:
                inlast();
                break;
          case 4:
                delfront();
                break;
           case 5:
                delpos();
                break;
           case 6:
                dellast();
                break;
           case 7:
                display();
                break;
           case 8:
                count();
                break;
           case 9:
                clrscr();
                exit();
           default:
                clrscr();
                puts("\nWRONG INPUT!!");
     }
     main();
}
void infirst(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=(struct list*)malloc(sizeof(struct list));
           temp->data=num;
           temp->next=head;
           temp->prev=NULL;
           head=temp;
           printf("\nContinue [y/n]?:");
           con=getche();
     }while(con=='y'||con=='Y');
}
void inpos(void)
{
     do
     {
           num=0;
           temp=head;
           printf("\nEnter position to insert at:");
           scanf("%d",&pos);
           while(temp->next!=NULL)
           {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
           }
           printf("\nEnter number:");
           scanf("%d",&num);
           node=(struct list*)malloc(sizeof(struct list));
           node->data=num;
           node->next=temp->next;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}
void inlast(void)
{
     do
     {
           printf("\nEnter data:");
           scanf("%d",&num);
           temp=head;
           while(temp->next!=NULL)
                temp=temp->next;
           node=(struct list *)malloc(sizeof(struct list));
           node->data=num;
           node->next=NULL;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}

void delfront(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                head=temp->next;
                head->prev=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void delpos(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                num=0;
                temp=head;
                printf("\nEnter position:");
                scanf("%d",&pos);
                while(temp->next!=NULL)
                {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
                }
                node=temp->next->next;
                free(temp->next);
                temp->next=node;
                node->prev=temp;
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void dellast(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                while(temp->next!=NULL)
                {
                     node=temp;
                     temp=temp->next;
                }
                node->next=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void display(void)
{
     if(empty()==1)
return;
     else
     {
           puts("The list is:");
           for(temp=head;temp!=NULL;temp=temp->next)
                printf("%d ",temp->data);
     }
}
void count(void)
{
     num=0;
     temp=head;
     while(temp!=NULL)
     {
           num++;
           temp=temp->next;
     }
     clrscr();
     printf("\nTotal number of nodes=%d",num);
}