Академический Документы
Профессиональный Документы
Культура Документы
Q=1.Write a program to create memory for int, char and float variable at a run time.
Ans:
#include<stdio.h>
int main()
{
insert();
return 0;
void insert()
{
int *r;
char *e;
float *h;
r=malloc(sizeof(int)*1);
e=malloc(sizeof(char)*1);
h=malloc(sizeof(float)*1);
scanf("%d",&r);
fflush(stdin);
scanf("%c",&e);
Q=2.C program to read a one dimensional array, print sum of all elements along with inputted
array
elements using Dynamic Memory Allocation.
Ans:-
#include<stdio.h>
int main()
{
int n;
int *a,c=0;
for(int i=0;i<n;i++)
{
printf("Enter the value of a[%d] : ",i);
scanf("%d",a+i);
}
for(int i=0;i<n;i++)
{
c=c+(*a+i);
return 0;
}
Q=3.Develop a structure to represent planets in the solar system. Each planet has the field for the
planets name, its distance from the sun in miles and the number of moon it has. Write a program
to read the data for each planet and store. Also print the name of the planet that has the highest
number of moons.
Ans:-
#include<stdio.h>
struct planet
{
char name[10];
int distance;
int moon;
}P[9];
int main()
{
insert();
return 0;
}
void insert()
{
int i,m,j;
for(i=1;i<9;i++)
{
printf("Enter the name of %d moon \n:",i);
scanf("%s",&P[i].name);
scanf("%d",&P[i].distance);
printf("Enter the number of moon for %d:\n",i);
scanf("%d",&P[i].moon);
for(i=1;i<9;i++)
{
printf("Enter the name of %s moon \n:",P[i].name);
printf("Distance from sun of %s is %d\n",P[i].name,P[i].distance);
m=P[1].moon;
for(int i=1;i<9;i++)
{
if(m<P[i].moon)
{
m=P[i].moon;
j=i;
}
}
printf("for this %s,%d moons",P[j].name,m);
}
Q=4.Write a c program to Add Two Complex Numbers by Passing Structure to a Function.
Ans:-
#include <stdio.h>
struct complex
{
char name[30];
int distance;
int moons;
}S[5];
void insert()
{
int i;
for(i=0;i<5;i++)
{
printf("\nEnter the name of the planet: ");
scanf("%s", S[i].name);
printf("\nEnter the distance of the planet from sun:
");
scanf("%d", &S[i].distance);
printf("\nEnter the number of moons for planet: ");
scanf("%d", &S[i].moons);
}
}
void Display()
{
int i, max_m=0, pos;
for(i=0;i<5;i++)
{
printf("\n%s\t%d\t%d", S[i].name,S[i].distance, S[i].moons);
}
max_m = S[0].moons;
pos=0;
for(i=0;i<5;i++)
{
if(S[i].moons> max_m)
{
max_m = S[i].moons;
pos=i;
}
}
for(i=0;i<5;i++)
{
printf("\n%s\t%d\t%d", S[i].name,S[i].distance, S[i].moons);
}
max_m = S[0].moons;
pos=0;
for(i=0;i<5;i++)
{
if(S[i].moons< max_m)
{
max_m = S[i].moons;
pos=i;
}
}
int main()
{
insert();
Display();
newinsert();
return 0;
Q=5.Write a c program to store Information using Structures with Dynamic memory allocation.
Ans:-
#include <stdio.h>
struct name
{
int a;
char c[30];
}*s;
void insert()
{
int i,n;
printf("Enter n: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter name and anrollment number\n");
scanf("%s%d",&(s+i)->c, &(s+i)->a);
}
}
void Display()
{
int i,n;
printf("Displaying Infromation:\n");
for(i=0;i<n;++i)
{
printf("%s\t%d\t\n",(s+i)->c,(s+i)->a);
}
}
int main()
{
insert();
Display();
int main()
{
insert();
return 0;
}
void insert()
{
int n,j;
int ele[n];
for(int i=0;i<n;i++)
{
printf("element %d =",i);
scanf("%d",&ele[i]);
}
int temp;
for(int i=0;i<n;i++)
{
int k=ele[i];
for( j=i-1 ;j>=0 && k<ele[j];j--)
{
ele[j+1]=ele[j];
}
ele[j+1]=k;
for(int i=0;i<n;i++)
{
printf("Value of is ele[%d] = %d" ,i,ele[i]);
printf("\n");
}
}
{
insert();
return 0;
void insert()
{
int n;
int ele[n];
for(int i=0;i<n;i++)
{
printf("element %d =",i);
scanf("%d",&ele[i]);
}
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(ele[i]>ele[j])
{
temp=ele[j];
ele[j]=ele[i];
ele[i]=temp;
}
}
}
for(int i=0;i<n;i++)
{
printf("Value of is ele[%d] = %d" ,i,ele[i]);
printf("\n");
}
}
#include<stdio.h>
int main()
{
insert();
return 0;
}
void insert()
{
int n;
int ele[n];
for(int i=0;i<n;i++)
{
}
int temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(ele[j]>ele[j+1])
{
temp=ele[j];
ele[j]=ele[j+1];
ele[j+1]=temp;
}
for(int i=0;i<n;i++)
{
printf("Value of is ele[%d] = %d" ,i,ele[i]);
printf("\n");
}
#include<stdio.h>
int main()
{
insert();
return 0;
void insert()
{
int n,k,f=1;
int ele[n];
for(int i=0;i<n;i++)
{
printf("element %d =",i);
scanf("%d",&ele[i]);
scanf("%d",&k);
for(int i=0;i<n;i++)
{
if(k==ele[i])
{
printf("element is found");
f=0;
}
}
if(f==1)
Ans:-
#include<stdio.h>
int main()
{
insert();
return 0;
}
void insert()
{
int b,n,l,h,flag=0;
float mid;
for(int i=0;i<n;i++)
{
printf("Enter the value of a[%d] : ",i);
scanf("%d",&a[i]);
}
printf("Enter the value for search : ");
scanf("%d",&b);
l=0;
h=n-1;
while(l<=h)
{
mid=(l+h)/2;
if(b<mid)
{
l=0;
h=mid-1;
}
else if(b>mid)
{
l=mid+1;
h=n;
}
else
{
}
if(flag==0)
{
}
Q=11.Write a program to implement stack and perform push, pop operation.
Ans:-
#include<stdio.h>
void push();
void pop();
void display();
int stack[5],top=-1,ele;
int main()
{
push();
push();
push();
push();
push();
pop();
display();
}
void push()
{
if(top==4)
printf("Stack is full");
else
{
top++;
printf("\n enter ele");
scanf("%d",&ele);
stack[top]=ele;
void pop()
if(top==-1)
printf("Stack is empty");
else
{
ele= stack[top];
top--;
}
}
void display()
{
for(int i=top;i>=0;i--)
{
printf("\n the value of top and element, %d ,%d", i , stack[i]);
12. Write a program to perform the following operations in linear queue – Addition, Deletion and
Traversing.
Ans:-
#include<stdio.h>
int Queue[5],ele,rear=-1,front=-1;
void insertion();
void deletion();
void dispaly();
int main()
{
int ch;
do
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
dispaly();
break;
case 4:
break;
default:
printf("Your choice is wrong");
}
}
while(ch != 4);
}
void insertion()
{
if(rear==4)
{
printf("Queue is FULL");
}
else if(front==-1)
{
front=0;
rear=0;
printf("\n Enter the element:");
scanf("%d",&ele);
Queue[rear]=ele;
}
else
{
rear++;
void deletion()
{
if(front==-1 || front==rear+1)
{
printf("Queue is Empty \n");
else
{
ele=Queue[front];
printf("Delete element %d",ele);
front++;
}
}
void dispaly()
{
for(int i=front;i<=rear;i++)
{
printf("element and step %d,%d",i,Queue[i]);
}
13. Write a program to perform the following operations in circular queue – Addition, Deletion,
and
Traversing.
Ans:-
#include<stdio.h>
int Queue[5],ele,rear=-1,front=-1;
void insertion();
void deletion();
void dispaly();
int main()
{
int ch;
do
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
dispaly();
break;
case 4:
break;
default:
printf("Your choice is wrong");
}
}
while(ch != 4);
}
void insertion()
{
if(front==(rear+1)%5)
{
printf("Queue is FULL");
}
else if(front==-1)
{
front=0;
rear=0;
printf("\n Enter the element:");
scanf("%d",&ele);
Queue[rear]=ele;
}
else
{
rear==(rear+1)%5;
void deletion()
{
if(front==-1 || front==rear+1)
{
printf("Queue is Empty \n");
else
{
ele=Queue[front];
printf("Delete element %d",ele);
front=(front+1)%5;
void dispaly()
{
for(int i=front;i<=rear;i++)
{
printf("element and step %d,%d",i,Queue[i]);
}
14. Write a program to perform the following operations in singly linked list – Creation, Insertion,
and
Deletion.
Ans:-
#include<stdio.h>
int main()
{
insert();
Sinsert();
display();
}
struct node
{
int data;
struct node*link;
}*START;
void insert()
{
int n;
struct node*temp;
temp=malloc(sizeof(struct node));
temp->link=NULL;
START=temp;
}
void Sinsert()
{
int m;
struct node*p;
p=malloc(sizeof(struct node));
p->data=m;
p->link=START;
START=p;
void display()
{
struct node*temp;
temp=START;
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
15. Write a program to perform the following operations in doubly linked list – Creation, Insertion,
and
Deletion.
Ans:-
#include<stdio.h>
int main()
{
insert();
Sinsert();
display();
}
struct node
{
int data;
struct node*link;
}*START;
void insert()
{
int n;
struct node*temp;
temp=malloc(sizeof(struct node));
temp->data=n;
temp->link=NULL;
START=temp;
void Sinsert()
{
int m;
struct node*p;
p=malloc(sizeof(struct node));
scanf("%d",&m);
p->data=m;
p->link=START;
START=p;
}
void display()
{
struct node*temp;
temp=START;
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
Ans:- #include<stdio.h>
struct node{
int data;
struct node*link;
}*top;
int main()
{
top==NULL;
int ch;
do
{
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Your choice is wrong");
}
}
while(ch!=4);
return 0;
}
void push()
{
if(top==NULL)
{
int f;
struct node*m;
m=malloc(sizeof(struct node));
printf("Enter data for m node");
scanf("%d",&f);
m->data=f;
m->link=NULL;
top=m;
else
{
int d;
struct node*n;
n=malloc(sizeof(struct node));
printf("Enter data for n node");
scanf("%d",&d);
n->data=d;
n->link=top;
top=n;
void pop()
{
struct node*temp;
if(top==NULL)
{
top=top->link;
printf("deleted node %d",temp->data);
free(temp);
void display()
{
struct node*temp;
temp=top;
if(top==NULL)
{
printf("\n stack is empty");
}
else
{
while(temp!=NULL)
{
printf("\n %d",temp->data);
temp=temp->link;
}
}
}
17. Write programs to implement linked queue.
Ans:-
#include<stdio.h>
struct node{
int data;
struct node*link;
}*rear,*front;
int main()
{
insert();
insert();
deletian();
display();
}
void insert()
{
if(rear==NULL)
{ int d;
struct node*temp;
temp=malloc(sizeof(struct node));
printf("Enter data for frist node:");
scanf("%d",&d);
temp->data=d;
temp->link=NULL;
rear=temp;
front=temp;
}
else
{
int s;
struct node*r;
r=malloc(sizeof(struct node));
printf("Enter data fro second node:");
scanf("%d",&s);
r->data=s;
rear->link=r;
r->link=NULL;
rear=r;
}
}
void deletian()
{
struct node*temp;
if(front==NULL)
{
printf("Queue is Empty");
else
{
temp=front;
front=front->link;
}
}
void display()
{
while(front!=NULL)
{
printf("\n%d\n",front->data);
front=front->link;
}
}
18. Write a program to create a binary tree and perform – Insertion, Deletion, and Traversal.
Deletion.
Ans:-
#include<stdio.h>
int main()
{
insert();
Sinsert();
last();
Inbetween();
display();
}
struct node
{
int data;
struct node*link;
}*START;
void insert()
{
int n;
struct node*temp;
temp=malloc(sizeof(struct node));
temp->data=n;
temp->link=temp;
START=temp;
}
void Sinsert()
{
int m;
struct node*p;
p=malloc(sizeof(struct node));
p->data=m;
p->link=START;
START->link=p;
START=p;
void last()
{
int r;
struct node*g;
struct node*u;
g=malloc(sizeof(struct node));
g=START;
while(g->link!=START)
{
g=g->link;
u=malloc(sizeof(struct node));
g->link=u;
u->link=START;
}
void Inbetween()
{
int num1,pos;
struct node*m;
temp=START;
for(int i=1;i<pos;i++)
{
temp=temp->link;
m=malloc(sizeof(struct node));
m->data=num1;
m->link=temp->link;
temp->link=m;
}
void display()
{
struct node*temp;
temp=START;
do
{
printf("\n%d",temp->data);
temp=temp->link;
}while(temp!=START);