0% found this document useful (0 votes)
55 views27 pages

Practical-1 Create A Program To Find Out The Interest: Chandigarh Engineering College, Landran

The document contains 10 programming assignments on different sorting and array manipulation techniques: 1. A program to find interest given rate, principal, time. 2. A program to check if a number is prime. 3. A program to insert an element into an array. 4. A program to delete an element from an array. 5. A program with menu driven options to insert, delete, search and traverse arrays using switch case. 6. A program to sort an array using bubble sort. 7. A program to sort an array using selection sort. 8. A program to sort an array using insertion sort. 9. A program to sort an array using merge sort. 10

Uploaded by

Hemant Marve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views27 pages

Practical-1 Create A Program To Find Out The Interest: Chandigarh Engineering College, Landran

The document contains 10 programming assignments on different sorting and array manipulation techniques: 1. A program to find interest given rate, principal, time. 2. A program to check if a number is prime. 3. A program to insert an element into an array. 4. A program to delete an element from an array. 5. A program with menu driven options to insert, delete, search and traverse arrays using switch case. 6. A program to sort an array using bubble sort. 7. A program to sort an array using selection sort. 8. A program to sort an array using insertion sort. 9. A program to sort an array using merge sort. 10

Uploaded by

Hemant Marve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Chandigarh Engineering College, Landran

PRACTICAL-1

Create a program to find out the interest


#include<iostream.h>
#include<conio.h>
void main()
{
int interest,rate,principle,time;
cout<<"enter rate,principle,time=";
cin>>rate>>principle>>time;
interest=(rate*principle*time)/100;
cout<<"interest="<<interest;
getch();
}

Output:

Roll No. 1703277 Page 1


Chandigarh Engineering College, Landran

PRACTICAL-2

Create a program to find out number is prime or not.

#include<iostream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a,flag;
cout<<"enter a number=";
cin>>a;
for(i=2;i<a;i++)
{
if(a%i==0)
{flag=1;}
}
if(flag==1)
{cout<<"not a prime number";}
else
cout<<"Prime number";
getch();
}

OUTPUT:

Roll No. 1703277 Page 2


Chandigarh Engineering College, Landran

PRACTICAL-3

Create a program to insert an element between an elements


of arrray.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,a[20],item,position;
cout<<"enter a no. of elements";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter new element u want to insert";
cin>>item;
cout<<"enter position where u want to insert element";
cin>>position;
for(i=n-1;i>=position-1;i--)
{
a[i+1]=a[i];}
a[position-1]=item;
for(i=0;i<n+1;i++)
{
cout<<a[i];
}
getch();
}

Roll No. 1703277 Page 3


Chandigarh Engineering College, Landran

OUTPUT:

Roll No. 1703277 Page 4


Chandigarh Engineering College, Landran

PRACTICAL-4

Create a program to delete an element from an array.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,a[7],position;
cout<<"enter an elements";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"enter position where u want to delete element";
cin>>position;
for(i=position;i<=n;i++)
{
a[i]=a[i+1];}
n=n-1;
for(i=1;i<=n;i++)
{cout<<a[i];
}
getch();
}

Roll No. 1703277 Page 5


Chandigarh Engineering College, Landran

OUTPUT:

Roll No. 1703277 Page 6


Chandigarh Engineering College, Landran

PRACTICAL-5

Create a program of Insertion,Deletion,Searching,Traversing


using switch statement.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int a[100],n,i,flag,ch,item,position;
cout<<"enter size of an array=";
cin>>n;
for(i=1;i<=n;i++)
{cout<<"enter an elements=";
cin>>a[i];}
do{
cout<<"1 insertion, 2 deletion,3 traversing,4 searching";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"enter new element you want to insert=";
cin>>item;
cout<<"enter position u want to insert element=";
cin>>position;
for(i=n;i>=position;i--)
{a[i+1]=a[i];}
a[position]=item;
for(i=1;i<=n+1;i++)
{
cout<<a[i];

Roll No. 1703277 Page 7


Chandigarh Engineering College, Landran

}
break;
}
case 2:
{
cout<<"enetr the position where u want to delete element=";
cin>>position;
for(i=position;i<=n;i++)
{
a[i]=a[i+1];}
n=n-1;
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;}
break;
}
case 3:
{ for(i=1;i<=n;i++)
{
cout<<a[i];
}
break;
}
case 4:
{
cout<<"enter element u want to search";
cin>>item;
for(i=1;i<=n;i++)
{
if(a[i]==item)
{flag=1;}}
if(flag==1)
cout<<"element found"<<endl;
else
cout<<"not found"<<endl;}

Roll No. 1703277 Page 8


Chandigarh Engineering College, Landran

break;
}
cout<<"PRESS:: 1 CONTINUE \t 2 EXIT";
cin>>i;
}
while(i==1);
getch();
}

OUTPUT:

Roll No. 1703277 Page 9


Chandigarh Engineering College, Landran

PRACTICAL-6
Create a program of sorting using bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int temp,j,a[20],n,i;
cout<<"enter the size of an array=";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter an elements of an array=";
cin>>a[i];
}
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
{
if(a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}}
for(i=1;i<=n;i++)
{cout<<a[i]}
getch();
}
OUTPUT:

Roll No. 1703277 Page 10


Chandigarh Engineering College, Landran

PRACTICAL-7

Create a program of sorting using selection sort.


using namespace std;
int main()
{
int a[100],loc,i,j,min,temp,n;
cout << "enter an size of an array=";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter an elements=";
cin>>a[i];
}
for(i=1;i<=n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n;j++)
{
if(min>a[j])
{min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
for(i=1;i<=n;i++)
{
cout<<a[i];

Roll No. 1703277 Page 11


Chandigarh Engineering College, Landran

return 0;
}

OUTPUT:

Roll No. 1703277 Page 12


Chandigarh Engineering College, Landran

PRACTICAL-8

Create a program of sorting using insertion sort.

#include <iostream>
using namespace std;
int main()
{ int n,i,j,a[100],temp;
cout << "enter size of array" << endl;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=2;i<=n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=1))
{a[j+1]=a[j];
j=j-1;
}a[j+1]=temp;}
for(i=1;i<=n;i++)
{
cout<<a[i];
}
return 0;
}
OUTPUT:

Roll No. 1703277 Page 13


Chandigarh Engineering College, Landran

PRACTICAL-9

Create a program of sorting using merge sort.


#include <iostream>
using namespace std;
void merge_sort(int,int);
void merge_array(int,int,int,int);
int a[100],n;
int main()
{
int i;
cout << "enter size of an array=";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
cout<<endl;
}
merge_sort(1,n-1);
cout<<"sorted array"<<endl;
for(i=1;i<=n;i++)
{
cout<<a[i]<<"\t";
}

return 0;
}
void merge_sort(int i,int j)
{ int mid;
if(i<j){
mid=(i+j)/2;
merge_sort(i,mid);
merge_sort(mid+1,n);
merge_array(i,mid,mid+1,n);
}

Roll No. 1703277 Page 14


Chandigarh Engineering College, Landran

}
void merge_array(int e,int b,int c,int d)
{ int i=e,j=c,k=1,t[100];
while(i<=b&&j<=d)
{
if(a[i]<a[j])
t[k++]=a[i++];
else
t[k++]=a[j++];
}
while(i<=b)
t[k++]=a[i++];
while(j<=d)
t[k++]=a[j++];
for(i=e,j=1;j<=n;i++,j++)
{a[i]=t[j];}
}

OUTPUT:

Roll No. 1703277 Page 15


Chandigarh Engineering College, Landran

PRACTICAL-10

Create a program of sorting using radix sort.


#include <iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[10];
int i,count[10] = {0};
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}

Roll No. 1703277 Page 16


Chandigarh Engineering College, Landran

void print(int arr[], int n)


{ cout<<"sorted array"<<endl;
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int n,arr[100];
cout<<"eneter size of an array=";
cin>>n;
cout<<"eneter an elements=";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
radixsort(arr, n);
print(arr, n);
return 0;
}
OUTPUT:

Roll No. 1703277 Page 17


Chandigarh Engineering College, Landran

PRACTICAL-11

Create a program for implementation of stack using an array


with push and pop operations.
#include <iostream>
using namespace std;
int push();
int pop();
int i,j,ch,maxi,top=0,stack[100],item;
int main()
{
cout<<"enter maximum size of stack=";
cin>>maxi;
do
{
cout<<"1 for push ,2 for pop=";
cin>>ch;
switch(ch)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
}cout<<" 1 for continue 2 for exit=";
cin>>i;
}while(i==1);
return 0;
}
int push()

Roll No. 1703277 Page 18


Chandigarh Engineering College, Landran

{
if(top<maxi)

cout<<"enter new element u want to push=";

cin>>item;

top=top+1;

stack[top]=item;

for(i=1;i<=top;i++)

{cout<<stack[i]<<" ";}

else

cout<<"stack is overflow"<<endl;

return 0;

int pop()

if(top==0)

cout<<"stack is empty";

else

cout<<"deleted element="<<stack[top]<<endl;

top--;

for(i=1;i<=top;i++)

Roll No. 1703277 Page 19


Chandigarh Engineering College, Landran

cout<<stack[i]<<" ";

return 0;

OUTPUT:

Roll No. 1703277 Page 20


Chandigarh Engineering College, Landran

PRACTICAL-12

Create a program for converting infix to postfix using stack.


#include<iostream>
#include<stdio.h>
using namespace std;
#define size 100
char stack[size];
int top=-1;
void push(char item)
{
if(top>=size-1)
cout<<"stack is overflow"<<endl;
else
{
top++;
stack[top]=item;
}
}

char pop()
{
char item;
item=stack[top];
top--;
return(item);
}

int is_operator(char symbol)


{
if(symbol=='^'||symbol=='/'||symbol=='+'||symbol=='-'||symbol=='*')
return 1;
else
return 0;
}

Roll No. 1703277 Page 21


Chandigarh Engineering College, Landran

int precedence(char symbol)


{
if(symbol=='^')
return 3;
else if(symbol=='*'||symbol=='/')
return 2;
else if(symbol=='-'||symbol=='+')
return 1;
else
return 0;
}
int main()
{
char infix[size],postfix[size],item,temp;
int i=0,j=0;
printf("enter elements in infix notation=\n");
gets(infix);
while(infix[i]!='\0')
{
item=infix[i];
if(item=='(')
{
push(item);
}
else if(item>='A'&&item<='Z'||item>='a'&&item<='z')
{
postfix[j]=item;
j++;
}
else if(is_operator(item)==1)
{temp=pop();
while(is_operator(temp)==1&&precedence(temp)>=precedence(item))
{
postfix[j]=temp;

Roll No. 1703277 Page 22


Chandigarh Engineering College, Landran

j++;
temp=pop();
}
push(temp);
push(item);
}
else if(item==')')
{
temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
}
else{
cout<<"invalid arthimetic expression"<<endl;
}
i++;
}
while(top>-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
printf("\nArithmetic expression in post is:");
puts(postfix);
return 0;
}

Roll No. 1703277 Page 23


Chandigarh Engineering College, Landran

OUTPUT:

Roll No. 1703277 Page 24


Chandigarh Engineering College, Landran

PRACTICAL-13

Create a program for implementation of Queue using an array


with insertion and deletion operations.
#include <iostream>
using namespace std;
int insertion();
int deletion();
int display();
int rear=0,front=1;
int ch,i,queue[100],j,maxsize,item;
int main()
{
cout<<"enter maxsize of queue=";
cin>>maxsize;
do
{
cout<<"enter 1 for insertion 2 for deletion 3 for display=";
cin>>ch;
switch(ch)
{
case 1:
{
insertion();

break;
}
case 2:
{
deletion();
break;
}
case 3:
{
display();

Roll No. 1703277 Page 25


Chandigarh Engineering College, Landran

break;
}
}
cout<<"1 for continue and 2 for exit=";
cin>>i;
}while(i=1);
}
int insertion()
{
if(rear==maxsize)
{
cout<<"overflow";
}
else
{rear++;
cout<<"enter element which u want to insert=";
cin>>item;
queue[rear]=item;
return 0;
}}
int deletion()
{
if(rear==0)
{
cout<<"underflow";
}
else cout<<"item deleted"<<queue[front]<<endl;
front ++;
return 0;
}
int display()
{
for(j=front;j<=rear;j++)
{
cout<<queue[j];

Roll No. 1703277 Page 26


Chandigarh Engineering College, Landran

}
return 0;
}
OUTPUT:

Roll No. 1703277 Page 27

You might also like