You are on page 1of 59

COMPUTER PRACTICAL FILE

SUBMITTED BY: ROHAN PRADHAN CLASS: XII A


SUBMITTED TO: Mr. Sunil Jangra
Roll No.: 16
1. Write a program to input two numbers m and n and display first m multiples of n.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m,n;
cout<<"enter the base number:";
cin>>n;
cout<<"enter the no. of multiples you want:";
cin>>m;
cout<<"The multiples are:"<<endl;
for(int i=1;i<=m;i++)
{
cout<<(n*i)<<endl;
}
getch();
}
OUTPUT:
2. Write a program to input day number of a week and display the corresponding day name.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<"Enter the day number:" ;
cin>>a;
switch(a)
{
case 1:
cout<<"the day is:"<<"\t"<<"Monday";break;
case 2:
cout<<"the day is:"<<"\t"<<"Tuesday";break;
case 3:
cout<<"the day is:"<<"\t"<<"Wednesday";break;
case 4:
cout<<"the day is:"<<"\t"<<"Thursday";break;
case 5:
cout<<"the day is:"<<"\t"<<"Friday";break;
case 6:
cout<<"the day is:"<<"\t"<<"Saturday";break;
case 7:
cout<<"the day is:"<<"\t"<<"Sunday";break;
default:
cout<<"Invalid number.";
}
getch();
}
OUTPUT:
3. Write a menu driven program to calculate the total surface area and volume of a cube,
cuboid, or cylinder depending upon user’s choice.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f,g;
cout<<"****WELCOME****"<<endl;
cout<<"The choices are:"<<endl;
cout<<"1.CUBE"<<endl<<"2.CUBOID"<<endl<<"3.CYLINDER"<<endl;
cout<<"Enter your choice: ";
cin>>a;
if(a==1)
{
cout<<"Enter the length of side of cube: ";
cin>>b;
cout<<"the surface area is = "<<(6*b*b)<<endl;
cout<<"the volume is = "<<(b*b*b);
}
else if(a==2)
{
cout<<"Enter the length of cuboid: ";
cin>>c;
cout<<"Enter the breadth of the cuboid: ";
cin>>d;
cout<<"Enter the height of the cuboid: ";
cin>>e;
cout<<"the surface area is = "<<(6*c*d*e)<<endl;
cout<<"the volume is = "<<(c*d*e);
}
else if(a==3)
{
cout<<"enter the length of the cylinder: " ;
cin>>f;
cout<<"enter the radius of cylinder: ";
cin>>g;
cout<<"the surface area of cylinder is= "<<(2*(22/7)*g*f);
cout<<"\n"<<"the volume of cylinder is= "<<((22/7)*g*g*f);
}
else
cout<<"Invalid choice.";
getch();
}
OUTPUT:
4. Write a program to read a string and print out the following:
 No. of capital alphabets.
 No. of small alphabets.
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
int c=0,s=0,m;
char str[100];
cout<<"enter the no. of alphabets in string:";
cin>>m;
cout<<"Enter the string(max=100):"<<endl;
for(int i=0;i<m;i++)
{
cin>>str[i];
if(islower(str[i]))
s++;
else if(isupper(str[i]))
c++;
}
cout<<"The no. of capital alphabets = "<<c<<endl;
cout<<"The no. of small alphabets = "<<s;
getch();
}
OUTPUT:
5. Write a program to read a string and print it after replacing each of its capital alphabets by
the corresponding small alphabet and each small alphabet by its corresponding capital
alphabet.
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
char str[100];
int a;
cout<<"Enter the no. of alphabets in string(max-100): ";
cin>>a;
cout<<"Enter the string: "<<endl;
for(int i=0;i<a;i++)
{
cin>>str[i];
if(islower(str[i]))
str[i]=toupper(str[i]);
else if(isupper(str[i]))
str[i]=tolower(str[i]);
}
cout<<"The new string is: "<<endl;
for(i=0;i<a;i++)
{
cout<<str[i];
}
getch();
}
OUTPUT:

6. Write a program to input 10 elements in an array and then display these elements in
reverse order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ar[10];
cout<<"Enter the array elements: ";
for(int i=0;i<10;i++)
{
cin>>ar[i];
}
cout<<"\n"<<"The reversed order is: ";
for(i=9;i>=0;i--)
{
cout<<ar[i]<<" ";
}
getch();
}
OUTPUT:
7. Write a program to input elements in a 2-D array and then print the sum of the main
diagonal elements of this array.
#include<iostream.h>
#include<conio.h>
void main()
{
int m,n,b=0;
clrscr();
int a[50][50];
cout<<"Enter the no. of rows in array(max-50): ";
cin>>m;
cout<<"Enter the no. of columns in array(max-50): ";
cin>>n;
cout<<"Enter the array: "<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"The sum of the main diagonl elements is: ";
for(i=0;i<m;i++)
b=(b+a[i][i]);
cout<<b;
getch();
}
OUTPUT:

8. Write a program to check whether the given string is palindrome or not.


#include <iostream>
#include <string.h>
#include<conio.h>
int main()
{ clrscr();
char str1[10],str2[10],c;

do
{
cout<<"\nENTER A STRING: ";
cin>>str1;

strcpy(str2,str1);

strrev(str1);

if(strcmp(str1,str2)==0) //compares str1 and str2


cout<<"GIVEN STRING IS PALINDROME";
else
cout<<"GIVEN STRING IS NOT PALINDROME";
cout<<"\n\nDO YOU WISH TO CONTINUE (Y/N)? ";
cin>>c;

}while(c=='y'||c=='Y');

return 0;
}
OUTPUT:
9. Write a program to input two matrices, find their sum, difference, or product depending
upon user’s choice, and then display the resultant matrix along with the original matrices.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50][50],c[50][50],m,n,b,x,y,d[50][50],z,i,j;
int e[50][50],f[50][50];
char g;
do
{
cout<<"******WELCOME******"<<endl;
cout<<"OPTIONS: "<<endl;
cout<<"1. Sum"<<endl<<"2. Product"<<endl<<"3. Difference";
cout<<"\n"<<"Enter your choice: ";
cin>>b;
cout<<"\n"<<"Enter the no. of rows of matrix 1: ";
cin>>m;
cout<<"\n"<<"Enter the no. of columns of matrix 1: ";
cin>>n;
cout<<"\n"<<"Enter the no. of rows of matrix 2: ";
cin>>x;
cout<<"\n"<<"Enter the no. of columns of matrix 2: ";
cin>>y;
if(b==1)
{
if((m==x)&&(n==y))
{
cout<<"Enter the matrix 1:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n"<<"The matrix 1 is: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"\n"<<"Enter the matrix 2:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>c[i][j];
}
cout<<"\n"<<"The matrix 2 is:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<c[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"\n"<<"The sum of two matrices is: ";
for(i=0;i<m;i++)
{
for(j=0;j<y;j++)
{
cout<<a[i][j]+c[i][j];
cout<<" ";
}
cout<<"\n";
}
}
else
cout<<"The sum is not possible.";
}
else if(b==2)
{
if(n==x)
{
cout<<"Enter the matrix 1:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n"<<"The matrix 1 is:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"\n"<<"Enter the matrix 2:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>c[i][j];
}
cout<<"\n"<<"The matrix 2 is:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<c[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"\n"<<"The product of matrices is: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<y;j++)
{
d[i][j]=0;
for(z=0;z<n;z++)
d[i][j]=d[i][j]+(a[i][z]*c[z][j]);
cout<<d[i][j]<<" ";
}
cout<<"\n";
}
}
else
cout<<"The multiplication is not possible.";
}
else if(b==3)
{
if((m==n)&&(x==y))
{
cout<<"Enter the matrix 1: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>e[i][j];
}
cout<<"The matrix 1 is:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<e[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"Enter the matrix 2:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
cin>>f[i][j];
}
cout<<"The matrix 2 is:"<<endl;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<f[i][j];
cout<<" ";
}
cout<<"\n";
}
cout<<"The differnce of two matrices is:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<y;j++)
{
cout<<(e[i][j]-f[i][j]);
cout<<" ";
}
cout<<"\n";
}
}
cout<<"The difference is not possible.";
}
else
cout<<"The option is invalid.";
getch();
cout<<"Do you want to continue(y/n)?";
cin>>g;
}
while(g=='y');
}
OUTPUT:
10. Write a program using function overloading to calculate area of circle, square,
rectangle, right triangle and triangle using Heron’s Formula.
#include<iostream.h>
#include<conio.h>
#include<math.h>

float area(float a)
{
return((3.14)*a*a);
}
float area1(float a)
{
return(a*a);
}
float area(float a,float b)
{
return(a*b);
}
float area1(float a,float b)
{
return((0.5)*a*b);
}
float area(float a,float b,float c)
{
float s=((0.5)*(a+b+c));
return(sqrt(s*(s-a)*(s-b)*(s-c)));
}

void main()
{
clrscr();
char i;
float x,y,z;
int d;
cout<<"\t"<<"****WELCOME****"<<endl;
cout<<"1. Circle"<<endl<<"2. Square"<<endl<<"3. Rectangle"<<endl;
cout<<"4. Right Traingle"<<endl<<"5. Triangle"<<endl;
do
{
cout<<"Enter your choice: ";
cin>>d;

if(d==1)
{
cout<<"Enter the radius of circle: ";
cin>>x;
cout<<"The area of circle is= "<<area(x);
}
else if(d==2)
{
cout<<"enter the side of sqaure: ";
cin>>x;
cout<<"The area of square is= "<<area1(x);
}
else if(d==3)
{
cout<<"Enter the length of rectangle: ";
cin>>x;
cout<<"Enter the breadth of rectangle: ";
cin>>y;
cout<<"The area of rectangle is= "<<area(x,y);
}
else if(d==4)
{
cout<<"Enter the length of base: ";
cin>>x;
cout<<"Enter the length of altitude: ";
cin>>y;
cout<<"The area of the right triangle is= "<<area1(x,y);
}
else if(d==5)
{
cout<<"Enter the length of side 1: ";
cin>>x;
cout<<"Enter the length of side 2: ";
cin>>y;
cout<<"Enter the length of side 3: ";
cin>>z;
cout<<"The area of triangle is:= "<<area(x,y,z);
}
else
cout<<"The choice is invalid.";
cout<<"\n"<<"Do you want to continue(y/n)?";
cin>>i;
}while(i=='y');
getch();
}
OUTPUT:
11. Write a function to find and display the sum of each row and each column of a two
dimensional array of type float.
#include<iostream.h>
#include<conio.h>
float array()
{
int m,n;
cout<<"Enter the number of rows(max-50):";
cin>>m;
cout<<"Enter the no. of columns(max-50):";
cin>>n;
float b[50],c[50],a[50][50];
cout<<"Enter the 2-D array:"<<"\n";
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(i=1;i<=m;i++)
{
b[m]=0;
for(int j=1;j<=n;j++)
{
b[m]+=a[i][j];
}
cout<<"the sum of row"<<i<<"="<<b[m]<<"\n";
}
for(int j=1;j<=n;j++)
{
c[n]=0;
for(int i=1;i<=m;i++)
{
c[n]+=a[i][j];
}
cout<<"The sum of column"<<j<<"="<<c[n]<<"\n";
}
return 0.1;
}

int main()
{
clrscr();
array();
getch();
return 0;
}
OUTPUT:

12. Write a program to sort the given array in ascending order using bubble sort method.
#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();
}
OUTPUT:

13. Write a program to implement stack as an array.


#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int push(int[],int&,int);
void display(int[],int);
const int size=50;
int main()
{
int stack[size],item,top=-1,res;
char ch='y';
system("cls");
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter ITEM for insertion:";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{
cout<<"OVERFLOW!!Aborting!!\n";
exit(1);
}
cout<<"\nThe stack now is:\n";
display(stack,top);
cout<<"\nWant to insert more elements?(y/n)...";
cin>>ch;
}
return 0;
}
int push(int stack[],int &top,int ele)
{
if (top==size-1)
return -1;
else
{
top++;
Stack[top]=ele;
}
return 0;
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<endl;
}
OUTPUT:

14. Write a program to implement queue as an array.


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

int insert_in_queue(int [], int);


void display(int [], int, int);

const int SIZE = 50;

int queue[SIZE];
int front=-1;
int rear=-1;
void main()
{
clrscr();
int item, check;
char ch='y';

while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{
cout<<"\nOverflow..!!..Aborting..!!..Press a key to exit..\n";
getch();
exit(1);
}
cout<<"Item inserted successfully..!!\n";
cout<<"\nNow the Queue (Front...to...Rear) is:\n";
display(queue, front, rear);
cout<<"\nWant to insert more ? (y/n).. ";
cin>>ch;
}
getch();
}

int insert_in_queue(int queue[], int elem)


{
if(rear == SIZE-1)
{
return -1;
}
else if(rear == -1)
{
front = rear = 0;
queue[rear] = elem;
}
else
{
rear++;
queue[rear] = elem;
}
return 0;
}

void display(int queue[], int front, int rear)


{
if(front == -1)
{
return;
}
for(int i=front; i<rear; i++)
{
cout<<queue[i]<<" <- ";
}
cout<<queue[rear]<<"\n";
}
OUTPUT:
15. Write a program to implement stack as a linked list.
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int info;
node*next;
}*top,*newptr,*save,*ptr;
node*create_new_node(int);
void push(node*);
void display(node*);
int main()
{
int inf;
char ch='y';
top=NULL;
system("cls");
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter information for the new node...";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"\ncannot create new node!!ABORTING!!\n";
exit(1);
}
push(newptr);
cout<<"\nNow the linked-stack is:\n";
display(top);
cout<<"\nPress Y to enter more nodes,N to exit...";
cin>>ch;
}
return 0;
}
node*create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void push(node*np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np->next=save;
}
}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"!!!\n";
}
OUTPUT:
16. Write a program to sort 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 selection 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();
}
OUTPUT:
17. Write a program to create a text file to input roll no. and marks of ten students and
display them on screen after reading from the text file using data file handling.

#include<fstream.h>

#include<conio.h>

void main()

clrscr();

int a[10]; float b[10];

for(int i=0; i<10; i++)

cout<<”\n\nEnter the roll no. of student”<<i+1<<” : “; cin>>a[i];

cout<<“\n\tEnter the marks of student “<<i+1<<” => “; cin>>b[i];

ofstream student;
student.open(“stud.txt”);

for(i=0; i<10; i++)

{ student<<“\n\nMarks of roll no. “<<a[i]<<” => “<<b[i];

student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open(“stud.txt”,ios::in);

stude.seekg(0);

while(!stude.eof())

{stude.getline(str[i],80);
cout<<str[i]<<“\n\n”;

i++;}

getch();

OUTPUT:

You might also like