Practical Assignment (XII CS) 2013-14 c++ LIST-1
Assignment on Programs for Practical Assignment (XII CS) 2013-14 LIST-1
Any computational based problems
Program 1: Write a program in C++ to do the following :
• Read an integer number X.
• Determine the number of digits n in X.
• Form an integer Y by reversing the digits of X and integer S having sum of digits of X.
• Check whether integer X is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include"math.h"
void main()
{ int x,y=0,dig,n=0,s=0,num;
int flag=0;
cout<<"\n\n Enter a number(other than 1/2)";
cin>>x;
num=x;
while(x)
{dig=x%10;
s=s+dig;
y=y*10+dig;
x=x/10;
n++;
}
cout<<"\n No. of digit= "<<n;
cout<<"\n Sum of all digits= "<<s;
cout<<"\n And reverse of No "<<num<<" is= "<<y;
// To check Number is prime or not
int t=sqrt(num)+1;
for(int i=2;i<=t;i++)
{ if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"\n Yes Number "<<num<<" is a Prime Number";
}
else
{
cout<<"\n Yes Number "<<num<<" is not a Prime Number";
}
getch();
}
Program 2: Write a program in C++, which read an operator and two numbers print result according operator.
include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{ clrscr();
char op,ch;
int a,b, res;
do
{ cout<<"\n\n Enter any one of the operators(+,-,*,/) \n";
cin>>op;
cout<<"\n Enter two numbers :- ";
cin>>a>>b;
switch(op)
{ case '+':
res=a+b;
cout<<"\n Here is sum of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '-':
res=a-b;
cout<<"\n Here is difference i.e (a-b) of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '*':
res=a*b;
cout<<"\n Here is Product of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '/':
res=a/b;
cout<<"\n Here is Quotient of two nos."<<a<<" and "<<b<<"is = "<<res;
cout<<"\n Here is Remainder of two nos."<<a<<" and "<<b<<"is = "<<a%b;
break;
default:
cout<<"\n Your choice is out of menu ::-"; }
cout<<"\n\nWould you want give another choice for yes enter(Y/y):-";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}
Arrays (One dimensional and two dimensional) in C++
Program 3: Write a program in C++, which read two matrices and print their product it these are compatible for multiplication.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[10][10], B[10][10],C[10][10],i,j,p,k,r1,c1,r2,c2,r,c;
void displayM(int[10][10],int, int);
void product(int[10][10],int[10][10],int,int,int); //prototype
cout<<" Enter the number of rows and columns of first matrix : ";
cin>>r1>>c1;
cout<<"\n Matrix 1 : rows = "<<r1<<"\ column = "<<c1;
cout<<"\n Enter the number of rows and columns of second matrix : ";
cin>>r2>>c2;
cout<<"\n Matrix 2 : rows = "<<r2<<"\ column = "<<c2;
r=r1;
c=c2;
p=c1;
if(c1==r2)
{ cout<<"\n\n\tYes the matrices are compatible for multiplication...";
cout<<"\n Enter the elements of first matrix";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Enter the elements of second matrix";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>B[i][j];
}
}
cout<<"\n First Matrix is ";displayM(A,r1,c1);
cout<<"\n Second Matrix is ";displayM(B,r2,c2);
cout<<"\n\t The product of two matrices is ";
product(A,B,r,c,p); //calling of function
}
else
{
cout<<"\nThe two matrices are not compatible for multiplication..";
}
getch();
}
void product(int X[10][10],int Y[10][10],int R,int C,int P)
{ int Z[10][10];
for(int i=0;i<R;i++)
{
cout<<endl;
for(int j=0;j<C;j++)
{ int s=0;
for(int k=0;k<P;k++)
{
s=s+X[i][k]*Y[k][j];
Z[i][j]=s;
}
cout<<Z[i][j]<<" ";
}
}
}
void displayM(int x[10][10],int r,int c)
{ for(int i=0;i<r;i++)
{
cout<<endl;
for(int j=0;j<c;j++)
{cout<<x[i][j]<<" ";
}
}
}
Program 4: Write a program in C++, which store information of 10 employees and to display information of an employee depending upon the employee no. given.(Using nested structure for address)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct Address
{
char hno[10];
char lane[16];
char area[25];
char city[25];
char dist[25];
long pin;
};
struct emp
{
char name[20];
int eno;
char dsg[25];
float sal;
Address A;
};
void main()
{
const int n=2;
clrscr();
emp e[n];
int i,en,flg=0;
cout<<"Enter Details of "<<n<<" employees:-";
for(i=0;i<n;i++)
{
cout<<"\n\n\n Enter name,eno(int),designation and salar(float) of "<<i+1<<" employee: ";
gets(e[i].name);cin>>e[i].eno;gets(e[i].dsg);cin>>e[i].sal;
cout<<"\n Enter address of the "<<i+1<<" employee :";
cout<<"\n Enter house no.,lane,area,city,district and pin code:\n ";
gets(e[i].A.hno); gets(e[i].A.lane); gets(e[i].A.area);
gets(e[i].A.city); gets(e[i].A.dist);cin>>e[i].A.pin;
}
cout<<"\n Enter any Employee No.";
cin>>en;
for(i=0;i<n;i++)
{
if(en==e[i].eno)
{
flg=1;
cout<<"\n Employee is found and its information is:\n";
cout<<"Name is :";puts(e[i].name);
cout<<"\n Employee No.:"<<e[i].eno;
cout<<"\n Designation is :";puts(e[i].dsg);
cout<<"\n Salary is :"<<e[i].sal;
cout<<"\n Address is :";
puts(e[i].A.hno);
puts(e[i].A.lane);
puts(e[i].A.area);
puts(e[i].A.city);
puts(e[i].A.dist);
cout<<e[i].A.pin;
} }
if(flg==0)
{ cout<<"\n There is no employee of such employee No.";
} getch(); }
Write a menu driven Program in C++ to
Program 5: Write a menu driven Program in C++ to
1. Find length of string
2. Compare two Strings
3. Concatenate two strings
4. Exit
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#define PI 3.14
void main()
{
int i,j,k,flag=1,len=0;
char str1[25],str2[25],str3[50],ch,choice;
clrscr();
do
{
cout<<"\n ---Menu---\n";
cout<<"\n a. To check inputted string is Palindrome \n";
cout<<"\n b. To concatenate two strings \n";
cout<<"\n c. To compare two strings \n";
cout<<"\n d. Exit program \n";
cout<<"\n\n Enter your choice ";
cin>>choice;
switch(choice)
{
case 'a':
cout<<"\n Enter a string:";
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
len++;
}
for(i=0,j=len-1;i<len/2;i++,j--)
{ if(str1[i]!=str1[j])
{
flag=0;
break;
}
}
if(flag)
{ cout<<"\n Inputted string is a palindrome String \n";
}
if(flag==0)
{
cout<<"\n Inputted string is not palindrome";
}
break;
case 'b':
cout<<"\n --- Concatenate of two strings ---\n";
cout<<"\n Enter first string:";
gets(str1);
cout<<"\n Enter second string:";
gets(str2);
for(i=0;str1[i]!='\0';i++)
{ str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{ str3[i+j]=str2[j];
}
str3[i+j]='\0';
puts(str3);
break;
case 'c':
cout<<"\n --- Comparison of two strings ---\n";
cout<<"\n Enter first string:";
gets(str1);
cout<<"\n Enter second string:";
gets(str2);
int lag=strcmp(str1,str2);
if(lag==0)
cout<<"\n Both string is same\n ";
if(lag<0)
cout<<"\n First string is smaller than second";
if(lag>0)
cout<<"\n Second string is smaller than first";
break;
case 'd':
cout<<"\n You are exiting from program";
exit(0);
default:
cout<<"\n Your choice is wrong ";
}
cout<<"\n Do you continue press(Y/y) ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Program 6: Write menu driven program in C++ for following:-
1. Area of rectangle.
2. Perimeter of rectangle.
3. Diagonal of rectangle.
4. To Exit.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
clrscr();
float l,b,d,a,p;
int choice;
char ch;
cout<<"\n\n ----MENU----\n";
cout<<"\n 1. Area of Rectangle. ";
cout<<"\n 2. Perimeter of Rectangle. ";
cout<<"\n 3. Diagonal of Rectangle. ";
cout<<"\n 4. Exit. ";
do
{
cout<<"\n Enter your Choice(1/2/3/4)::-";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
a=l*b;
cout<<"\nArea of rectangle= "<<a;
break;
case 2:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
p=2*(l+b);
cout<<"\nPerimeter of rectangle= "<<p;
break;
case 3:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
d=sqrt(l*l+b*b);
cout<<"\n Diagonal of rectangle= "<<d;
break;
case 4:
// Yes Now Program Exit ::";
exit(0);
default:
cout<<"\n Your choice is out of menu ::-";
}
cout<<"\n\nWould you want give another choice for yes enter(Y/y):-";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}
Program 7 : Write a C++ program to use the following function:
(i) display( ) to display a matrix of a size m x n.
(ii) times2() to double each number of the matrix of size m x n.
(iii) main( ) to read a matrix of size m x n and then to display original matrix and then to display the new matrix formed by doubling its elements.
include<iostream.h>
#include<conio.h>
void main()
{
int A[10][10],i,j,r,c;
clrscr();
void display(int[10][10],int,int);
void times2(int[10][10],int,int);
cout<<"Enter the no. or row and columns ::";
cin>>r>>c;
cout<<"Enter the elements of matrix";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Here is entered matrix :\n";
display(A,r,c);
cout<<"\n Here is matrix after doubling each elements :\n";
times2(A,r,c);
display(A,r,c);
getch();
}
void display(int B[10][10],int R,int C)
{
int i,j;
for(i=0;i<R;i++)
{ cout<<endl;
for(j=0;j<C;j++)
{
cout<<B[i][j]<<" ";
}
}
}
void times2(int B[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
B[i][j]=2*B[i][j];
}
}
}
Implementation of Object Oriented Programming Concepts in C++
Program 8 - Write a program to accept information about a person and then display it.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
char name[30];
long phone;
public:
person()
{
cout<<"\nConstructor initialised\n";
}
void getname()
{
cout<<"\nEnter the name\n";
gets(name);
cout<<"\nEnter the phone number\n";
cin>>phone;
}
void display()
{
cout<<"\nName\t";puts(name);
cout<<"\nPhone number\t"<<phone;
}
~person()
{
cout<<"\n******************\n";
}
};
class spouse:public person
{
char spousename[40];
public:
void getsp();
void display();
};
void spouse::getsp()
{
person::getname();
cout<<"\nEnter the spouse name\n";
gets(spousename);
}
void spouse::display()
{
person::display();
cout<<"Spouse name\t";puts(spousename);
}
void main()
{
clrscr();
spouse obj;
obj.getsp();
obj.display();
getch();
}
I
Program 9 - Write a program to calculate the no. of types of guests i.e gentlemen or ladies or children using a class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
void incount()
{
count++;
}
int givecount()
{
return (count);
}
}c1,c2,c3;
void main()
{
clrscr();
char guest[10];
int i,a,b,c;
for(i=0;i<10;i++)
{
cout<<"enter gntlmn(g),ladies(l),chldrn(c) "<<endl;
cin>>guest[i];
if(guest[i]=='g'||guest[i]=='G')
{
c1.incount();
a=c1.givecount();
}
else if(guest[i]=='l'||guest[i]=='L')
{
c2.incount();
b=c2.givecount();
}
else
{
c3.incount();
c=c3.givecount();
}
}
cout<<"GENTLEMEN :"<<a<<endl;
cout<<"LADIES :"<<b<<endl;
cout<<"CHILDREN :"<<c<<endl;
getch();
}
Program 10 - Write a c++ program using class serial to store and display serial's title, duration, number of episodes,serial code.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
int code;
char title[20];
float duration;
int noe;
public:
serial()
{
duration=30;
noe=103;
}
void newserial();
void otherentries(int dur,int no);
void displaydata();
};
void serial::newserial()
{
cout<<"\n\nEnter the serial code:";
cin>>code;
cout<<"Enter the title:";
gets(title);
}
void serial::otherentries(int dur,int no)
{
duration=dur;
noe=no;
}
void serial::displaydata()
{
cout<<"\tSerial code is:"<<code<<endl;
cout<<"\tTitle is:"<<title<<endl;
cout<<"\tDurations is:"<<duration<<endl;
cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}
int main()
{
clrscr();
char ch='y';
int i=0,dur,no;
serial s1[10];
while(ch=='y')
{
s1[i].newserial();
cout<<"Enter the duration and the no. of episodes:";
cin>>dur>>no;
s1[i].otherentries(dur,no);
i++;
cout<<"\n\nDo you want to continue:";
cin>>ch;
}
cout<<"\n\nThe details you have entered are:"<<endl<<endl;
for(int j=0;j<i;j++){
cout<<"Data of serial "<<j+1<<" is:"<<endl;
s1[j].displaydata();
}
return 0; }
Program 11 - Write a c++ program to illustrate a calculator. Perform calculations on two operands using a class calculator. Calculator should add, subtract, multiply and divide operands.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class calculator
{
float result;
int o1,o2;
public:
void enter();
void showresult();
void add();
void sub();
void mul();
void div();
void clear();
};
void calculator::enter()
{
cout<<"Enter a operant:";
cin>>o1;
cout<<"Enter the other operant:";
cin>>o2;
}
void calculator::showresult()
{
cout<<"The result of the operation is:"<<result<<endl;
}
void calculator::add()
{
result=o2+o1;
}
void calculator::sub()
{
result=o1-o1;
}
void calculator::mul()
{
result=o1*o2;
}
void calculator::div()
{
result=o1/o2;
}
void calculator::clear()
{
result=0;
}
int main()
{
char ch='y';
calculator c1;
while(ch=='y')
{
c1.enter();
cout<<"Which operation do you want to perform:";
cin>>ch;
switch(ch)
{
case '+':c1.add();
break;
case '-':c1.sub();
break;
case '*':c1.mul();
break;
case '/':c1.div();
break;
default :cout<<"Wrong choice:";
exit(0);
}
c1.showresult();
c1.clear();
cout<<"Do you want to continue:";
cin>>ch;
}
return 0;
}
Constructor and Destructor:
Program 12- A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stock
{
char author[50];
char title[50];
char pub[50];
double price;
int numcopies;
public:
stock();
int access_title(char a[]);
void input();
void getdata(int);
};
stock::stock()
{
char author[50]={"abc"};
char title[50]={"efg"};
char pub[50]={"hij"};
price=500;
numcopies=50;
}
int stock::access_title(char a[])
{
if(strcmp(title,a))
return 0;
else return 1;
}
void stock::getdata(int num)
{
if(numcopies>=num)
cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);
else
cout<<"\nSorry! These many copies are unavailable!";
}
void stock::input()
{
cout<<"\nTitle: ";
gets(title);
cout<<"\nAuthor:";
gets(author);
cout<<"\nPublisher:";
gets(pub);
cout<<"\nPrices:";
cin>>price;
cout<<"\ncopies available:";
cin>>numcopies;
}
void main()
{
clrscr();
stock obj[2];
int n;
char ttle[50];
cout<<"Enter details of 3 books";
for(int i=0;i<2;++i)
obj[i].input();
cout<<endl;
cout<<"\n Enter title of required book\n";
gets(ttle);
for(i=0;i<2;i++)
{
if(obj[i].access_title(ttle))
{
cout<<"\nHow many copies? ";
cin>>n;
obj[i].getdata(n);
}
else
cout<<"\nBook unavailable";
}
getch();
}
Inheritance:
Program 13: Program to implement Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
//class result;
//class test;
class student
{
protected:
int rollno;
public:
void get_number(int a)
{
rollno=a;
}
void put_number()
{
cout<<"Rollno: "<<rollno<<endl;
}
};
class test : public student //class test publically accesses class student
{ //1st level inheritance
protected:
float subj1,subj2;
public:
void get_marks(float x,float y)
{
subj1=x;
subj2=y;
}
void put_marks()
{
cout<<"Marks in subject 1: "<<subj1<<endl;
cout<<"Marks in subject 2: "<<subj2<<endl;
}
};
class result : public test /*class result inherits class test publically*/
{
float total;
public:
void display()
{
total=subj1+subj2;
put_number();
put_marks();
cout<<"Total Marks= "<<total;
}
};
void main()
{ clrscr();
result s1;
int a;
float b,c;
cout<<"Enter Rollno:";
cin>>a;
cout<<"Enter Marks in sub 1: ";
cin>>b;
cout<<"Enter Marks in sub 2: ";
cin>>c;
s1.get_number(a);
s1.get_marks(b,c);
s1.display();
getch();
}
File Handling:
Program 14:
// File Handling in C++
/* Develop a program to read the text from a source file and display the
same on the screen */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
char c,fname[30];
cout<<"Enter the File Name"<<endl;
cin>>fname;
k.open(fname);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
while(!k.eof())
{
c=(char)k.get();
cout.put(c);
}
k.close();
getch();
}
/*
where open(), fail(), close(), get() are methods of 'ifstream.h' by
including header file.
*/
Program 15 - Write a C++ program, which initialises a string variable to the content. "time is great teacher but unfortunately it kills all its pupils. Berlioz"
and output the string one character atra time to the disk file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
int main()
{
char str[]="Time is a great teacher but unfortunately it kills all its
pupils.Berlioz",ch;
int i;
ofstream fout;
fout.open("OUT.TXT",ios::out);
int len=strlen(str);
for(i=0;i<len;i++){
ch=str[i];
fout.put(ch);
}
cout<<"The string has been successfully written into the file";
return 0;
}
Program 16: Write a program to write records of 10 students in a file say "vrdestd.dat" and search a record of student of inputtedRoll No. Assume structure name is student & it contains following variables:rollno of integer, name as string.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<process.h>
struct student
{int rollno;
char name[25];
};
void main()
{
int i;
const int size=3;
student s;
clrscr();
ifstream fin;
ofstream fout;
fout.open("vrdestd.dat",ios::out|ios::binary);
cout<<"\n Enter Record of a student";
cout<<" \n Enter Rollno and Name of "<<size<<" students";
for(i=0;i<size;i++)
{ cout<<"\n Enter rollno :: ";cin>>s.rollno;
cout<<"\n Enter name :: "; gets(s.name);
fout.write((char*)&s,sizeof(s));
}//5 Records are written in file
fout.close();
fin.open("vrdestd.dat",ios::in|ios::binary);
int rn,flag=0;
cout<<"\n Enter Rollno to be search :: ";cin>>rn;
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(s.rollno==rn)
{
cout<<"Rollno \t Name"<<endl;
cout<<s.rollno<<"\t"<<s.name<<endl;
flag=1;
break;
}
}
fin.close();
if(flag==0)
{
cout<<"\n No Record is found";
}
getch();
}
Program 17:
Write a program to merge content of two text files.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
fstream f1,f2,f3;
f1.open("f1.txt",ios::in);
if(!f1)
{
cout<<"File 1 Read operation failed ";
exit(0);
}
f2.open("f2.txt",ios::in);
if(!f2)
{
cout<<"File 2 Read operation failed ";
exit(0);
}
f3.open("f3.txt",ios::out);
if(!f3)
{
cout<<"File Read operation failed ";
exit(0);
}
cout<<"\nContent of file 1 is:\n ";
while(f1.eof()==0)
{
char c;
f1>>c;
cout<<c;
f3<<c;
}
cout<<"\nContent of file 2 is: \n ";
while(f2.eof()==0)
{
char c;
f2>>c;
cout<<c;
f3<<c;
}
f1.close();
f2.close();
f3.close();
cout<<"\nContent of file 3 after merging f1 and f2 is: \n ";
f3.open("f3.txt",ios::in);
while(f3.eof()==0)
{
char c;
f3>>c;
cout<<c;
}
f3.close();
getch();
}
Program 18:
/* Copy the contents of one text file into another text file */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
ofstream o;
char c,fname1[30],fname2[30];
cout<<"Enter the Source File Name"<<endl;
cin>>fname1;
k.open(fname1);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
cout<<"Enter the Objective File Name"<<endl;
cin>>fname2;
o.open(fname2);
if(o.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
while(!k.eof())
{
c=(char)k.get();
o.put(c);
}
cout<<"The contents of "<<fname1<<" file are Copied into "<<fname2<<" file";
k.close();
o.close();
getch();
}
Program 19:
// Creation of a binary file
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ofstream k;
char name[20];
cout<<"Enter the Entire Path";
cin>>name;
k.open(name,ios::out|ios::binary);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
int x=5,y=3;
k<<x<<y;
cout<<"The Values are Written into the "<<name<<" file";
k.close();
getch();
}
Program 20 :
// Reading from binary file
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
char name[20];
cout<<"Enter the Entire Path";
cin>>name;
k.open(name,ios::in|ios::binary);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
cout<<"The File Contents are\n";
int x,y;
while(!k.eof())
{
k>>x>>y;
cout<<x<<endl<<y;
}
k.close();
getch();
}
Pointers:
Program 21 - Suppose 7 names are stored in an array of pointers names[] as shown below. Write a program to reverse the order of these names.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[]={"anand","naureen","banjot","wahid","sheena"};
int i,j;
cout<<"\nOriginal string\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
char *t;
for(i=0,j=4;i<5/2;i++,j--)
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
cout<<"\nReversed string:\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
getch();
}
Any computational based problems
Program 1: Write a program in C++ to do the following :
• Read an integer number X.
• Determine the number of digits n in X.
• Form an integer Y by reversing the digits of X and integer S having sum of digits of X.
• Check whether integer X is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include"math.h"
void main()
{ int x,y=0,dig,n=0,s=0,num;
int flag=0;
cout<<"\n\n Enter a number(other than 1/2)";
cin>>x;
num=x;
while(x)
{dig=x%10;
s=s+dig;
y=y*10+dig;
x=x/10;
n++;
}
cout<<"\n No. of digit= "<<n;
cout<<"\n Sum of all digits= "<<s;
cout<<"\n And reverse of No "<<num<<" is= "<<y;
// To check Number is prime or not
int t=sqrt(num)+1;
for(int i=2;i<=t;i++)
{ if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"\n Yes Number "<<num<<" is a Prime Number";
}
else
{
cout<<"\n Yes Number "<<num<<" is not a Prime Number";
}
getch();
}
Program 2: Write a program in C++, which read an operator and two numbers print result according operator.
include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{ clrscr();
char op,ch;
int a,b, res;
do
{ cout<<"\n\n Enter any one of the operators(+,-,*,/) \n";
cin>>op;
cout<<"\n Enter two numbers :- ";
cin>>a>>b;
switch(op)
{ case '+':
res=a+b;
cout<<"\n Here is sum of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '-':
res=a-b;
cout<<"\n Here is difference i.e (a-b) of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '*':
res=a*b;
cout<<"\n Here is Product of two nos. "<<a<<" and "<<b<<" is = "<<res;
break;
case '/':
res=a/b;
cout<<"\n Here is Quotient of two nos."<<a<<" and "<<b<<"is = "<<res;
cout<<"\n Here is Remainder of two nos."<<a<<" and "<<b<<"is = "<<a%b;
break;
default:
cout<<"\n Your choice is out of menu ::-"; }
cout<<"\n\nWould you want give another choice for yes enter(Y/y):-";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}
Arrays (One dimensional and two dimensional) in C++
Program 3: Write a program in C++, which read two matrices and print their product it these are compatible for multiplication.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[10][10], B[10][10],C[10][10],i,j,p,k,r1,c1,r2,c2,r,c;
void displayM(int[10][10],int, int);
void product(int[10][10],int[10][10],int,int,int); //prototype
cout<<" Enter the number of rows and columns of first matrix : ";
cin>>r1>>c1;
cout<<"\n Matrix 1 : rows = "<<r1<<"\ column = "<<c1;
cout<<"\n Enter the number of rows and columns of second matrix : ";
cin>>r2>>c2;
cout<<"\n Matrix 2 : rows = "<<r2<<"\ column = "<<c2;
r=r1;
c=c2;
p=c1;
if(c1==r2)
{ cout<<"\n\n\tYes the matrices are compatible for multiplication...";
cout<<"\n Enter the elements of first matrix";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Enter the elements of second matrix";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>B[i][j];
}
}
cout<<"\n First Matrix is ";displayM(A,r1,c1);
cout<<"\n Second Matrix is ";displayM(B,r2,c2);
cout<<"\n\t The product of two matrices is ";
product(A,B,r,c,p); //calling of function
}
else
{
cout<<"\nThe two matrices are not compatible for multiplication..";
}
getch();
}
void product(int X[10][10],int Y[10][10],int R,int C,int P)
{ int Z[10][10];
for(int i=0;i<R;i++)
{
cout<<endl;
for(int j=0;j<C;j++)
{ int s=0;
for(int k=0;k<P;k++)
{
s=s+X[i][k]*Y[k][j];
Z[i][j]=s;
}
cout<<Z[i][j]<<" ";
}
}
}
void displayM(int x[10][10],int r,int c)
{ for(int i=0;i<r;i++)
{
cout<<endl;
for(int j=0;j<c;j++)
{cout<<x[i][j]<<" ";
}
}
}
Program 4: Write a program in C++, which store information of 10 employees and to display information of an employee depending upon the employee no. given.(Using nested structure for address)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct Address
{
char hno[10];
char lane[16];
char area[25];
char city[25];
char dist[25];
long pin;
};
struct emp
{
char name[20];
int eno;
char dsg[25];
float sal;
Address A;
};
void main()
{
const int n=2;
clrscr();
emp e[n];
int i,en,flg=0;
cout<<"Enter Details of "<<n<<" employees:-";
for(i=0;i<n;i++)
{
cout<<"\n\n\n Enter name,eno(int),designation and salar(float) of "<<i+1<<" employee: ";
gets(e[i].name);cin>>e[i].eno;gets(e[i].dsg);cin>>e[i].sal;
cout<<"\n Enter address of the "<<i+1<<" employee :";
cout<<"\n Enter house no.,lane,area,city,district and pin code:\n ";
gets(e[i].A.hno); gets(e[i].A.lane); gets(e[i].A.area);
gets(e[i].A.city); gets(e[i].A.dist);cin>>e[i].A.pin;
}
cout<<"\n Enter any Employee No.";
cin>>en;
for(i=0;i<n;i++)
{
if(en==e[i].eno)
{
flg=1;
cout<<"\n Employee is found and its information is:\n";
cout<<"Name is :";puts(e[i].name);
cout<<"\n Employee No.:"<<e[i].eno;
cout<<"\n Designation is :";puts(e[i].dsg);
cout<<"\n Salary is :"<<e[i].sal;
cout<<"\n Address is :";
puts(e[i].A.hno);
puts(e[i].A.lane);
puts(e[i].A.area);
puts(e[i].A.city);
puts(e[i].A.dist);
cout<<e[i].A.pin;
} }
if(flg==0)
{ cout<<"\n There is no employee of such employee No.";
} getch(); }
Write a menu driven Program in C++ to
Program 5: Write a menu driven Program in C++ to
1. Find length of string
2. Compare two Strings
3. Concatenate two strings
4. Exit
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#define PI 3.14
void main()
{
int i,j,k,flag=1,len=0;
char str1[25],str2[25],str3[50],ch,choice;
clrscr();
do
{
cout<<"\n ---Menu---\n";
cout<<"\n a. To check inputted string is Palindrome \n";
cout<<"\n b. To concatenate two strings \n";
cout<<"\n c. To compare two strings \n";
cout<<"\n d. Exit program \n";
cout<<"\n\n Enter your choice ";
cin>>choice;
switch(choice)
{
case 'a':
cout<<"\n Enter a string:";
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
len++;
}
for(i=0,j=len-1;i<len/2;i++,j--)
{ if(str1[i]!=str1[j])
{
flag=0;
break;
}
}
if(flag)
{ cout<<"\n Inputted string is a palindrome String \n";
}
if(flag==0)
{
cout<<"\n Inputted string is not palindrome";
}
break;
case 'b':
cout<<"\n --- Concatenate of two strings ---\n";
cout<<"\n Enter first string:";
gets(str1);
cout<<"\n Enter second string:";
gets(str2);
for(i=0;str1[i]!='\0';i++)
{ str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{ str3[i+j]=str2[j];
}
str3[i+j]='\0';
puts(str3);
break;
case 'c':
cout<<"\n --- Comparison of two strings ---\n";
cout<<"\n Enter first string:";
gets(str1);
cout<<"\n Enter second string:";
gets(str2);
int lag=strcmp(str1,str2);
if(lag==0)
cout<<"\n Both string is same\n ";
if(lag<0)
cout<<"\n First string is smaller than second";
if(lag>0)
cout<<"\n Second string is smaller than first";
break;
case 'd':
cout<<"\n You are exiting from program";
exit(0);
default:
cout<<"\n Your choice is wrong ";
}
cout<<"\n Do you continue press(Y/y) ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Program 6: Write menu driven program in C++ for following:-
1. Area of rectangle.
2. Perimeter of rectangle.
3. Diagonal of rectangle.
4. To Exit.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
clrscr();
float l,b,d,a,p;
int choice;
char ch;
cout<<"\n\n ----MENU----\n";
cout<<"\n 1. Area of Rectangle. ";
cout<<"\n 2. Perimeter of Rectangle. ";
cout<<"\n 3. Diagonal of Rectangle. ";
cout<<"\n 4. Exit. ";
do
{
cout<<"\n Enter your Choice(1/2/3/4)::-";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
a=l*b;
cout<<"\nArea of rectangle= "<<a;
break;
case 2:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
p=2*(l+b);
cout<<"\nPerimeter of rectangle= "<<p;
break;
case 3:
cout<<"\n Enter Length and Breadth ::";
cin>>l>>b;
d=sqrt(l*l+b*b);
cout<<"\n Diagonal of rectangle= "<<d;
break;
case 4:
// Yes Now Program Exit ::";
exit(0);
default:
cout<<"\n Your choice is out of menu ::-";
}
cout<<"\n\nWould you want give another choice for yes enter(Y/y):-";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}
Program 7 : Write a C++ program to use the following function:
(i) display( ) to display a matrix of a size m x n.
(ii) times2() to double each number of the matrix of size m x n.
(iii) main( ) to read a matrix of size m x n and then to display original matrix and then to display the new matrix formed by doubling its elements.
include<iostream.h>
#include<conio.h>
void main()
{
int A[10][10],i,j,r,c;
clrscr();
void display(int[10][10],int,int);
void times2(int[10][10],int,int);
cout<<"Enter the no. or row and columns ::";
cin>>r>>c;
cout<<"Enter the elements of matrix";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Here is entered matrix :\n";
display(A,r,c);
cout<<"\n Here is matrix after doubling each elements :\n";
times2(A,r,c);
display(A,r,c);
getch();
}
void display(int B[10][10],int R,int C)
{
int i,j;
for(i=0;i<R;i++)
{ cout<<endl;
for(j=0;j<C;j++)
{
cout<<B[i][j]<<" ";
}
}
}
void times2(int B[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
B[i][j]=2*B[i][j];
}
}
}
Implementation of Object Oriented Programming Concepts in C++
Program 8 - Write a program to accept information about a person and then display it.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
char name[30];
long phone;
public:
person()
{
cout<<"\nConstructor initialised\n";
}
void getname()
{
cout<<"\nEnter the name\n";
gets(name);
cout<<"\nEnter the phone number\n";
cin>>phone;
}
void display()
{
cout<<"\nName\t";puts(name);
cout<<"\nPhone number\t"<<phone;
}
~person()
{
cout<<"\n******************\n";
}
};
class spouse:public person
{
char spousename[40];
public:
void getsp();
void display();
};
void spouse::getsp()
{
person::getname();
cout<<"\nEnter the spouse name\n";
gets(spousename);
}
void spouse::display()
{
person::display();
cout<<"Spouse name\t";puts(spousename);
}
void main()
{
clrscr();
spouse obj;
obj.getsp();
obj.display();
getch();
}
I
Program 9 - Write a program to calculate the no. of types of guests i.e gentlemen or ladies or children using a class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
void incount()
{
count++;
}
int givecount()
{
return (count);
}
}c1,c2,c3;
void main()
{
clrscr();
char guest[10];
int i,a,b,c;
for(i=0;i<10;i++)
{
cout<<"enter gntlmn(g),ladies(l),chldrn(c) "<<endl;
cin>>guest[i];
if(guest[i]=='g'||guest[i]=='G')
{
c1.incount();
a=c1.givecount();
}
else if(guest[i]=='l'||guest[i]=='L')
{
c2.incount();
b=c2.givecount();
}
else
{
c3.incount();
c=c3.givecount();
}
}
cout<<"GENTLEMEN :"<<a<<endl;
cout<<"LADIES :"<<b<<endl;
cout<<"CHILDREN :"<<c<<endl;
getch();
}
Program 10 - Write a c++ program using class serial to store and display serial's title, duration, number of episodes,serial code.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
int code;
char title[20];
float duration;
int noe;
public:
serial()
{
duration=30;
noe=103;
}
void newserial();
void otherentries(int dur,int no);
void displaydata();
};
void serial::newserial()
{
cout<<"\n\nEnter the serial code:";
cin>>code;
cout<<"Enter the title:";
gets(title);
}
void serial::otherentries(int dur,int no)
{
duration=dur;
noe=no;
}
void serial::displaydata()
{
cout<<"\tSerial code is:"<<code<<endl;
cout<<"\tTitle is:"<<title<<endl;
cout<<"\tDurations is:"<<duration<<endl;
cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}
int main()
{
clrscr();
char ch='y';
int i=0,dur,no;
serial s1[10];
while(ch=='y')
{
s1[i].newserial();
cout<<"Enter the duration and the no. of episodes:";
cin>>dur>>no;
s1[i].otherentries(dur,no);
i++;
cout<<"\n\nDo you want to continue:";
cin>>ch;
}
cout<<"\n\nThe details you have entered are:"<<endl<<endl;
for(int j=0;j<i;j++){
cout<<"Data of serial "<<j+1<<" is:"<<endl;
s1[j].displaydata();
}
return 0; }
Program 11 - Write a c++ program to illustrate a calculator. Perform calculations on two operands using a class calculator. Calculator should add, subtract, multiply and divide operands.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class calculator
{
float result;
int o1,o2;
public:
void enter();
void showresult();
void add();
void sub();
void mul();
void div();
void clear();
};
void calculator::enter()
{
cout<<"Enter a operant:";
cin>>o1;
cout<<"Enter the other operant:";
cin>>o2;
}
void calculator::showresult()
{
cout<<"The result of the operation is:"<<result<<endl;
}
void calculator::add()
{
result=o2+o1;
}
void calculator::sub()
{
result=o1-o1;
}
void calculator::mul()
{
result=o1*o2;
}
void calculator::div()
{
result=o1/o2;
}
void calculator::clear()
{
result=0;
}
int main()
{
char ch='y';
calculator c1;
while(ch=='y')
{
c1.enter();
cout<<"Which operation do you want to perform:";
cin>>ch;
switch(ch)
{
case '+':c1.add();
break;
case '-':c1.sub();
break;
case '*':c1.mul();
break;
case '/':c1.div();
break;
default :cout<<"Wrong choice:";
exit(0);
}
c1.showresult();
c1.clear();
cout<<"Do you want to continue:";
cin>>ch;
}
return 0;
}
Constructor and Destructor:
Program 12- A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stock
{
char author[50];
char title[50];
char pub[50];
double price;
int numcopies;
public:
stock();
int access_title(char a[]);
void input();
void getdata(int);
};
stock::stock()
{
char author[50]={"abc"};
char title[50]={"efg"};
char pub[50]={"hij"};
price=500;
numcopies=50;
}
int stock::access_title(char a[])
{
if(strcmp(title,a))
return 0;
else return 1;
}
void stock::getdata(int num)
{
if(numcopies>=num)
cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);
else
cout<<"\nSorry! These many copies are unavailable!";
}
void stock::input()
{
cout<<"\nTitle: ";
gets(title);
cout<<"\nAuthor:";
gets(author);
cout<<"\nPublisher:";
gets(pub);
cout<<"\nPrices:";
cin>>price;
cout<<"\ncopies available:";
cin>>numcopies;
}
void main()
{
clrscr();
stock obj[2];
int n;
char ttle[50];
cout<<"Enter details of 3 books";
for(int i=0;i<2;++i)
obj[i].input();
cout<<endl;
cout<<"\n Enter title of required book\n";
gets(ttle);
for(i=0;i<2;i++)
{
if(obj[i].access_title(ttle))
{
cout<<"\nHow many copies? ";
cin>>n;
obj[i].getdata(n);
}
else
cout<<"\nBook unavailable";
}
getch();
}
Inheritance:
Program 13: Program to implement Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
//class result;
//class test;
class student
{
protected:
int rollno;
public:
void get_number(int a)
{
rollno=a;
}
void put_number()
{
cout<<"Rollno: "<<rollno<<endl;
}
};
class test : public student //class test publically accesses class student
{ //1st level inheritance
protected:
float subj1,subj2;
public:
void get_marks(float x,float y)
{
subj1=x;
subj2=y;
}
void put_marks()
{
cout<<"Marks in subject 1: "<<subj1<<endl;
cout<<"Marks in subject 2: "<<subj2<<endl;
}
};
class result : public test /*class result inherits class test publically*/
{
float total;
public:
void display()
{
total=subj1+subj2;
put_number();
put_marks();
cout<<"Total Marks= "<<total;
}
};
void main()
{ clrscr();
result s1;
int a;
float b,c;
cout<<"Enter Rollno:";
cin>>a;
cout<<"Enter Marks in sub 1: ";
cin>>b;
cout<<"Enter Marks in sub 2: ";
cin>>c;
s1.get_number(a);
s1.get_marks(b,c);
s1.display();
getch();
}
File Handling:
Program 14:
// File Handling in C++
/* Develop a program to read the text from a source file and display the
same on the screen */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
char c,fname[30];
cout<<"Enter the File Name"<<endl;
cin>>fname;
k.open(fname);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
while(!k.eof())
{
c=(char)k.get();
cout.put(c);
}
k.close();
getch();
}
/*
where open(), fail(), close(), get() are methods of 'ifstream.h' by
including header file.
*/
Program 15 - Write a C++ program, which initialises a string variable to the content. "time is great teacher but unfortunately it kills all its pupils. Berlioz"
and output the string one character atra time to the disk file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
int main()
{
char str[]="Time is a great teacher but unfortunately it kills all its
pupils.Berlioz",ch;
int i;
ofstream fout;
fout.open("OUT.TXT",ios::out);
int len=strlen(str);
for(i=0;i<len;i++){
ch=str[i];
fout.put(ch);
}
cout<<"The string has been successfully written into the file";
return 0;
}
Program 16: Write a program to write records of 10 students in a file say "vrdestd.dat" and search a record of student of inputtedRoll No. Assume structure name is student & it contains following variables:rollno of integer, name as string.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<process.h>
struct student
{int rollno;
char name[25];
};
void main()
{
int i;
const int size=3;
student s;
clrscr();
ifstream fin;
ofstream fout;
fout.open("vrdestd.dat",ios::out|ios::binary);
cout<<"\n Enter Record of a student";
cout<<" \n Enter Rollno and Name of "<<size<<" students";
for(i=0;i<size;i++)
{ cout<<"\n Enter rollno :: ";cin>>s.rollno;
cout<<"\n Enter name :: "; gets(s.name);
fout.write((char*)&s,sizeof(s));
}//5 Records are written in file
fout.close();
fin.open("vrdestd.dat",ios::in|ios::binary);
int rn,flag=0;
cout<<"\n Enter Rollno to be search :: ";cin>>rn;
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(s.rollno==rn)
{
cout<<"Rollno \t Name"<<endl;
cout<<s.rollno<<"\t"<<s.name<<endl;
flag=1;
break;
}
}
fin.close();
if(flag==0)
{
cout<<"\n No Record is found";
}
getch();
}
Program 17:
Write a program to merge content of two text files.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
fstream f1,f2,f3;
f1.open("f1.txt",ios::in);
if(!f1)
{
cout<<"File 1 Read operation failed ";
exit(0);
}
f2.open("f2.txt",ios::in);
if(!f2)
{
cout<<"File 2 Read operation failed ";
exit(0);
}
f3.open("f3.txt",ios::out);
if(!f3)
{
cout<<"File Read operation failed ";
exit(0);
}
cout<<"\nContent of file 1 is:\n ";
while(f1.eof()==0)
{
char c;
f1>>c;
cout<<c;
f3<<c;
}
cout<<"\nContent of file 2 is: \n ";
while(f2.eof()==0)
{
char c;
f2>>c;
cout<<c;
f3<<c;
}
f1.close();
f2.close();
f3.close();
cout<<"\nContent of file 3 after merging f1 and f2 is: \n ";
f3.open("f3.txt",ios::in);
while(f3.eof()==0)
{
char c;
f3>>c;
cout<<c;
}
f3.close();
getch();
}
Program 18:
/* Copy the contents of one text file into another text file */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
ofstream o;
char c,fname1[30],fname2[30];
cout<<"Enter the Source File Name"<<endl;
cin>>fname1;
k.open(fname1);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
cout<<"Enter the Objective File Name"<<endl;
cin>>fname2;
o.open(fname2);
if(o.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
while(!k.eof())
{
c=(char)k.get();
o.put(c);
}
cout<<"The contents of "<<fname1<<" file are Copied into "<<fname2<<" file";
k.close();
o.close();
getch();
}
Program 19:
// Creation of a binary file
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ofstream k;
char name[20];
cout<<"Enter the Entire Path";
cin>>name;
k.open(name,ios::out|ios::binary);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
int x=5,y=3;
k<<x<<y;
cout<<"The Values are Written into the "<<name<<" file";
k.close();
getch();
}
Program 20 :
// Reading from binary file
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream k;
char name[20];
cout<<"Enter the Entire Path";
cin>>name;
k.open(name,ios::in|ios::binary);
if(k.fail())
{
cerr<<"No Such File Exists";
exit(1);
}
cout<<"The File Contents are\n";
int x,y;
while(!k.eof())
{
k>>x>>y;
cout<<x<<endl<<y;
}
k.close();
getch();
}
Pointers:
Program 21 - Suppose 7 names are stored in an array of pointers names[] as shown below. Write a program to reverse the order of these names.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[]={"anand","naureen","banjot","wahid","sheena"};
int i,j;
cout<<"\nOriginal string\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
char *t;
for(i=0,j=4;i<5/2;i++,j--)
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
cout<<"\nReversed string:\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
getch();
}