Slip 2_1 : Evalution of polynomial
Program Code :
#include<stdio.h>
#include<conio.h>
#include<math.h>
int eval(int a[],int n,int x) {
int i;
double p,sum=0;
for(i=n;i>=0;i--)
{
p=pow(x,i);
sum=sum+a[i]*p;
}
return sum;
}
void main() {
int a[10],n,c,i,e,x;
printf("Enter the Degree of polynomial : ");
scanf("%d",&n);
printf("\n Enter the coefficient : ");
for(i=n;i>=0;i--)
{
printf("\n Enter coefficient A[%d]",i);
scanf("%d",&a[i]);
}
printf("\n Enter the polynomial : ");
for(i=n;i>=0;i--)
{
if(a[i]!=0)
printf("%dX^%d + ",a[i],i);
}
printf("%d",a[i]);
printf("\n Enter the value for X ");
scanf("%d",&x);
e=eval(a,n,x);
printf("\n Evalution of polynomaial is = %d",e);
getch();
}
Output :
Slip2_2 : Dynamic implemenataion of stack
Program Code:
#include<conio.h>
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *top;
void init()
{
top=NULL;
}
int isEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}
void push(int n)
{
struct node *nw;
nw = (struct node*)malloc(sizeof(struct node));
nw->data = n;
nw->next=top;
top=nw;
}
int pop()
{
struct node *temp;
temp=top;
if(isEmpty())
printf("Stack is empty");
else
{
int n;
n=top->data;
top=top->next;
free(temp);
return n;
}
}
void display()
{
struct node *t;
for(t=top;t!=NULL;t=t->next)
printf("%d\t",t->data);
}
main()
{
int ch,n;
init();
do
{
printf("\n 1.push \n 2.pop \n 3.display \n 4.exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter data ");
scanf("%d",&n);
push(n);
break;
case 2: printf("\n TOS is : ");
printf("\t %d",pop());
break;
case 3: printf("\n Stack elements are : ");
display();
break;
case 4:break;
}
}while(ch!=4);
}
Output:
DS slip 2
Reviewed by Dinesh Varal
on
August 01, 2018
Rating:
No comments: