C Programs(1 5)

Embed Size (px)

Citation preview

  • 8/2/2019 C Programs(1 5)

    1/15

    Q.1) Write a C program using data structure to accept the details of employee

    from user and display it on the screen using Dynamic Memory Allocation.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include

    #include

    struct employee

    {

    char ename[20];

    int eid;

    };

    struct node

    {

    struct employee e;

    struct node *next;

    };

    typedef struct node node;

    node *head=NULL,*temp;

    void create();

    void display();

    void exit();

    void main()

    {

    int ch;

    while(1)

  • 8/2/2019 C Programs(1 5)

    2/15

    {

    clrscr();

    printf("\nMENU\n\n");

    printf("1.New Employee Details\n");

    printf("2.Display the details\n");

    printf("3.Exit\n\n");

    printf("enter your choice:");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1: create();

    break;

    case 2: display();

    getch();

    break;

    case 3: exit(0);

    default: printf("\n\nPlease enter a right option.......");

    getch();

    }

    }

    }

    void create()

    {

  • 8/2/2019 C Programs(1 5)

    3/15

    char ch,name[20];

    int id;

    do

    {

    printf("\nEnter id of employee: \n");

    scanf("%d",&id);

    printf("\nEnter name of employee: \n");

    fflush(stdin);

    gets(name);

    if(head==NULL)

    {

    head=(node *)malloc(sizeof(node));

    head->next=NULL;

    strcpy(head->e.ename,name);

    head->e.eid=id;

    temp=head;

    }

    else

    {

    temp->next=(node *)malloc(sizeof(node));

    temp=temp->next;

    temp->next=NULL;

    strcpy(temp->e.ename,name);

    temp->e.eid=id;

  • 8/2/2019 C Programs(1 5)

    4/15

    }

    printf("\n\nDo you want to enter new data(Yes/No): \n");

    fflush(stdin);

    scanf("%c",&ch);

    }while(ch=='y'||ch=='Y');

    }

    void display()

    {

    temp=head;

    printf("\n\t Employee Details\n");

    printf("\nEmployee Id\tEmployee Name\n");

    while(temp!=NULL)

    {

    printf("%d\t\t%s",temp->e.eid,temp->e.ename);

    printf("\n\n");

    temp=temp->next;

    }

    }

    Q.2. Write a C program to reverse a string using static implementation of stack.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include

  • 8/2/2019 C Programs(1 5)

    5/15

    #include

    #define MAX 20

    char stack[MAX],ch;

    int top;

    void init()

    {

    top=-1;

    }

    void push(char ch)

    {

    if(top==MAX-1)

    printf("\n\tSTACK OVERFLOW\n");

    else

    stack[++top]=ch;

    }

    char pop()

    {

    if(top==-1)

    {

    printf("\n\tSTACK UNDERFLOW\n");

    exit(0);

    }

    else

    return stack[top--];

    }

  • 8/2/2019 C Programs(1 5)

    6/15

    void main()

    {

    char str_s[20],str_t[20];

    int i;

    clrscr();

    init();

    printf("\n\tEnter string : ");

    gets(str_s);

    i=0;

    while(i

  • 8/2/2019 C Programs(1 5)

    7/15

    OUTPUT :

    -----------------------------------------------------------

    Enter string : shruti deshpande

    Reverse string = ednaphsed iturhs

    3. Write a C program to evaluate a given polynomial using function. (Use array)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include

    #include

    #include

    void add();

    void evl();

    int a[10],b[10],c[10],i,m,n,cnt=0,ch,k;

    void main()

    {

    clrscr();

    for(i=0;i

  • 8/2/2019 C Programs(1 5)

    8/15

    for(i=m;i>=0;i--)

    {

    scanf("%d",&a[i]);

    }

    printf("\nEnter the order of Second Polynomail");

    scanf("%d",&n);

    printf("\nEnter the Co-efficient");

    for(i=n;i>=0;i--)

    {

    scanf("%d",&b[i]);

    }

    do

    {

    printf("\n1:addition of 2 polynomial");

    printf("\n2:Evaluation of accepted polynomial");

    printf("\n3:exit");

    printf("Enter ur choice:");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:add();

    break;

    case 2:evl();

    break;

    case 3:exit(0);

  • 8/2/2019 C Programs(1 5)

    9/15

    }

    }while(ch!=3);

    getch();

    }

    void add()

    {

    k=m>n ? m:n;

    {

    for(i=m;i>=0;i--)

    {

    c[i]=a[i]+b[i];

    cnt++;

    }

    }

    printf("\n\nRESULTANT POLYNOMIAL IS :A:=");

    for(i=cnt-1;i>0;i--)

    {

    printf("%dX^%d+",c[i],i);

    }

    printf("%d",c[i]);

    }

    void evl()

    {

    int sum=0,x;

  • 8/2/2019 C Programs(1 5)

    10/15

    k=m>n ? m:n;

    {

    for(i=m;i>=0;i--)

    {

    c[i]=a[i]+b[i];

    cnt++;

    }

    }

    printf("Enter value of X:");

    scanf("%d",&x);

    printf("\n%d",cnt);

    for(i=cnt;i>=0;i--)

    {

    sum=sum+c[i]*pow(x,i);

    }

    printf("\nEvaluation :%d",sum);

    }

    -----------------------------OUTPUT-------------------------------

    Enter the order of first Polynomial 3

    Enter the Co-efficient 2

    1

    0

    3

  • 8/2/2019 C Programs(1 5)

    11/15

    Enter the order of Second Polynomail 2

    Enter the Co-efficient 1

    2

    3

    1:addition of 2 polynomial

    2:Evaluation of accepted polynomial

    3:exitEnter ur choice:1

    RESULTANT POLYNOMIAL IS :A:=2X^3+2X^2+2X^1+6

    1:addition of 2 polynomial

    2:Evaluation of accepted polynomial

    3:exitEnter ur choice:2

    Enter value of X:2

    8

    Evaluation :34

    1:addition of 2 polynomial

    2:Evaluation of accepted polynomial

    3:exitEnter ur choice:3

  • 8/2/2019 C Programs(1 5)

    12/15

    4. Write a C program for implementing linear search method using function.

    ----------------------------------------------------------------------------------------------

    #include

    #include

    int search(int [],int,int);void main()

    {

    int a[10],flag;int i,size,n;

    clrscr();

    printf("Enter limit of array : ");scanf("%d",&size);

    printf("\nEnter the array : ");for(i=0;i

  • 8/2/2019 C Programs(1 5)

    13/15

    }

    }return(flag);

    }

    OUTPUT :

    Enter limit of array : 5

    Enter the array :

    value at 0 : 11value at 1 : 22

    value at 2 : 33

    value at 3 : 44

    value at 4 : 55

    Enter the number to search : 11

    Number found

    5. Write a C program to find given element into the array list using recursive Binary Search

    Method.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include

    #include

    void main()

    {

    int a[10],num,i,ele,pos;

    clrscr();

    printf("\n Enter the limit:");

  • 8/2/2019 C Programs(1 5)

    14/15

    scanf("%d",&num);

    printf("\n Enter the elements :\n");

    for(i=0;i

  • 8/2/2019 C Programs(1 5)

    15/15

    }

    return-1;

    }