Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Name: G.

Sitaramaswamy RegNo 18BCE0289

1. Print hello world

Pseudocode: 1.Start
2.Display Hello
World 3.End

Code: #include<stdio.h>
int main()
{
printf("Hello world");
}

Output:

2. Perform arithmetic operations (addition, subtraction, multiplication, division, modulus)


Pseudocode:
1. START
2. INITIALIZE a,b
3. a+b
4.a-b
5.a*b
6.a/b
7.print result

Code: #include<stdio.h>
int main()
{
int a,b;
printf(“Enter two numbers\n”);
scanf("%d%d",&a,&b);
printf("%d\n",a+b);
printf("%d\n",a-b);
printf("%d\n",a*b);
printf("%d\n",a/b);
}

Output:

3. Compute area and perimeter of circle


Pseudocode:
1. Start
2. Read radius r
3.a=pi*r*r
4.p=2*pi*r
5.Display a and p
6. End

Code:
#include<stdio.h> void
main()
{
float r; scanf("%f\n",&r);
printf("%f\n",(3.14*r*r));
printf("%f\n",(3.14*2*r));
}

Output:

4. Take your name as input and display.

Pseudocode: 1.Start
2.Read name
3.Display name

Code: #include<stdio.h>
void main()
{
char n[20];
scanf("%s",n);
printf("The name is %s",n);
}

Output:
5. Swap values of 2 variables using temporary variable

Pseudocode: 1.Start
2.Read a,b
3.Declare temporary variable c 4.c=a
5. a=b
6.b=c
7.Display a and b 8.End
Code: #include<stdio.h>
int main()
{
int a,b,c;
scanf("%d",&a);
scanf("%d",&b); c=a;
a=b; b=c;
printf("%d\n",a);
printf("%d\n",b);
}

Output:
6. Swap values of 2 variables without using temporary variable

Pseudocode: 1.Start
2. Read a and b
3. a=a+b
4. b=a-b
5. a=a-b
6.Display a and b
7. End

Code: #include<stdio.h>
int main()
{
int a,b; scanf("%d",&a);
scanf("%d",&b); a=a+b;
b=a-b;
a=a-b; printf("%d\n",a);
printf("%d\n",b);
}

Output:
7. Find greatest among 2 numbers

Pseudocode: 1.Start
2. Rad a and b
3. If a>b Display a
4.Else Display b
5.End

Code: #include<stdio.h>
int main()
{
int a,b; scanf("%d",&a);
scanf("%d",&b); if(a>b)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
}

Output:
8. Take n numbers and search element as input. Search for an element and print its

position Pseudocode:

1. Start
2. Read n
3.Create an array
4.Read n elements into the array 5.Read
element to be searched.
6.If element is present in array, print present 7.Else
print not present.
8. End

Code: #include<stdio.h>
void main()
{
int array[100],search,c,n;
printf(“Enter number of elements:\n”);
scanf(“%d”,&n);
printf(“Enter %d elements\n”,n);
for(c=0;c<n;c++)
scanf(“%d”,&array[c]);
printf(“Enter element to search\n”);
scanf(“%d”,&search);
for(c=0;c<n;c++)
{
if(array[c]==search)
{
printf(“%d is present at location %d\n”,search,c+1); break;
}
}
if(c==n)
printf(“%d isn't present in the array\n”,search);
}

Output:

9. Take n numbers as input and find maximum and minimum element. (Do not use
sorting technique)

Pseudocode: 1.Start
2.Read number of elements n 3.Read
the array of n elemnts
4.Find and display maximum element 5.Find
and display minimum element 6.End

Code: #include<stdio.h>
void mian()
{
int arr[100];
int i,max,min,size; printf(“Enter size of
array\n”); scanf(“%d”,&size);
printf(“Enter the elements of array\n”);
for(i=0;i<size;i++)
scanf(“%d”,&arr[i]);
max=arr[0];min=arr[0];
for(i=1;i<size;i++)
{
if(arr[i]>max)
max=arr[i]
if(arr[i]<min)
min=arr[i];
}
printf(“Maximum element is %d\n”,max);
printf(“Minimum element is %d\n”,min);
}

Output:

10. Implement menu-driven program to perform arithmetic operations (addition, subtraction,


multiplication, division, modulus)
Name: G.Sitaramaswamy RegNo:18BCE0289

1. Menu-driven program to implement stack data structure.

Pseudocode:

1.Start 2.push(item)
3.if top==name print OVERFLOW 4.else
top=top+1
5.stack[top]=item 6.pop()
7.If top==0 print UNDERFLOW 8.else
item=stack[top]
9.top=top-1 10.item
11.display()
12.print stack

Code:

#include<stdio.h>
#include<conio.h>
#define MAX 10
void push(int a);
void pop();
void display();
int stack[MAX],item,top,i; void
push(int a)
{
if (top==MAX)
printf("OVERFLOW");
else
{
s
t
} a
} c
k
[
t
o
p
]
=
a
;

t
o
p
=
t
o
p
+
1
;
void pop()
{
if (top==0)
printf("UNDERFLOW");
else
{
printf("\nRemoved item is %d\n",stack[top-1]);
top=top-1;
}
}
void display()
{
for(i=top-1;i>=0;i--)
{
printf("\n%d\n",stack[i]);
}
}
int main()
{
int ch,element; top=0;
do
{
printf("\n1.Push");
printf("\n2.Pop"); printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter insertion data: ");
scanf("%d",&element);
push(element);
}
if(ch==2) pop();
if(ch==3)
display();
}
while(ch!=4); return
0;
}

Output:

1. Showing the push opeartion.


2. Showing pop operation

3. Showing display operation


2.Menu driven program to implement queue data structure

Pseudocode:

1.Start 2.enq(item)
3.if rear==MAX 4.print
OVERFLOW 5.else
6.queue[rear]=item
7.rear=rear+1
8.if front=0
9.front=1
10.deq()
11.if front=0 print UNDERFLOW 12.if
front=rear, then front=0,rear=0
13.else front=front+1

Code:

#include<stdio.h>
#include<conio.h>
#define MAX 10
int front,rear,queue[MAX],item,i; void
enq(int item)
{
if(rear==MAX)
printf("OVERFLOW");
else
{
rear=rear+1;
queue[rear]=item;
if(front==0)
front=1;
}
}
void deq()
{
if(front==0)
printf("\nUNDERFLOW");
else
{
printf("\nThe removed item is %d",queue[front]);
if(front==rear)
{
front=0; rear=0;
}
else
front=front+1;
}
}
void display()
{
for(i=front;i<=rear;i++)
printf("%d ",queue[i]);
}
int main()
{
int ch,element,front=0,rear=0; do
{
printf("\n1.Enqueue");
printf("\n2.Dequeue");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter insertion data: ");
scanf("%d",&element);
enq(element);
}
if(ch==2) deq();
if(ch==3)
display();
}
while(ch!=4); return
0;
}
Output:

1. To show enqueue operation

2. To show dequeue operation

3. To show display operation

You might also like