191CS35 - Lab Programs Print Out
191CS35 - Lab Programs Print Out
a
FACTORAIL OF A GIVEN NUMBER
#include <iostream>
using namespace std;
int main()
{
int n, i, fact;
cout<<"Enter the integer:";
cin>>n;
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"The factorial is:"<<fact;
return 0;
}
Output:
Enter the integer:4
The factorial is:24
Ex. No. - 1.b
PALINDROME OR NOT
#include <iostream>
using namespace std;
int main()
{
int a,num,rem,rnum;
cout<<"Enter a number:";
cin>>num;
a=num;
rnum=0;
while(num!=0)
{
rem=num%10;
rnum=rnum*10+rem;
num=num/10;
}
if(a==rnum)
cout<<"The given number is palindrome"<<endl;
else
cout<<"The given number is not palindrome"<<endl;
return 0;
}
Output:
Enter a number:121
The given number is palindrome
Ex. No. - 1.c
ARMSTRONG OR NOT
#include <iostream>
using namespace std;
int main()
{
int a,num,i,sum;
cout<<"Enter a number:";
cin>>num;
a=num;
sum=0;
while(num>0)
{
i=num%10;
sum=sum+i*i*i;
num=num/10;
}
if(a==sum)
cout<<"Given number is Armstrong"<<endl;
else
cout<<"Given number is not Armstrong"<<endl;
return 0;
}
Output:
Enter a number:153
Given number is Armstrong
Ex. No. - 2.a
TO PRINT STUDENT DETAILS USING CLASS & OBJECTS
#include <iostream>
using namespace std;
class record
{
public:
char name[20];
int regno;
int marks,m1,m2,m3,sum;
float avg;
public:
void getdata();
void calculate();
void display();
};
void record::getdata()
{
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the regno:";
cin>>regno;
cout<<"Enter the m1,m2,m3:\n";
cin>>m1>>m2>>m3;
}
void record::calculate()
{
sum=m1+m2+m3;
avg=sum/3;
}
void record::display()
{
cout<<"******************\n";
cout<<"\nName: "<<name;
cout<<"\nRegno: "<<regno;
cout<<"\nMark1: "<<m1;
cout<<"\nMark2: "<<m2;
cout<<"\nMark3: "<<m3;
cout<<"\nTotal is:"<<sum;
cout<<"\nAverage: "<<avg;
cout<<"\n******************\n";
}
int main()
{
record r;
r.getdata();
r.calculate();
r.display();
return 0;
}
Output:
Enter the name:RAJA
Enter the regno:10001
Enter the m1, m2, m3:
95
98
92
******************
Name: RAJA
Regno: 10001
Mark1: 95
Mark2: 98
Mark3: 92
Total is:285
Average: 95
******************
Ex. No. - 2.b
PRIME OR NOT USING CLASS & OBJECTS
#include <iostream>
using namespace std;
class prime
{
int n,i,count;
public:
void getdata();
void showdata();
};
void prime::getdata()
{
cout<<"Enter the value of n:";
cin>>n;
}
void prime::showdata()
{
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
cout<<"The given number is prime";
else
cout<<"The given number is not prime";
return;
}
int main()
{
prime x;
x.getdata();
x.showdata();
return 0;
}
Output:
Enter the value of n:3
The given number is prime
Ex. No. - 3
AREA OF CIRCLE, TRIANGLE AND RECTANGLE USING
FUNCTION OVERLOADING
#include <iostream>
using namespace std;
#include<stdlib.h>
#define pi 3.14
class fn
{
public:
void area(int);
void area(int,int);
void area(float,int,int);
};
void fn::area(int r)
{
cout<<"Area of circle:"<<pi*r*r;
}
void fn::area(int l,int h)
{
cout<<"Area of rectangle:"<<l*h;
}
void fn::area(float t,int b,int h)
{
cout<<"Area of triangle:"<<0.5*b*h;
}
int main()
{
int ch,r,l,b,h;
float t;
fn obj;
cout<<"FUNCTION OVERLOADING";
cout<<"\n 1.Area of circle\n 2.Area of rectangle\n 3.Area of traingle\n 4.exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the radius\n";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"enter the sides of rectangle\n";
cin>>l>>h;
obj.area(l,h);
break;
case 3:
cout<<"enter the sides of triangle\n";
cin>>b>>h;
obj.area(0.5,b,h);
break;
case 4:
exit(0);
}
return 0;
}
Output:
FUNCTION OVERLOADING
1.Area of circle
2.Area of rectangle
3.Area of traingle
4.exit
enter your choice
1
enter the radius
3
Area of circle:28.26
Ex. No. - 4
AREAS OF CIRCLE & TRIANGLE USING VIRTUAL FUNCTIONS
#include <iostream>
using namespace std;
class shape
{
public:
virtual void getdata()=0;
virtual void display()=0;
};
class circle:public shape
{
private:
float a,r;
public:
void getdata()
{
cout<<"Enter the radius:";
cin>>r;
}
void display()
{
a=3.14*r*r;
cout<<"\nArea of the circle:"<<a<<"square units";
}};
class triangle:public shape
{
private:
int b,h;
float a;
public:
void getdata()
{
cout<<"\nEnter b and h values:";
cin>>b>>h;
}
void display()
{
a=0.5*b*h;
cout<<"\nArea of the triangle:"<<a<<"square units\n\t";
}};
int main()
{
shape *s[2];
circle c;
triangle t;
s[0]=&c;
s[0]->getdata();
s[0]->display();
s[1]=&t;
s[1]->getdata();
s[1]->display();
return 0;
}
Output:
Enter the radius:3
Area of the circle:28.26square units
Enter b and h values:2 2
Area of the triangle:2square units
Ex. No. - 5.
LINKED LIST IMPLEMENTATION OF LIST ADT
#include <iostream>
using namespace std;
class LIST
{
struct Node
{
int num;
struct Node *next;
};
Node *first;
public:
LIST() { first=NULL; } // Default constructor
void insertfirst();
void deletefirst();
void display();
};
void LIST :: insertfirst()
{
Node *nn=new Node;
cout<<"\nEnter the element to be inserted\n";
cin>>nn->num;
nn->next=first;
first=nn;
}
void LIST :: deletefirst()
{
if(first==NULL)
cout<<"\nList is empty!\n";
else
{
Node *temp=first;
cout<<"\nDeleted element is "<<first->num;
first=first->next;
delete(temp);
}
}
void LIST :: display()
{
if(first==NULL)
cout<<"\nList is empty!\n";
else
{
Node *temp=first;
while(temp!=NULL)
{
cout<<temp->num<<" ";
temp=temp->next;
}
}
}
int main()
{
LIST L;
int ch;
do
{
cout<<"\n1. Insert at front\n2. Delete from front\n3. Display\n4. Exit\n\nEnter your choice:\n";
cin>>ch;
switch(ch)
{
case 1: L.insertfirst(); break;
case 2: L.deletefirst(); break;
case 3: L.display(); break;
}
}while(ch!=4);
return 0;
}
Output:
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
1
Enter the element to be inserted
5
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
1
Enter the element to be inserted
6
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
1
Enter the element to be inserted
8
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
1
Enter the element to be inserted
9
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
2
Deleted element is 9
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
3
865
1. Insert at front.
2. Delete from front
3. Display
4. Exit
Enter your choice:
4
Ex. No. - 6.
ARRAY IMPLEMENTATION OF STACK ADT
#include <iostream>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
int main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
Output:
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5
1.push 2.pop 3.display 4.exit
Enter ur choice3
52
1.push 2.pop 3.display 4.exit
Enter ur choice 4
Ex. No. - 7.
LINKED LIST IMPLEMENTATION OF QUEUE ADT
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
}
*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
void enqueue(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int dequeue()
{
int x;
if(front == NULL)
{
cout<<"empty queue\n";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}
int main()
{
int n,c = 0,x;
cout<<"Enter the number of values to be pushed into queue\n";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queue\n";
cin>>x;
enqueue(x);
c++;
}
cout<<"\n\nRemoved Values\n\n";
while(true)
{
if (front != NULL)
cout<<dequeue()<<endl;
else
break;
}
return 0;
}
Output:
6
Enter the value to be entered into queue
5
Enter the value to be entered into queue
4
Enter the value to be entered into queue
3
Enter the value to be entered into queue
2
Enter the value to be entered into queue
1
Enter the value to be entered into queue
0
Removed Values
5
4
3
2
1
0
Ex. No. - 8.
IMPLEMENTATION OF QUICK SORT
#include <iostream>
using namespace std;
const int INPUT_SIZE = 10;
// A simple print function
void print(int *input)
{
for ( int i = 0; i < INPUT_SIZE; i++ )
cout << input[i] << " ";
cout << endl;
}
// The partition function
int partition(int* input, int p, int r)
{
int pivot = input[r];
while ( p < r )
{
while ( input[p] < pivot )
p++;
while ( input[r] > pivot )
r--;
if ( input[p] == input[r] )
p++;
else if ( p < r )
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
}
}
return r;
}
// The quicksort recursive function
void quicksort(int* input, int p, int r)
{
if ( p < r )
{
int j = partition(input, p, r);
quicksort(input, p, j-1);
quicksort(input, j+1, r);
}
}
int main()
{
int input[INPUT_SIZE] = {500, 700, 800, 100, 300, 200, 900, 400, 1000, 600};
cout << "Input: ";
print(input);
quicksort(input, 0, 9);
cout << "Output: ";
print(input);
return 0;
}
Output:
Input: 500 700 800 100 300 200 900 400 1000 600
Output: 100 200 300 400 500 600 700 800 900 1000