CS Practical Class 12
CS Practical Class 12
PRACTICAL FILE
MADE BY:BY:
MADE
ANMOL
AnmolPURI
Puri
CLASS : XII-D
CLASS XI-D
ROLL NO. : 07
Roll No. 07
INDE
X
Some basic programs(Flow of Control)
1.The C++ program to print the half pyramid using stars (*)
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i, j;
cout<<"* ";
cout<<"\n";
getch();
}
2.A C++ program using switch() case.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int n;
cin>>n;
switch(n)
case 1 : cout<<"monday";
break;
case 2 : cout<<"tuesday";
break;
case 3 : cout<<"wednesday";
break;
case 4 : cout<<"thursday";
break;
case 5 : cout<<"friday";
break;
getch();
}
3.A C++ program to display the sum of the series from 1 to n.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i,n,sum=0;
cout<<“1+2+3+……+n”;
cin>>n;
for(i=1;i<=n;++i)
sum+=i;
cout<<“Sum=”<<sum;
getch();
#include<conio.h>
void main()
n = num;
do
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
else
getch();
#include<conio.h>
void main()
clrscr ();
int sum=0,n2=1,n1,n;
cin>>n;
while(n2!=0)
{
n1=n%10;
sum+=n1;
n2=n/10;
cout<<n1<<',';
n=n2;
getch ();
#include<conio.h>
void main()
clrscr();
cin>>num1;
cin>>num2;
swap=num1;
num1=num2;
num2=swap;
getch();
#include<conio.h>
void main()
int year;
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 == 0)
else
else
}
else
getch();
#include<conio.h>
void main()
int a ,b ,c ,greatest;
cin>>a>>b>>c;
greatest=(a>b&&a>c)?a:(b>c)?b : c;
getch();
#include<conio.h>
void main()
int n, t1 = 0, t2 = 1, nextTerm = 0;
cin >> n;
if(i == 1)
t1; continue;
if(i == 2)
"; continue;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
getch();
Strings
1.Program to know the length of the strings
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
clrscr();
inti;
char s[80];
gets (s);
for(i=0;s[i]!='\0';i++);
getch();
#include <conio.h>
#include<stdio.h>
void main()
clrscr();
int i,j,flag=0;
char s1[100],s2[100];
gets(s1);
gets(s2);
for(i=0;s1[i]!='\0',s2[i]!='\0';i++)
if (s1[i]!=s2[i])
flag=1;
break;
if (flag==1)
else
if (flag==0)
getch();
void main()
clrscr();
gets(s);
for(i=0;s[i]!='\0';i++);
j=i-1;
i=0;
while(i<j)
temp=s[i];
s[i]=s[j];
s[j]=temp;
--j;
++i;
puts(s);
getch();
#include <conio.h>
#include<stdio.h>
void main()
clrscr();
int i,j,flag=0;
char temp,s[100];
gets(s);
for(i=0;s[i]!='\0';i++);
j=i-1;
i=0;
while(i<j)
if (s[i]!=s[j])
flag=1;
break;
--j;
++i;
if (flag==0)
else
if(flag==1)
}
getch();
#include <conio.h>
#include<stdio.h>
void main()
clrscr();
int i,j;
char s1[100],s2[100],s3[100];
gets(s1);
gets(s2);
for(i=0;s1[i]!='\0';i++)
s3[i]=s1[i];
for(j=0;s2[j]!='\0';j++)
s3[i+j]=s2[j];
s3[i+j]='\0';
getch();
#include<conio.h>
#include<stdio.h>
void main()
clrscr();
gets(str1);
gets(str2);
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
while(str1[i]!='\0')
temp[j]=str1[i];
i++;
j++;
}
temp[j]='\0';
i=0, j=0;
while(str2[i]!='\0')
str1[j]=str2[i];
i++;
j++;
str1[j]='\0';
i=0, j=0;
while(temp[i]!='\0')
str2[j]=temp[i];
i++;
j++;
str2[j]='\0';
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
getch();
Functions
1.A C++ program to explain function overloading.
#include<iostream.h>
#include<conio.h>
int main()
long a, b, x;
float c, d, y;
x = add(a, b);
y = add(c, d);
return 0;
long sum;
sum = x + y;
return sum;
}
float sum;
sum = x + y;
return sum;
#include<conio.h>
#include<stdlib.h>
void main()
clrscr ();
int i;
for(i=0;i<10;i++)
getch ();
#include<conio.h>
#include<stdlib.h>
void main()
clrscr ();
int i;
randomize();
for(i=0;i<10;i++)
cout<<random(91)+90<<endl;
getch ();
#include<conio.h>
void main()
clrscr ();
inta,b;
cin>>a>>b;
swap(a,b); //Invokation
int temp;
temp=x;
x=y;
y=temp;
#include<conio.h>
void main()
clrscr ();
int a,b;
cin>>a>>b;
swap(a,b); //Invokation
getch();
}
int temp;
temp=x;
x=y;
y=temp;
Arrays
1.FINDING ODD AND EVEN ELEMENTS
#include<iostream.h>
#include<conio.h>
void main( )
int a[100],size;
int e[100],o[100]; //e is for array of even elements and o for odd int
i=0,j=0;
cin>>size;
for(int x=0;x<size;x++)
cin>>a[x];
for(x=0;x<size;x++)
if(a[x]%2==0)
e[i++]=a[x];
else
o[j++]=a[x];
for(int k=0;k<i;k++)
cout<<e[k]<<’\t’;
cout<<endl;
for(int l=0;l<j;l++)
cout<<o[l]<<’\t’;
getch( );
#include<conio.h>
void main( )
clrscr( );
int a[100];
cout<<”Enter size”;
int size;
cin>>size;
for(int x=0;x<size;x++)
cin>>a[x];
int t;
cin>>t;
for(x=0;x<size;x++)
if(a[x]==t)
if(x==size)
array”<<endl; getch( );
#include<conio.h>
void main( )
clrscr( );
int a[20],size;
cin>>size;
for(int i=0;i<size;++i)
cin>>a[i];
clrscr();
for(i=0;i<size;++i)
cout<<a[i]<<" ";
for(i=0;i<size-1;++i)
int k=0;
for(int j=0;j<size-i-1;++j)
if(a[j]>a[j+1])
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
k++;
if(k==0)
break;
for(i=0;i<size;++i)
cout<<a[i]<<" ";
getch( );
#include<conio.h>
void main()
clrscr();
int n[10];
for(int i=0;i<10;++i)
cin>>n[i];
clrscr();
for(i=0;i<10;++i)
cout<<n[i]<<" ";
int l,sl,s;
for(i=0;i<10;++i)
int x=n[i];
if(i==0)
s=x;
l=x;
sl=0;
}
else if(i==1&&x<l)
sl=x;
s=x;
else if(x>l)
sl=l;
l=x;
else if(x>sl)
sl=x;
else if(x<s)
s=x;
getch();
#include<conio.h>
void main()
{
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>arr[i][j];
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<arr[i][j];
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(arr[i][j]%2==0)
arr[i][j]+=2;
else
arr[i][j]+=3;
cout<<endl;
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<arr[i][j]<<" ";
getch();
#include<conio.h>
void main()
clrscr();
for(j=0;j<3;j++)
cin>>arr[i][j];
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<arr[i][j];
cout<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i>j)
cout<<arr[i][j]<<endl;
}
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i<j)
cout<<arr[i][j]<<endl;
getch();
#include<conio.h>
void main()
cin>>n;
for(i=0;i<n;i++)
cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];
getch();
#include<conio.h>
int main()
cin >> r;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << "Enter element a" << i + 1 << j + 1 << " : ";
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cout << endl << "Sum of two matrix is: " << endl;
if(j == c - 1)
return 0;
#include<conio.h>
int main()
k; cout << "Enter rows and columns for first matrix: ";
cout << "Enter rows and columns for second matrix: ";
while (c1!=r2)
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cout << endl << "Enter elements of matrix 1:" << endl;
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
cout << endl << "Enter elements of matrix 2:" << endl;
cout << "Enter element b" << i + 1 << j + 1 << " : ";
mult[i][j]=0;
if(j == c2-1)
return 0;
#include<conio.h>
void main()
clrscr();
cin>>arr[i][j];
cout<<"Transposing Array...\n";
for(i=0; i<3; i++)
arrt[i][j]=arr[j][i];
cout<<arrt[i][j];
cout<<"\n";
getch();
Structures
1.program to illustrate nested structures.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct data
char name[25];
char section;
} ns;
struct student
int rollno;
int semester;
int age;
data ns;
};
void main( )
clrscr();
student genius;
gets(genius.ns.name);
cin>>genius.rollno;
cin>>genius.ns.section;
cin>>genius.age;
clrscr();
No = "<<genius.rollno<<endl;
getch();
i. call by val
#include <iostream.h>
#include<conio.h>
struct Person
char name[50];
int age;
float salary;
};
void main()
{
Person p;
cin.get(p.name, 50);
displayData(p);
getch();
void displayData(Person p)
#include<conio.h>
struct box
int height;
int weight;
int length;
};
void main()
clrscr();
f(b1);
cout<<"Height is :"<<b1.height<<endl;
cout<<"Weight is :"<<b1.weight<<endl;
cout<<"Length is :"<<b1.length<<endl;
f(b2);
cout<<"Height is :"<<b2.height<<endl;
cout<<"Weight is :"<<b2.weight<<endl;
cout<<"Length is :"<<b2.length<<endl;
cin>>a1.height;
cin>>a1.weight;
cin>>a1.length;
}
Inheritance
1.Program to show Single inheritance:-
#include<iostream.h>
#include<conio.h>
using namespace std;
class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();
};
void staff::getdata()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}
void staff::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}
void typist::getdata()
{
cout<<"Speed:";
cin>>speed;
}
void typist::display()
{
cout<<"Speed:"<<speed<<endl;
}
int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}
#include <iostream>
#include <conio.h>
using namespace std;
class A
{
public:
void display()
{
cout <<"This is method of A";
}
};
class B
{
public:
void display()
{
cout <<"This is method of B";
}
};
{
public:
};
int main()
{
C sample;
sample.display(); /*causes ambiguity*/
return 0;
getch();
}
3.Program to show Multilevel inheritance:-
#include <iostream>
#include <conio.h>
using namespace std;
class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
int main()
{
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<<"Displaying data"<<endl;
p.display();
getch();
return 0;
}
class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
int main()
{
student s;
employee e;
cout<<"Student"<<endl;
cout<<"Enter data"<<endl;
s.getdata();
cout<<endl<<"Displaying data"<<endl;
s.display();
cout<<endl<<"Employee"<<endl;
cout<<"Enter data"<<endl;
e.getdata();
cout<<endl<<"Displaying data"<<endl;
e.display();
return 0;
getch();
}
Data Structures
#include<iostream.h>
using namespace std;
class Stack
{
int top;
public:
int a[MAX]; //Maximum size of Stack
bool Stack::push(int x)
{
if (top >= MAX)
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
return true;
}
}
int Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
return 0;
}
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
}
#include <iostream.h>
#define MAX 5
using namespace std;
/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void insert(int item)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
/*
* Display Circular Queue
*/
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
cout<<"Queue elements :\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
cout<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, item;
Circular_Queue cq;
do
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for insertion in queue : ";
cin>>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}
while(choice != 4);
return 0;
}
// The queue, front stores the front node of LL and rear stores ths
// last node of LL
struct Queue
{
struct QNode *front, *rear;
};
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}
// Class Declaration
class person {
//Access - Specifier
public:
//Variable Declaration
string name;
int number;
};
//Main Function
int main() {
// Object Creation For Class
person obj;
getch();
return 0;
Class student
{
Private:
Int roll;
Float height, weight;
Public:
Student ()
roll=0;
height=0;
weight=0;
Void display ()
Cout<<”\n Height”<<height;
Cout<<”\n weight”<<weight;
~ Student ()
};
Void main ()
Student obj;
getch ();
}