Showing posts with label assignment. Show all posts
Showing posts with label assignment. Show all posts

Friday, 2 September 2016

Linear doubly circular linked list : C implementation

Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.


//assignment 4
//doubly circular linked list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *next;
struct node *prev;
};

struct node *head=NULL;

void insert();
void delete();
void search();
void display();

int main()
{
int choice;
//struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav,*nth;
trav=head;
int n,count=0;

    printf("\nEnetr your choice :\n");
    printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
    scanf("%d",&choice);
    //getting the data
    printf("Enter name :\n");
    scanf("%s",temp->name);
    printf("Enter roll number :\n");
    scanf("%d",&(temp->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(temp->cgpa));
   
    if(head==NULL)
    {
        head=temp;
        temp->next=head;
        temp->prev=head;
    }
    else{
   
        switch(choice)
        {
        case 1:

            temp->next=head;
            head->prev=temp;
            while(trav->next!=head)
            {
                trav=trav->next;
            }   
            trav->next=temp;
            temp->prev=trav;
            head=temp;
            break;
        case 2:
            while(trav->next!=head)
            {
                trav=trav->next;
            }   
            trav->next=temp;
            temp->prev=trav;
            temp->next=head;break;   
        case 3:
           
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->next;
                count++;
            }
            nth=trav->next;
            temp->next=nth;
            temp->prev=trav;
            trav->next=temp;
            nth->prev=temp;
            break;   
        }
   
    }

}

void delete()
{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1,*trav;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->next;
        while(trav->next!=head)
        {
            trav=trav->next;
        }
        trav->next=temp;
        temp->prev=trav;
        head=temp;
        break;
    case 2:
        while(temp->next->next!=head)
        {
            temp=temp->next;
        }
        last=temp->next;
        temp->next=head;
        head->prev=temp;
        free(last);break;       
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->next;
            count++;
        }
        temp1=temp->next;
        temp->next=temp1->next;
        temp1->next->prev=temp;
        free(temp1);break;
    }
}

void search()
{
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    do
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;           
        }
        temp=temp->next;
    }while(temp->next!=head);
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
int c;
printf("Press 0 : Print in usual order\n");
printf("Press 1 : Print in reverse order\n");
scanf("%d",&c);

if(c==0){
        int count=1;
        printf("\n...................................\n");
        do
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            temp=temp->next;
            count++;
        }while(temp!=head);
        printf("...................................\n");
    }
else if(c==1){
        int count=1;
        while(temp->next!=head)
        {
            temp=temp->next;
        }
        printf("\n...................................\n");
        do
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n",temp->cgpa);
            temp=temp->prev;
            count++;
        }while(temp!=head);
       
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n",temp->cgpa);
           
        printf("...................................\n");
    }
}

Thanking you !

Linear doubly linked list : C implementation

Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.


//assignment 4
//double linked list

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *next;
struct node *prev;
};

struct node *head=NULL;

void insert();
void delete();
void search();
void display();

int main()
{
int choice;
struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav,*nth;
trav=head;
int n,count=0;

    printf("\nEnetr your choice :\n");
    printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
    scanf("%d",&choice);
    //getting the data
    printf("Enter name :\n");
    scanf("%s",temp->name);
    printf("Enter roll number :\n");
    scanf("%d",&(temp->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(temp->cgpa));
   
    if(head==NULL)
    {
        temp->next=NULL;
        temp->prev=NULL;
        head=temp;
    }
    else{
   
        switch(choice)
        {
        case 1:
            temp->next=head;
            head->prev=temp;
            head=temp;break;  
        case 2:
            while(trav->next!=NULL)
            {
                trav=trav->next;
            }  
            trav->next=temp;
            temp->prev=trav;
            temp->next=NULL;break;  
        case 3:
          
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->next;
                count++;
            }
            nth=trav->next;
            temp->next=nth;
            temp->prev=trav;
            trav->next=temp;
            nth->prev=temp;
            break;  
        }
   
    }

}

void delete()
{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->next;
        head=temp;
        head->prev=NULL;
        free(temp);break;
    case 2:
        while(temp->next->next!=NULL)
        {
            temp=temp->next;
        }
        last=temp->next;
        temp->next=NULL;
        free(last);break;      
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->next;
            count++;
        }
        temp1=temp->next;
        temp->next=temp1->next;
        temp1->next->prev=temp;
        free(temp1);break;
    }
}

void search()
{
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    while(temp!=NULL)
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;          
        }
        temp=temp->next;
    }
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
int c;
printf("Press 0 : Print in usual order\n");
printf("Press 1 : Print in reverse order\n");
scanf("%d",&c);

if(c==0){
        int count=1;
        printf("\n...................................\n");
        while(temp!=NULL)
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            temp=temp->next;
            count++;
        }
        printf("...................................\n");
    }
else if(c==1){
        int count=1;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        printf("\n...................................\n");
        while(temp!=NULL)
        {
            printf("Student %d : \n",count);
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n",temp->cgpa);
            temp=temp->prev;
            count++;
        }
        printf("...................................\n");
    }
}


Thanking you ! 

Linear circular linked list : C implementation


Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.


//single circular linked list
//assignment 4

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *link;
};

struct node *head=NULL;

void insert();
void delete();
void search();
void display();

int main()
{
int choice;
struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav;
trav=head;
int n,count=0;

    printf("\nEnetr your choice :\n");
    printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
    scanf("%d",&choice);
    //getting the data
    printf("Enter name :\n");
    scanf("%s",temp->name);
    printf("Enter roll number :\n");
    scanf("%d",&(temp->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(temp->cgpa));
   
    if(head==NULL)
            {
              
                temp->link=temp;
                head=temp;
            }
    else{
   
        switch(choice)
        {
        case 1:
                while((trav->link)!=head)
                {
                    trav=trav->link;
                }
                trav->link=temp;
                temp->link=head;
                head=temp;break;  
        case 2:
            while(trav->link!=head)
            {
                trav=trav->link;
            }  
            trav->link=temp;
            temp->link=head;break;  
        case 3:
          
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->link;
                count++;
            }
            temp->link=trav->link;
            trav->link=temp;break;  
        }
   
    }

}

void delete()
{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        if (head==NULL)
        {
            printf("List is empty\n");
        }
        else
        {
        temp1=head;
        while(temp1->link!=head)
        {
            temp1=temp1->link;
        }
        temp1->link=head->link;
        head=head->link;
        } break;
    case 2:
        if (head==NULL)
        {
            printf("List is empty\n");  
        }
        else
        {
            while((temp->link->link)!=head)
            {
                temp=temp->link;
            }
            temp->link=head;
        }break;      
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->link;
            count++;
        }
        temp1=temp->link;
        temp->link=temp1->link;
        free(temp1);break;
    }
}

void search()
{
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    do
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;          
        }
        temp=temp->link;
    }while(temp!=head);
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
    int count=1;
    printf("\n...................................\n");
    do
    {
        printf("Student %d : \n",count);
        printf("Name : %s\n",temp->name);
        printf("Roll Number : %d\n",temp->roll_no);
        printf("CGPA : %.2f\n\n",temp->cgpa);
        temp=temp->link;
        count++;
    }while(temp!=head);
    printf("...................................\n");
}


Thanking you !

Linear ordered linked list : C implementation


Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.



//ordered linked list
//assignment 4

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *link;
};

struct node *head=NULL;

void orderedInsertion();
void delete();
void search();
void display();

int main()
{
int choice;
struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                orderedInsertion();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void orderedInsertion()
{
    struct node* current;
    struct node* new_node;
    new_node=(struct node *)malloc(sizeof(struct node));
  
        printf("Enter name :\n");
    scanf("%s",new_node->name);
    printf("Enter roll number :\n");
    scanf("%d",&(new_node->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(new_node->cgpa));
    /* Special case for the head end */
    if (head == NULL || head->roll_no >= new_node->roll_no)
    {
        new_node->link = head;;
        head = new_node;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = head;
        while (current->link!=NULL &&
               current->link->roll_no < new_node->roll_no)
        {
            current = current->link;
        }
        new_node->link = current->link;
        current->link = new_node;
    }
}

void delete()
{
    if(head==NULL)
    {
    printf("List is empty\n");
    return;
    }
    else{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->link;
        head=temp;
        free(temp);break;
    case 2:
        while(temp->link->link!=NULL)
        {
            temp=temp->link;
        }
        last=temp->link;
        temp->link=NULL;
        free(last);break;      
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->link;
            count++;
        }
        temp1=temp->link;
        temp->link=temp1->link;
        free(temp1);break;
    }
    }
}

void search()
{  
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    while(temp!=NULL)
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;          
        }
        temp=temp->link;
    }
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
    int count=1;
    printf("\n...................................\n");
    while(temp!=NULL)
    {
        printf("Student %d : \n",count);
        printf("Name : %s\n",temp->name);
        printf("Roll Number : %d\n",temp->roll_no);
        printf("CGPA : %.2f\n\n",temp->cgpa);
        temp=temp->link;
        count++;
    }
    printf("...................................\n");
}



Thanking you !

Linear linked list : C implementation


Write menu driven program for the followings:

a) Linear linked list : Insertion, Deletion, Search, Display.
b) Linear ordered linked list : Insertion, Deletion, Search, Display.
c) Linear circular linked list : Insertion, Deletion, Search, Display.
d) Linear doubly linked list : Insertion, Deletion, Search, Display.
e) Linear doubly circular linked list : Insertion, Deletion, Search, Display.

Note :
1) Insertion and Deletion ( At first node, At last node, At inbetween nodes)
2) Each node in linked list contains Student's Roll_No, Name, CGPA.


//singly linked list
//assignment 4

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
char name[20];
int roll_no;
float cgpa;
struct node *link;
};

struct node *head=NULL;

void insert();
void delete();
void search();
void display();

int main()
{
int choice;
struct node *head=NULL;
while(1){
    printf("Enter your choice:\n");
    printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                insert();break;
            case 2:
                delete();break;
            case 3:
                search();break;
            case 4:
                display();break;
            case 5:
                return 0;
            default :
                printf("Try valid input\n");
        }
    }
}

void insert()
{
int choice;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); // allocating memory in heap
struct node *trav;
trav=head;
int n,count=0;

    printf("\nEnetr your choice :\n");
    printf("1.Insert at beginning \n2.Insert at end \n3.Insert at nth position\n");
    scanf("%d",&choice);
    //getting the data
    printf("Enter name :\n");
    scanf("%s",temp->name);
    printf("Enter roll number :\n");
    scanf("%d",&(temp->roll_no));
    printf("Enter CGPA :\n");
    scanf("%f",&(temp->cgpa));
   
    if(head==NULL)
    {
        temp->link==NULL;
        head=temp;
    }
    else{
   
        switch(choice)
        {
        case 1:
            temp->link=head;
            head=temp;break;   
        case 2:
            while(trav->link!=NULL)
            {
                trav=trav->link;
            }   
            trav->link=temp;
            temp->link=NULL;break;   
        case 3:
           
            printf("Enter position :\n");
            scanf("%d",&n);
            while(count<n-2)
            {
                trav=trav->link;
                count++;
            }
            temp->link=trav->link;
            trav->link=temp;break;   
        }
   
    }

}

void delete()
{
    if(head==NULL)
    {
    printf("List is empty\n");
    return;
    }
    else{
    int choice;
    printf("Enter your choice :\n");
    printf("1.Delete 1st node\n2.Delete last node\n3.Delete nth node\n");
    scanf("%d",&choice);
    int n,count=0;
    struct node *temp,*temp1;
    struct node *last;
    temp=head;
    switch(choice)
    {
    case 1:
        temp=temp->link;
        head=temp;
        free(temp);break;
    case 2:
        while(temp->link->link!=NULL)
        {
            temp=temp->link;
        }
        last=temp->link;
        temp->link=NULL;
        free(last);break;       
    case 3:
        printf("Enter n :\n");
        scanf("%d",&n);
        while(count<n-2)
        {
            temp=temp->link;
            count++;
        }
        temp1=temp->link;
        temp->link=temp1->link;
        free(temp1);break;
    }
    }
}

void search()
{   
    struct node *temp;
    temp=head;
    char name[20];
    printf("Search : Name ?\n");
    scanf("%s",name);
    while(temp!=NULL)
    {
        if(strcmp(name,temp->name)==0)
        {
            printf("Here is the required data :\n");
            printf("Name : %s\n",temp->name);
            printf("Roll Number : %d\n",temp->roll_no);
            printf("CGPA : %.2f\n\n",temp->cgpa);
            return;           
        }
        temp=temp->link;
    }
    printf("(X) Sorry , NO MATCHES FOUND !\n");
}

void display()
{
struct node *temp;
temp=head;
    int count=1;
    printf("\n...................................\n");
    while(temp!=NULL)
    {
        printf("Student %d : \n",count);
        printf("Name : %s\n",temp->name);
        printf("Roll Number : %d\n",temp->roll_no);
        printf("CGPA : %.2f\n\n",temp->cgpa);
        temp=temp->link;
        count++;
    }
    printf("...................................\n");
}




Thanking you !

Saturday, 6 August 2016

Stack implementation using C - II .

Implement the stack operations for the stack of books, 
for the followings:(i.e.: Also check the underflow & 
overflow conditions)
Note : 1.Implement using array of structure only.
 2.Each book contains book_name,book_id and book_price.

1. Push 5 book informations.
2. Pop 2 book information.
3. Peep the book.4. Display all books from stack.







 #include<stdio.h>
#include<stdlib.h>

#define max_size 5

//defining a library of books
struct book{
char book_name[50];
int book_id;
float book_price;
};

int top=-1;
void push(struct book *);//function for inserting value at the top
void pop(struct book *);//function for deleting the value from the top
void peep(struct book *);//function for accessing top element
void display(struct book *);//function for displaying all books.

void main()
{
struct book stack[max_size];
int choice;
while(1){

    printf("\tEnter your choice :\n");
    printf("\t\t1.Push\n");
    printf("\t\t2.Pop\n");
    printf("\t\t3.Top element\n");
    printf("\t\t4.Display all books\n");
    printf("\t\t5.Exit\n");

    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        if(top==max_size-1)
        printf("Error : Stack is full \n");
        else
        {
        printf("\tEnter book \n\t1.Name\n\t2.Book id\n\t3.Book price\n");
        push(stack);
        }break;

    case 2:
        if(top==-1)
        printf("Error : The library is empty\n");
        else{
        pop(stack);
        }break;
    case 3:
        if(top==-1)
        printf("Error : The library is empty\n");
        else{
        peep(stack);
        }break;
    case 4:
        display(stack);break;
    case 5:
        printf("\nThank you !\n");
        return ;
    }
}

}

void push(struct book *s)
{  
    top++;
    scanf("%s",s[top].book_name);scanf("%d",&s[top].book_id);scanf("%f",&s[top].book_price);  
}

void pop(struct book *s)
{  
    printf("\nthe book poped is");
    printf("\nname:%s",s[top].book_name);
    printf("\nid:%d",s[top].book_id);
    printf("\nprice:%.2f\n",s[top].book_price);
    top--;
}

void peep(struct book *s)
{  
    printf("\nBook at top:");
    printf("\nname:%s",s[top].book_name);
    printf("\nid:%d",s[top].book_id);
    printf("\nprice:%.2f\n",s[top].book_price);  
}

void display(struct book *s)
{
    int i;
    if(top==-1)
        printf("\nStack is empty \n");
    else{
    for(i=0;i<=top;i++)
    {
        printf("\nname:%s",s[i].book_name);
        printf("\nid:%d",s[i].book_id);
        printf("\nprice:%.2f\n",s[i].book_price);
    }
    }
}





Thanking you !

Sunday, 31 July 2016

String operations using a header file .

Hello Friends!
If there are several functions alike or we can sort them together for that we can create our own header file with extension '.h' and include them in our program as #include"SAMPLE.h" .
So, here are some simple operations performed on the string using that source header file .


//Q1.
// check length

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];

printf("Enter the string :\n");
gets(str);
int l;
l=length(str);
printf("Length of string is : %d\n",l);
}

//Q2
// Concatenation of two different strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
printf("Enter the string 1:\n");
gets(str1);
printf("Enter the string 2:\n");
gets(str2);

conc(str1,str2);
}


//Q3.
//position and occurence of a given symbol in string

#include<stdio.h>

#include"STRING.h"

void main()
{
char str[50],character;
printf("Enter string:\n");
gets(str);
printf("Enter character:\n");
scanf("%c",&character);
pos(str,&character);
}

//Q4
//retriving the last occurence

#include<stdio.h>

#include"STRING.h"

void main()
{
char str[50],character;
printf("Enter string:\n");
gets(str);
printf("Enter character:\n");
scanf("%c",&character);
lastpos(str,&character);
}

//Q5
//retriving the position of substring in main string.

#include<stdio.h>
#include"STRING.h"

void main()
{
char str[50],sub[50];
int position;
printf("Enter string:\n");
gets(str);
printf("Enter substring :\n");
gets(sub);
position=substring(str,sub);
printf("The position of substring is :\t%d\n",position);
}

//Q6
//Replace the given substring in the main string from the given position

#include<stdio.h>
#include"STRING.h"

void main()
{
char str[50],sub[50],rep[50];
int position;
printf("Enter string:\n");
gets(str);
printf("Enter substring to be replaced:\n");
gets(sub);
printf("Enter substring of same lenght:\n");
gets(rep);
position=substring(str,sub);
replace(str,rep,position);
}

//Q7.
//compare strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
int ans=0;
printf("Enter two strings to compare :\n");
gets(str1);
gets(str2);
ans = comp(str1,str2);
if(ans)
    printf("the strings are equal\n");
else
    printf("the strings are not equal\n");
}

//Q8.
//compare strings without case sensitivity

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
int ans=0;
printf("Enter two strings to compare :\n");
gets(str1);
gets(str2);
ans = compare(str1,str2);
if(ans)
    printf("the strings are equal\n");
else
    printf("the strings are not equal\n");
}

// q9.
//reverse string

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
reverse(str);
}

//Q. 10
// upper case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
up(str);
}

// Q.11
// lower case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
low(str);
}

//Q.12
// Convert the string to Title Case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
title(str);
}

//Q 13
//Concatenation of two different strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
printf("Enter the string to duplicate and concatenate :\n");
gets(str);
dupconc(str);
}

// Q. 14
// concatenate the reversal of the string
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
printf("Enter the string :\n");
gets(str);
revconc(str);
}

// Q15
// check palindrome
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
int ans=0;
printf("enter string :\nFor checking it is palindrome or not?\n");
gets(str);
ans = check(str);
if(ans)
    printf("Yes ! Its a palindrome\n");
else
    printf("Sorry!\n");

}

// Q.16
// Display and count the vowels present in the string
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
vow(str);
}

//Q17
//Count the characters and words
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
int no_char,no_words;
no_char=countch(str);
no_words=countw(str);
printf("The number of:\nCharacters : %d\nWords : %d\n",no_char,no_words);
}


Header file : 



// A header file "STRING.h"
// consisting functions
// regarding to string operations .

// 1.length of a string
int length(char *a)
{
int i;
for(i=0;*(a+i)!='\0';i++);
return i;
}

//2. concatenate two strings
void conc(char *a,char *b)
{
int i,j,l;
char c[100];
for(i=0;*(a+i)!='\0';i++)
{
    c[i]= *(a+i);
}
for(j=0;*(b+j)!='\0';j++)
{
    c[i+j]=*(b+j);
}
c[j+i]='\0';
printf("The concatenated string is :\n%s\n",c);
}

//3. position and occurence of the given character in string.

void pos(char *a,char *character)
{
int i,j,position,count=0;
for(i=0;*(a+i)!=*character;i++);
position = i+1;
for(j=0;*(a+j)!='\0';j++)
{
    if(*character == *(a+j))
        count++;
}
printf("The first position is %d\nOccurence is %d\n",position,count);

}

//4. last occurence

void lastpos(char *a,char *character)
{
int i,j,position,count=0;
for(i=0;*(a+i)!='\0';i++)
{
    if( *(a+i)==*character)
        position = i+1;
}
for(j=0;*(a+j)!='\0';j++)
{
    if(*character == *(a+j))
        count++;
}
printf("The last position is %d\nOccurence is %d\n",position,count);

}

//5. position of substring

//retriving the position of substring in main string.

int substring(char *a,char *sub)
{
int i=0,j=0;

while(a[i]!='\0')
{
    if(a[i]==sub[0])
    {    j=1;
        while(sub[j]!='\0' && a[i+j]!='\0' && a[i+j]==sub[j] )
        {
            j++;
        }
    }
    if(sub[j]=='\0')
        return i+1;
i++;
}

return 0;
}


//6. replacing substring

void replace(char *a,char *rep,int p)
{
int i,j;
for(i= p-1,j=0;rep[j] != '\0';i++,j++)
{
    a[i]=rep[j];
}
printf("The new string is :\n%s\n",a);
}

//7. comparing two strings

int comp(char *a,char *b)
{
int i=0,j=0,k;
while(*(a+i)==*(b+i) && *(a+i)!='\0' && *(b+i)!='\0')
{
    i++;
}
if(*(a+i)=='\0' && *(b+i)=='\0')
    return 1;
else
    return 0;
}

//8. comparing two strings without case sensitivity

int compare(char *s1,char *s2)
{
int i ,j=0;
for( i=0;*(s1+i)!='\0' && *(s2+i)!='\0';i++)
{
    if(s1[i]==s2[i])
        j++;
    else if(s1[i]+32==s2[i])
        j++;
    else if(s1[i]-32==s2[i])
        j++;
    else
        return 0;
}
return 1;
}

//9. Reverse the given string !

void reverse(char *a)
{
int i,j,k;
char r[50];
for(i=0;a[i]!='\0';i++);
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';

printf("the reverse string is :\n%s \n",r);
}

//10. convert the string to upper case

void up(char *a)
{
int i;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]>=97 && a[i]<=123)
        a[i]=a[i]-32;
}
printf("The required string is :\n%s\n",a);
}

//11. convert the string to lower case

void low(char *a)
{
int i;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]>=65 && a[i]<=91)
        a[i]=a[i]+32;
}
printf("The required string is :\n%s\n",a);
}

//12. Convert the string to Title Case

void title(char *a)
{
int i;
if(a[0]>=97 && a[0]<=122)
    a[0]=a[0]-32;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]==' ')
    {
        if(a[i+1]>=97 && a[i+1]<=122)
            a[i+1]=a[i+1]-32;
    }
}
printf("The required string is :\n%s\n",a);
}

// 13.duplicate and concatenate two strings
void dupconc(char *a)
{
int i,j,l;
char dup[50];
for(i=0;*(a+i)!='\0';i++)
{
    dup[i]= *(a+i);
}
dup[i]='\0';
conc(a,dup);
}

// 14. reverse and concatenate two strings
void revconc(char *a)
{
int i,j,k;
char r[50];
for(i=0;a[i]!='\0';i++);
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';
conc(a,r);
}

// 15. check palindrome
int check(char *a)
{
int i,j,k,n,m=0,len;
char r[50];
for(i=0;a[i]!='\0';i++);
len=i;
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';
for(n=0;a[n]!='\0' && r[n]!='\0';n++)
{
    if(a[n]==r[n])
    {
    m++;
    }
}
if(m==len)
    return 1;
else
    return 0;
}

//16. Display and count the vowels present in the string
void vow(char *a)
{
int i,j,count=0;
printf("The vowels are :\n");
for(i=0;a[i]!='\0';i++)
{
    if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U'){
    printf("%c,",a[i]);
    count++;
    }
}
printf("Number of vowels are :\n%d\n",count);
}

//17. count characters and words
//17.1 counting characters
//white space is excluded.
int countch(char *a)
{
int i,j,count=0;
for(i=0;a[i]!='\0';i++)
{
    if(a[i]!=' '){
    count++;
    }
}
return count;
}

//17.2 counting words
//it is assumed that words are seperated by white spaces
int countw(char *a)
{
int i,j,count=1;
for(i=0;a[i]!='\0';i++)
{
    if(a[i]==' '){
    count++;
    }
}
return count;
}


Thanking you !


dealing with substrings !

Hello friends !
Here are some pieces of codes which deal with "substrings",
  • retrieving the position of substring
  • replacing the substring.

//retrieving the position of substring in main string.

#include<stdio.h>
void replace(char *a,char *rep,int p);
int substring();
void main()
{
char str[50],sub[50],rep[50];
int position;
printf("Enter string:\n");
gets(str);
printf("Enter substring :\n");
gets(sub);
position=substring(str,sub);
printf("The starting point of substring is :\t%d\n",position);
printf("Enter the new substring :\n");
gets(rep);
replace(str,rep,position);
}

// for position
int substring(char *a,char *sub)
{
int i=0,j=0;

while(a[i]!='\0')// checking each character of string
{
    if(a[i]==sub[0])// if any character matches the initial of substring
    {    j=1;        // we need to check all character matches or not
        while(sub[j]!='\0' && a[i+j]!='\0' && a[i+j]==sub[j] )
        {
            j++;// iff all charater matches , means this is the required substring
                // it will run every time.
        }
    }
    if(sub[j]=='\0')// in matching case only , this "if" condition is satisfied
        return i+1;
i++;
}

return 0;
}

// function for replacing the given string
// works for same length substring only

void replace(char *a,char *rep,int p)
{
int i,j;
for(i= p-1,j=0;rep[j] != '\0';i++,j++)
{
    a[i]=rep[j];
}
printf("The new string is :\n%s\n",a);
}



Thanking you !

Wednesday, 27 July 2016

C program to find 2nd minimum and 2nd maximum

Hello friends !
This program sounds quite simple but its little confusing rather , the algorithms i had used before were appropriate for some cases only . I guess this program contains no error or if any just let me know .


#include<stdio.h>
#define largest_value 1000
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

max=max2 = array[0];
min=min2= largest_value;
for(i=0;i<n;i++)
{
    if(array[i]<min)
    {
    min=array[i];
    }

    if(array[i]>max)
    {
    temp2=max;
    max=array[i];
    max2=temp2;
    }
}
for(i=0;i<n;i++)
{ if(array[i]<min2 && array[i]!=min)
    min2=array[i]; }

printf("max %d 2nd max %d min %d 2nd min %d \n",max,max2,min,min2);
}

 /*
accessing array by address
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int minmax2();
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minmax2(array,n,&min,&min2,&max,&max2);
}

//function

int minmax2(int *array,int n,int *min,int *min2,int *max,int *max2)
{
    int i,temp1,temp2;
    *max=*max2 = *array;
    *min=*min2= largest_value;
    for(i=0;i<n;i++)
    {
    if(*(array+i)<*min)
    {
    *min=*(array+i);
    }

    if(*(array+i)>*max)
    {
    temp2=*max;
    *max=*(array+i);
    *max2=temp2;
    }
    }
    for(i=0;i<n;i++)
    { if(*(array+i)<*min2 && *(array+i)!=*min)
    *min2=*(array+i); }

    printf("2nd max %d \n2nd min %d \n",*max2,*min2);
}




Thanking you !

Tuesday, 26 July 2016

Checking minimum and maximum of an array using functions : C program

/*
 Write a function which return min and max value from an array
*/
#include<stdio.h>
void minmax();
void main()
{
int i,n,minValue,maxValue;
printf("Enter the size of array:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minValue = array[0];maxValue=array[0];
minmax(array,n,&minValue,&maxValue);
printf("Minimum value is %d and maximum value is %d ",minValue,maxValue);
}

void minmax(int* array,int n,int* minValue,int* maxValue)
{
int i;
for(i=0;i<n;i++)
{
    if(*(array+i) < *minValue )
        *minValue = *(array+i);
    if(*(array+i) > *maxValue )
        *maxValue = *(array+i);
}
}

Sunday, 24 July 2016

Data strucutres : Assignment 1

Hello guys and girls !
This an humble attempt to learn, share, and help !
I hope this will be helpful to you . Please refer this/ check this or if there is error suggest modification/ better algorithms .

Question : 1

/*

Array operation by index
Q.1 Write a program to calculate sum of array and find the average.
*/


#include<stdio.h>

float main()
{
int n,i;
float avg,sum=0.0;
printf("Enetr no of elements:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
    scanf("%d",&array[i]);
    sum += array[i];
}
avg = sum/n;
printf("The average is %4.2f \n",avg);
}


/*
Array operation by address
Q.1 Write a program to calculate sum of array and find the average.
*/


#include<stdio.h>

float main()
{
int n,i;
float avg,sum=0.0;
printf("Enetr no of elements:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
    scanf("%d",array+i);
    sum += *(array+i);
}
avg = sum/n;
printf("The average is %4.2f \n",avg);
}




Question : 2

/*
Array operation by index
Q 2 Array contains age of different person.  write a program to find max age and min age from array.
*/

#include<stdio.h>
void main()
{
int n,i,j,k,maxAge=0,minAge=1000;
printf("Enter the number of person:\n");
scanf("%d",&n);
int age[n];
printf("Enter the age:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&age[i]);

        if( age[i] < minAge )
        {
            minAge = age[i];
        }
        if( age[i] > maxAge )
        {
            maxAge = age[i];
        }
    }
printf("\nMax age : %d \n",maxAge);
printf("Min age : %d \n",minAge);
}

/*
Array operation by address
Q 2 Array contains age of different person.  write a program to find max age and min age from array.
*/

#include<stdio.h>
void main()
{
int n,i,j,k,maxAge=0,minAge=1000;
printf("Enter the number of person:\n");
scanf("%d",&n);
int age[n];
printf("Enter the age:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(age+i));

        if( *(age+i) < minAge )
        {
            minAge = *(age+i);
        }
        if( *(age+i) > maxAge )
        {
            maxAge = *(age+i);
        }
    }
printf("\nMax age : %d \n",maxAge);
printf("Min age : %d \n",minAge);
}







Question : 3

/*
accessing array by index
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

max=max2 = array[0];
min=min2= largest_value;
for(i=0;i<n;i++)
{
    if(array[i]<min)
    {
    min=array[i];
    }

    if(array[i]>max)
    {
    temp2=max;
    max=array[i];
    max2=temp2;
    }
}
for(i=0;i<n;i++)
{ if(array[i]<min2 && array[i]!=min)
    min2=array[i]; }

printf("2nd max %d \n2nd min %d \n",max2,min2);
}

/*
accessing array by address
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int minmax2();
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minmax2(array,n,&min,&min2,&max,&max2);
}

//function

int minmax2(int *array,int n,int *min,int *min2,int *max,int *max2)
{
    int i,temp1,temp2;
    *max=*max2 = *array;
    *min=*min2= largest_value;
    for(i=0;i<n;i++)
    {
    if(*(array+i)<*min)
    {
    *min=*(array+i);
    }

    if(*(array+i)>*max)
    {
    temp2=*max;
    *max=*(array+i);
    *max2=temp2;
    }
    }
    for(i=0;i<n;i++)
    { if(*(array+i)<*min2 && *(array+i)!=*min)
    *min2=*(array+i); }

    printf("2nd max %d \n2nd min %d \n",*max2,*min2);
}


Question : 4

/*
accessing array by index
Q4 Add an element in array at particular index given by user.
*/

#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

//getting the index and value

printf("Enter the index:\n");
scanf("%d",&index);

printf("Enter element:\n");
scanf("%d",&new);

// inserting the element
// shifting elements upper than the index to next
    for(i=n-1;i>=index;i--)
    {
        array[i+1] = array[i];
    }
array[index] = new;
printf("Here is the desired array:\n");
    for(i=0;i<=n;i++)
    {
        printf("array[%d] = %d \n",i,array[i]));
    }
}


/*
accessing array by address
Q4 Add an element in array at particular index given by user.
*/

#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",array+i);
}

//getting the index and value

printf("Enter the index:\n");
scanf("%d",&index);

printf("Enter element:\n");
scanf("%d",&new);

// inserting the element
// shifting elements uper than the index to next
    for(i=n-1;i>=index;i--)
    {
        *(array+i+1) = *(array+i);
    }
*(array+index) = new;
printf("Here is the desired array:\n");
    for(i=0;i<=n;i++)
    {
        printf("array[%d] = %d \n",i,*(array+i)));
    }
}





Question : 5

/*
Accessing array elements by index
Q5 delete an element in an array at particular index given by user
*/
#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

//deleting the element

printf("Enter the index:\n");
scanf("%d",&index);
for(i=index;i<n-1;i++)
{
array[i]=array[i+1];
}
printf("The desired array is:\n");
for(i=0;i<n-1;i++)
{
printf("array[%d] = %d\n",i,array[i]);
}
}


/*
Accessing array elements by address
Q5 delete an element in an array at particular index given by user
*/
#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",array+i);
}

//deleting the element

printf("Enter the index:\n");
scanf("%d",&index);
for(i=index;i<n-1;i++)
{
    *(array+i)=*(array+i+1);
}
printf("The desired array is:\n");
for(i=0;i<n-1;i++)
{
printf("array[%d] = %d\n",i,*(array+i));
}
}






Question : 6

/*
Q6 Write a function which return min and max value from an array
*/
#include<stdio.h>
int min(int *array,int n);
int max(int *array,int n);
void main()
{
int i,n,minValue,maxValue;
printf("Enter the size of array:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minValue = min(array,n);
maxValue = max(array,n);
printf("The min :\t%d\nThe max :\t%d\n",minValue,maxValue);
}

//function for minimum of an array
int min(int *array,int n)
{
int i;
int minimum = *(array);
for(i=0;i<n;i++)
{
    if(*(array+i)<minimum)
    {
    minimum = *(array+i);
    }
}
return minimum;
}


//function for maximum of an array
int max(int *array,int n)
{
int i;
int maximum = 0;
for(i=0;i<n;i++)
{
    if(*(array+i)>maximum)
    {
    maximum = *(array+i);
    }
}
return maximum;
}



Question : 7

/*
Q 7
Write a program to add weight
create a structure for weight which contains Kg and g
*/
#include<stdio.h>
#define conversion 1000
struct weight{
int kg,gram;
};


main()
{
struct weight w1,w2;
int grandKg,grandGram;
printf("Enter weight 1 :\n");
printf("kg :\t");
scanf("%d",&w1.kg);
printf("\ngram :\t");
scanf("%d",&w1.gram);


printf("Enter weight 2 :\n");
printf("kg :\t");
scanf("%d",&w2.kg);
printf("\ngram :\t");
scanf("%d",&w2.gram);

grandKg = w1.kg + w2.kg ;

grandGram = w1.gram + w2.gram;

if(grandGram >= conversion )
{
grandKg += 1;
grandGram -= conversion ;
}

printf("total weight kg: %d gram %d  :\n",grandKg,grandGram);


}


/*
Q 7
Write a program to add weight using pointer .
create a structure for weight which contains Kg and g
*/


#include<stdio.h>

struct weight {
    int kg;
    int grams;
};
typedef    struct weight w;
void  add(w *w1,w *w2);

void main()
{

  
   w w1,w2;
 
  
   printf("enter the weight1 with kg and its grams\n");
   scanf("%d%d",&w1.kg,&w1.grams);
    printf("enter the weight2 with kg and its grams\n");
   scanf("%d%d",&w2.kg,&w2.grams);
  
   add(&w1,&w2);
 
  
  
  
  
}
void  add(w *w1,w *w2)
{
    w p;
    p.kg= w1->kg + w2->kg;
    if((w1->grams)+(w2->grams)>=1000)
    {
        p.grams=(w1->grams)+(w2->grams)-1000;
        p.kg++;
    }
    else
    {
        p.grams=w1->grams+w2->grams;
    }
    printf("the total weight is %dkg %dgrams",p.kg,p.grams);
}







Thanking you !