You are on page 1of 22

WAP to find the largest element in an array:

#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int large, arr[50], size, i;
cout<<"Enter Array Size (max 50) : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Searching for largest number ...\n\n";
large=arr[0];
for(i=0; i<size; i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
cout<<"Largest Number = "<<large;
getch();
}

WAP to insert an element in an array:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;
// now create a space at the required position
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!\n";
cout<<"Now the new array is : \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch(); }

WAP to delete an element from an array:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, del, count=0;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be delete : ";
cin>>del;
for(i=0; i<size; i++)
{
if(arr[i]==del)
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
cout<<"Element deleted successfully..!!\n";
cout<<"Now the new array is :\n";
for(i=0; i<(size-1); i++)
{
cout<<arr[i]<<" ";
}
}
getch();
}

WAP to search an element in an array:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}

WAP to enter 10 numbers in an array and calculate


the sum and average:
#include<iostream.h> 
#include<conio.h> 
void main ( ) 

      int a[10], i, s; 
      float av; 
      clrscr();
      cout<<"\n Enter the Different 10 Values : "; 
      for (i = 0; i < 10; i++) 
         { 
               cin>>a[i]; 
         } 
               s=0; 
                for(i=0; i<10; i++) 
                    { 
                          s=s+a[i]; 
                    } 
                          cout<<"\nSum of  " <<n <<" Value is : " <<s; 
                          av=(float)s/10; 
                          cout<<"\nAverage of "<<n <<" Value is : "<<av; 
                          getche(); 

WAP to sort the elements of an array using Insertion sort:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using insertion sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

WAP to sort the elements of an array using bubble sort:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

WAP using recursion to check if the number


is prime or not:

#include<iostream.h>
#include<conio.h>

int isPrime(int n, int i = 2)


{
    // Base cases
    if (n <= 2)
        return (n == 2) ? 1 : 0;
    if (n % i == 0)
        return 0;
    if (i * i > n)
        return 1;
 
    // Check for next divisor
    return isPrime(n, i + 1);
}
 
// Driver Program
int main()
{
    int n;
cin>>n:
    if (isPrime(n))
        cout << "Prime number";
    else
        cout << "Not a prime number";
 
    return 0;
}

WAP using recursion to print fibonacci series


for first 15 numbers:
#include<iostream.h>
#include<conio.h>

int fibonacci(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}

int main()
{
int n,i=0;
while(i<15)
{
cout<<" "<<fibonacci(i);
i++;
}
cout<<"\n";

return 0;
}

WAP using recursion to calculate the factorial:


#include<iostream.h>
#include<conio.h>

int fact(int);

void main()
{
int num,f;
clrscr();

cout<<"\n Enter the number: ";


cin>>num;

f=fact(num);
cout<<"\n The factorial of " <<num<<" is "<<f;
getch();
}

int fact(int n)
{
if(n==0||n==1)
return 1;
else
return(n * fact(n-1));
}

WAP to enter 2 matrix using 2-D arrays, calculate


the sum and display the resultant:
#include<iostream.h>
#include<conio.h>

main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];

cout << "Enter the elements of second matrix\n";

for ( c = 0 ; c < m ;c++ )


for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

cout << "Sum of entered matrices:-\n";

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";

cout << endl;


}

return 0;
}

WAP to perform stack operations, push() and


pop() using arrays:
#include<iostream.h>
#include<conio.h>
int stack[20],top=-1; //global declaration
void push();
void pop();
void display();
void main()
{ clrscr();
int ch;
cout<<"Program for Stack Operations by-Tarun Rawat\n\n";
do
{ cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice : ";
cin>>ch;
if(ch==1)
push();
else if(ch==2)
pop();
else if(ch==3)
display();
else if(ch==4)
cout<<"\nEnd Of program";
}while(ch!=4);
}
//to push elements in a stack
void push()
{
if(top>9)
cout<<"\nStack Overflow";
else
{ top=top+1;
cout<<"\nEnter a value : ";
cin>>stack[top];
}

//to pop/delete stack elements


void pop()
{
if(top==-1)
cout<<"\nStack Overflow. No Elements to pop";
else
{
cout<<stack[top]<<" is deleted..";
top--;
}
}

//displaying stack elements


void display()
{
int i;
if(top==-1)
cout<<"\nStack underflow. No elements to display..";
else
{
cout<<"\nThe stack elements are : ";
for(i=0;i<=top;i++)
{
cout<<"\t"<<stack[i];
}
cout<<"\n";

}
}
WAP to reverse a string using an array:
WAP to insert an element, delete an element and
display the elements of a queue using array:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],front=0,rear=0;
void main()
{
int ch;
clrscr();
void enqueue();
void dequeue();
void display();
while(1)
{
cout<<"\n 1. add element";
cout<<"\n 2. remove element";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
}
void enqueue()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}
void dequeue()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{
no=q[front];
front++;
cout<<"\n"<<no<<" -removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}
}

W.A.P to create a linked list, add elements, display the


elements and delete an element from the linked list:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int info;
Node* next;
}
class List:public Node
{

Node *first,*last;
public:
List()
{
first=NULL;
last=NULL;
}
void create();
void insert();
void delet();
void display();
};
void List::create()
{
Node *temp;
temp=new Node;
int n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=first;
}

else
{
last->next=temp;
last=temp;
}
}
void List::insert()
{
Node *prev,*cur;
prev=NULL;
cur=first;
int count=1,pos,ch,n;
Node *temp=new Node;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
cout<<"\nINSERT AS\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
temp->next=first;
first=temp;
break;
case 2:
last->next=temp;
last=temp;
break;
case 3:
cout<<"\nEnter the Position to Insert:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
prev->next=temp;
temp->next=cur;
}
else
cout<<"\nNot Able to Insert";
break;

}
}
void List::delet()
{
Node *prev=NULL,*cur=first;
int count=1,pos,ch;
cout<<"\nDELETE\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(first!=NULL)
{
cout<<"\nDeleted Element is "<<first->info;
first=first->next;
}
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(cur!=last)
{
prev=cur;
cur=cur->next;
}
if(cur==last)
{
cout<<"\nDeleted Element is: "<<cur->info;
prev->next=NULL;
last=prev;
}
else
cout<<"\nNot Able to Delete";
break;
case 3:
cout<<"\nEnter the Position of Deletion:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
cout<<"\nDeleted Element is: "<<cur->info;
prev->next=cur->next;
}
else
cout<<"\nNot Able to Delete";
break;
}
}
void List::display()
{
Node *temp=first;
if(temp==NULL)
{
cout<<"\nList is Empty";
}
while(temp!=NULL)
{
cout<<temp->info;
cout<<"-->";
temp=temp->next;
}
cout<<"NULL";
}

int main()
{
List l;
int ch;
while(1)
{
cout<<"\n**** MENU ****";
cout<<"\n1:CREATE\n2:INSERT\n3:DELETE\n4:SEARCH\n5:DISPLAY\n6:EXIT\n";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.delet();
break;
case 4:
l.display();
break;
case 5:
return 0; } } return 0; }
W.A.P to perform a search operation using linked list:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int info;
Node* next;
};
class List:public Node
{

Node *first,*last;
public:
List()
{
first=NULL;
last=NULL;
}

void create();
void search();
};

void List::create()
{
Node *temp;
temp=new Node;
int n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=first;
}

else
{
last->next=temp;
last=temp;
}
}

void List::search()
{
int value,pos=0;
bool flag=false;
if(first==NULL)
{
cout<<"List is Empty";
return;
}
cout<<"Enter the Value to be Searched:";
cin>>value;
Node *temp;
temp=first;
while(temp!=NULL)
{
pos++;
if(temp->info==value)
{
flag=true;
cout<<"Element"<<value<<"is Found at "<<pos<<" Position";
return;
}
temp=temp->next;
}
if(!flag)
{
cout<<"Element "<<value<<" not Found in the List";
}
}

Void main()
{
List I;
I.create();
I.search();
}

You might also like