Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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<stdio.h> #include<conio.h> 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)

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

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;

} 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<stdio.h>

#include<conio.h> #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--]; }

void main() { char str_s[20],str_t[20]; int i; clrscr(); init(); printf("\n\tEnter string : "); gets(str_s);

i=0; while(i<strlen(str_s)) { push(str_s[i]); i++; } i=0; while(top!=-1) { str_t[i++]=pop(); } str_t[i]='\0'; printf("\n\tReverse string = %s",str_t); getch(); }

OUTPUT : ----------------------------------------------------------Enter string : shruti deshpande Reverse string = ednaphsed iturhs

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

#include<stdio.h> #include<conio.h> #include<math.h> 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<=9;i++) a[i]=0; for(i=0;i<=9;i++) b[i]=0; printf("\nEnter the order of first Polynomial"); scanf("%d",&m); printf("\nEnter the Co-efficient");

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

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

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

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

4. Write a C program for implementing linear search method using function. ---------------------------------------------------------------------------------------------#include<stdio.h> #include<conio.h> 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<size;i++) { printf("\tvalue at %d : ",i); scanf("%d",&a[i]); } printf("\nEnter the number to search : "); scanf("%d",&n); flag=search(a,size,n); if(flag==1) { printf("\nNumber found"); } else { printf("\nNumber not found"); } getch(); } int search(int a[10],int size,int n) { int i,flag=0; for(i=0;i<size;i++) { if(a[i]==n) { flag=1; break;

} } return(flag); }

OUTPUT : Enter limit of array : 5 Enter the array : value at 0 : 11 value 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<stdio.h> #include<conio.h> void main() { int a[10],num,i,ele,pos; clrscr(); printf("\n Enter the limit:");

scanf("%d",&num); printf("\n Enter the elements :\n"); for(i=0;i<num;i++) scanf("%d",&a[i]); printf("\n\nEnter the element you want to search:\n\n"); scanf("%d",&ele); pos=b_search_recursive(a,0,num,ele); if(pos==-1) printf("Element is not found"); else printf("Element is found at %d position",pos); getch(); } int b_search_recursive(int a[],int start,int end,int i) { int mid; if(start<=end) { mid=(start+end)/2; if(a[mid]==i) return mid; else if(a[mid]>i) return b_search_recursive(a,start,mid-1,i); else return b_search_recursive(a,mid+1,end,i);

} return-1; }

You might also like