……………..PUBLIC SENIOR SECONDARY
SCHOOL,…………….
PROGRAMMINING
In
SUBMITTED TO SUBMITTED BY
……………………….. …………………..
(COMPUTER SCIENCE)
ACKNOWLEDGEMENT
I would like to convey my heartful thanks to
……………………… (Computer Science) who
always gave valuable suggestions & guidance for
completion of my project.
He helped me to understand & remember
important details of the project. My project has
been a success only because of his guidance.
I am especially indented & I am also beholden to
my friends. And finally I thank to the members of
my family for their support & encouragement.
CERTIFICATE
This is to certify that ………………. of class
XII of ……………PUBLIC SENIOR
SECONDARY SCHOOL , …………… has
completed his project under my supervision. He
has taken proper care & shown sincerity in
completion of this project.
I certify that this project is up to my
expectation & as per the guideline issued by
CBSE.
……………………….
(Computer Science faculty )
INDEX
S.NO. PROGRAMS SIGNATURE
1 C++ Program - Find Largest Element in Array
2 C++ Program to accept the 10 numbers in an array and search array
using - Linear Search
3 C++ Program to accept the numbers in an array and Reverse Array
4 C++ Program to accept the numbers in an array and Insert new Element
in Array
5 C++ Program to accept the numbers in an array and Delete Element from
Array
6 C++ Program to accept the numbers in an array arr1, arr2 and Merge
Two Arrays in third array merge
7 C++ Program to accept the numbers in an array and sort them using
Bubble Sort
8 C++ Program to accept the numbers in an array and sort them using
Selection Sort
9 C++ Program to accept the numbers in an array and sort them Insertion
Sort
10 C++ Function Overloading - This C++ program demonstrates the concept
of function overloading in C++ practically.
11 C++ Function Overloading - This C++ program demonstrates the working
of default arguments in C++
12 C++ Function Overloading - This C++ program illustrates the working
of function overloading as compared to default arguments in C++
13 C++ Function Overloading - C++ Program Example demonstrating function
overloading in C++
14 C++ Function Overloading - Example program demonstrating function
overloading in C++
15 C++ Classes and Objects - This C++ program stores price list of 5
items and to print the largest price as well as the sum of all prices
using class in C++
16 C++ Program to create a Class student with rollno,name,marks and
grade and using Object invoke read() and display()
17 C++ Classes program to illustrates the call by reference mechanism on
objects
18 C++ program demonstrates the working of a function returning an
object
19 C++ program demonstrates the working of a Constructors and
Destructors - Example Program
20 C++ program uses an overloaded constructor
21 C++ program illustrates the working of function overloading as
compared to default arguments
22 C++ program to explain the concept of single inheritance
23 C++ program illustrate the working of constructors and destructors in
multiple inheritance
24 C++ program demonstrates the concept of Pushing and Popping from the
stack-array in C++
25 C++ Stack program demonstrates the concept Pushing and Popping from
the linked-stack in C++
26 C++ Queue - Example Program of C++ Queue program demonstrates the
concept of
Insertion and deletion in an array queue in C++
27 C++ Queue - Example Program of C++ Queue to demonstrates the concept
of
Insertion and deletion from the linked queue in C++
28 C++ Pointers and Arrays. This C++ program demonstrates the concept of
close association between arrays and pointers in C++.
29 C++ program to accept string in a pointer array
30 C++ Pointers and Functions. This C++ program demonstrates about
functions returning pointers in C++
31
C++ program to demonstrates the structure pointer in C++
32 C++ Pointers and Objects.This C++ program demonstrates about the
“this” pointer in C++
33 C++ program add two 3*3 matrices to form the third matrix
34 C++ Program ask to the user to enter any two 3*3 array elements to
subtract them i.e., Matrix1 - Matrix2, then display the subtracted
result of the two matrices (Matrix3)*/
35 C++ Program ,ask to the user to enter any 3*3 array/matrix element to
transpose and display the transpose of the matrix */
36
C++ Program ask to the user to enter the two 3*3 matrix elements, to
multiply them to form a new matrix which is the multiplication result
of the two entered 3*3 matrices, then display the result */
37
C++ Program accept the string and print Length of String
38 C++ Program accept two string and Compare Two String
39
C++ Program to accept the string and Delete Vowels from String
40 C++ Program accept the string and Delete Words from Sentence
41 C++ Program - Count Word in Sentence
42 C++ Program - Read and Display File
43 C++ Program - Merge Two Files
/* C++ Program - Find Largest Element in 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 ...nn";
large=arr[0];
for(i=0; i<size; i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
cout<<"Largest Number = "<<large;
getch();
}
/* C++ Program to accept the 10 numbers in an array and search array using - Linear Search */
#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();
}
/* C++ Program to accept the numbers in an array and Reverse Array */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, j, temp;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
j=i-1; // now j will point to the last element
i=0; // and i will be point to the first element
while(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
cout<<"Now the Reverse of the Array is : n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
/* C++ Program to accept the numbers in an array and Insert new Element in 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();
/* C++ Program to accept the numbers in an array and Delete Element from 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();
}
/* C++ Program to accept the numbers in an array arr1, arr2 and Merge Two Arrays in third
array merge */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
cout<<"Enter Array 1 Size : ";
cin>>size1;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter Array 2 Size : ";
cin>>size2;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}
cout<<"Now the new array after merging is :n";
for(i=0; i<size; i++)
{
cout<<merge[i]<<" ";
}
getch();
}
/* C++ Program to accept the numbers in an array and sort them 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();
}
/* C++ Program to accept the numbers in an array and sort them using Selection 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=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
/* C++ Program to accept the numbers in an array and sort them 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();
}
/* C++ Function Overloading - This C++ program demonstrates the concept of function
overloading in C++ practically. */
#include<iostream.h>
#include<conio.h>
class printData
{
public:
void print(int i) // function 1
{
cout<<"Printing int: "<<i<<"n";
}
void print(double f) // function 2
{
cout<<"Printing float: "<<f<<"n";
}
void print(char* c)
{
cout<<"Printing characters (string): "<<c<<"n";
}
};
void main()
{
clrscr();
printData pdobj;
pdobj.print(5); // called print() to print integer
pdobj.print(50.434); // called print() to print float
pdobj.print("C++ Function Overloading"); // called print() to print string
getch();
}
/* C++ Function Overloading - This C++ program demonstrates the working of default
arguments in C++ */
#include<iostream.h>
#include<conio.h>
void amount(float pri, int tim=2, float rat=0.06);
void amount(float pri, int tim, float rat)
{
cout<<"ntPrincipal Amount = "<<pri;
cout<<"ntTime = "<<tim;
cout<<"ntRate = "<<rat;
cout<<"ntInterest Amount = "<<(pri*tim*rat)<<"n";
}
void main()
{
clrscr();
cout<<"Results on amount(2000)";
amount(2000);
cout<<"nResults on amount(2500, 3)";
amount(2500, 3);
cout<<"nResults on amount(2300, 3, 0.11)";
amount(2300, 3, 0.11);
cout<<"nResults on amount(2500, 0.12)";
amount(2500, 0.12);
getch();
}
/* C++ Function Overloading - This C++ program illustrates the working of function
overloading as compared to default arguments in C++ */
#include<iostream.h>
#include<conio.h>
void amount(float pr, int ti, float ra)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = "<<ti<<" years";
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(pr*ti*ra)<<"n";
}
void amount(float pr, int ti)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = "<<ti;
cout<<"tRate = 0.04";
cout<<"ntInterest Amount = "<<(pr*ti*0.04)<<"n";
}
void amount(float pr, float ra)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = 2 years";
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(pr*2*ra)<<"n";
}
void amount(int ti, float ra)
{
cout<<"ntPrincipal Amount = 2000";
cout<<"ttTime = "<<ti;
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(2000*ti*ra)<<"n";
}
void amount(float pr)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = 2 years";
cout<<"tRate = 0.04";
cout<<"ntInterest Amount = "<<(pr*2*0.04)<<"n";
}
void main()
{
clrscr();
cout<<"Results on amount(2000.0F)";
amount(2000.0F);
cout<<"nResults on amount(2500.0F, 3)";
amount(2500.0F, 3);
cout<<"nResults on amount(2300.0F, 3, 0.11F)";
amount(2300.0F, 3, 0.11F);
cout<<"nResults on amount(2, 0.12F)";
amount(2, 0.12F);
cout<<"nResults on amount(6, 0.07F)";
amount(6, 0.07F);
getch();
}
/* C++ Function Overloading - C++ Program Example demonstrating function overloading in
C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float calarea(float a, float b, float c)
{
float s, are;
s = (a+b+c)/2;
are = sqrt(s*(s-a)*(s-b)*(s-c));
return are;
}
float calarea(float a, float b)
{
return a*b;
}
float calarea(float a)
{
return a*a;
}
void main()
{
clrscr();
int choice, s1, s2, s3, a;
do
{
cout<<"nArea Calculation Main Menun";
cout<<"1.Trianglen";
cout<<"2.Squaren";
cout<<"3.Rectanglen";
cout<<"4.Exitn";
cout<<"Enter your choice (1-4): ";
cin>>choice;
cout<<"n";
switch(choice)
{
case 1: cout<<"Enter three sides: ";
cin>>s1>>s2>>s3;
a = calarea(s1, s2, s3);
cout<<"Area = "<<a;
break;
case 2: cout<<"Enter a side: ";
cin>>s1;
a = calarea(s1);
cout<<"Area = "<<a;
break;
case 3: cout<<"Enter length and breadth: ";
cin>>s1>>s2;
a = calarea(s1, s2);
cout<<"Area = "<<a;
break;
case 4: cout<<"Exiting...press any key...";
getch();
exit(1);
default:cout<<"Wrong choice..!!";
}
cout<<"n";
}while(choice>0 && choice<=4);
getch();
}
/* C++ Function Overloading - Example program demonstrating function overloading in C++*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int divide(int num, int den)
{
if(den==0)
{
return -1;
}
if((num%den)==0)
{
return 1;
}
else
{
return 0;
}
}
int divide(int a)
{
int j = a/2, flag = 1, i;
for(i=2; (i<=j) && (flag); i++)
{
if(a%i == 0)
{
flag = 0;
}
}
return flag;
}
void main()
{
clrscr();
int choice, res, a, b;
do
{
cout<<"1.Check for divisibilityn";
cout<<"2.Check for Primen";
cout<<"3.Exitn";
cout<<"Enter your choice(1-3): ";
cin>>choice;
cout<<"n";
switch(choice)
{
case 1: cout<<"Enter numerator and denominator: ";
cin>>a>>b;
res = divide(a, b);
if(res == -1)
{
cout<<"Divide by zero error..!!n";
break;
}
cout<<((res) ? "It is" : "It is not")<<"n";
break;
case 2: cout<<"Enter the number: ";
cin>>a;
res = 0;
res = divide(a);
cout<<((res) ? "It is" : "It is not")<<"n";
break;
case 3: cout<<"Exiting...press any key...";
getch();
exit(1);
default:cout<<"Wrong choice..!!";
}
cout<<"n";
}while(choice>0 && choice<=3);
getch();
}
* C++ Classes and Objects - This C++ program stores price list of 5 items and to print
the largest price as well as the sum of all prices using class in C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class ITEM
{
int itemcode[5];
float itprice[5];
public:
void initialize(void);
float largest(void);
float sum(void);
void displayitems(void);
};
void ITEM::initialize(void)
{
for(int i=0; i<5; i++)
{
cout<<"Item No.: "<<(i+1);
cout<<"nEnter item code: ";
cin>>itemcode[i];
cout<<"Enter item price: ";
cin>>itprice[i];
cout<<"n";
}
}
float ITEM::largest(void)
{
float larg=itprice[0];
for(int i=1; i<5; i++)
{
if(larg<itprice[i])
{
larg=itprice[i];
}
}
return larg;
}
float ITEM::sum(void)
{
float sum=0;
for(int i=0; i<5; i++)
{
sum = sum + itprice[i];
}
return sum;
}
void ITEM::displayitems(void)
{
cout<<"nCodetPricen";
for(int i=0; i<5; i++)
{
cout<<itemcode[i]<<"t";
cout<<itprice[i]<<"n";
}
}
void main()
{
clrscr();
ITEM order;
order.initialize();
float tot, big;
int ch=0;
do
{
cout<<"nMain Menun";
cout<<"1.Display Largest Pricen";
cout<<"2.Display Sum of Pricesn";
cout<<"3.Display Item Listn";
cout<<"4.Exitn";
cout<<"Enter your choice(1-4): ";
cin>>ch;
switch(ch)
{
case 1: big=order.largest();
cout<<"Largest Price = "<<big;
break;
case 2: tot=order.sum();
cout<<"Sum of Prices = "<<tot;
break;
case 3: order.displayitems();
break;
case 4: cout<<"Exiting...press any key...";
getch();
exit(1);
default:cout<<"nWrong choice..!!";
break;
}
cout<<"n";
}while(ch>=1 && ch<=4);
getch();
}
/* C++ Program to create a Class student with rollno,name,marks and grade and using
Object invoke read() and display() */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
class STUDENT
{
private:
int rollno;
char name[40];
float marks;
char grade;
public:
void read() // mutator
{
cout<<"nEnter rollno: ";
cin>>rollno;
cout<<"Enter name: ";
gets(name);
cout<<"Enter marks: ";
cin>>marks;
}
void display() // accessor
{
calculategrade();
cout<<"Roll no.: "<<rollno<<"n";
cout<<"Name: "<<name<<"n";
cout<<"Marks: "<<marks<<"n";
cout<<"Grade: "<<grade<<"n";
}
int getrollno() // accessor
{
return rollno;
}
float getmarks() // accessor
{
return marks;
}
void calculategrade() // mutator
{
if(marks>=80)
{
grade = 'A';
}
else if(marks>=60)
{
grade = 'B';
}
else if(marks>=40)
{
grade = 'C';
}
else
{
grade = 'F';
}
}
};
void main()
{
clrscr();
STUDENT tw[5];
for(int i=0; i<5; i++)
{
cout<<"nEnter details for Student "<<i+1<<": ";
tw[i].read();
}
int choice, rno, pos=-1, highmarks=0;
do
{
cout<<"nMain Menun";
cout<<"1.Specific Studentn";
cout<<"2.Toppern";
cout<<"3.Exitn";
cout<<"Enter youce choice(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter roll no of student whose details you want to know/see: ";
cin>>rno;
for(i=0; i<5; i++)
{
if(tw[i].getrollno()==rno)
{
tw[i].display();
break;
}
}
if(i==5)
{
cout<<"Invalid rollno..!!";
}
break;
case 2:
for(i=0; i<5; i++)
{
if(tw[i].getmarks()>highmarks)
{
pos=i;
highmarks=tw[i].getmarks();
}
}
tw[pos].display();
break;
case 3:
cout<<"Exiting..press a key..";
getch();
exit(1);
default:
cout<<"Wrong choice..!!";
break;
}
}while(choice>=1 && choice<=3);
getch();
}
/* C++ Classes program to illustrates the call by reference mechanism on objects */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class TIME
{
int hrs, mins, secs;
char suf[4];
public:
int totsecs;
void gettime(int h, int m, int s)
{
hrs=h;
mins=m;
secs=s;
totsecs=(hrs*60)+(mins*60)+secs;
strcpy(suf, "Hrs");
}
void puttime(void)
{
cout<<"Time is: "<<hrs<<":"<<mins<<":"<<secs<<" "<<suf<<"n";
}
char *getsuf()
{
return suf;
}
void convert(TIME &t, char ch);
void sum(TIME &t1, TIME &t2);
int gethrs()
{
return hrs;
}
int getmins()
{
return mins;
}
int getsecs()
{
return secs;
}
};
void TIME::convert(TIME &t, char ch)
{
switch(ch)
{
case 'h':
if(strcmp(t.suf, "Hrs")!=0)
{
t.hrs=(strcmp(t.suf, "am")==0)?t.hrs:t.hrs+12;
strcpy(t.suf,"Hrs");
}
cout<<"Time in hours is: "<<t.hrs<<":"<<t.mins<<":"<<t.secs<<" "<<t.suf<<"n";
break;
case 'p':
if(strcmp(t.suf,"Hrs")==0)
{
(t.hrs>12)?strcpy(t.suf,"pm"):strcpy(t.suf,"am");
t.hrs=((t.hrs>12)?(t.hrs-12):t.hrs);
}
cout<<"Time in am/pm is: "<<t.hrs<<":"<<t.mins<<":"<<t.secs<<" "<<t.suf<<"n";
break;
default:
cout<<"Wrong choice..!!";
break;
}
}
void TIME::sum(TIME &t1, TIME &t2)
{
int h, m, s, sq, mq;
if(strcmp(t1.getsuf(),"pm")==0)
{
convert(t1,'h');
}
if(strcmp(t2.getsuf(),"pm")==0)
{
convert(t2,'h');
}
sq=(t1.secs+t2.secs)/60;
s=(t1.secs+t2.secs)%60;
mq=(sq+t1.mins+t2.mins)/60;
m=(sq+t1.mins+t2.mins)%60;
h=mq+t1.hrs+t2.hrs;
if(h==24) h=0;
cout<<"Total time is: "<<h<<":"<<m<<":"<<s<<"Hrsn";
}
void prnvalues(TIME &t1)
{
cout<<"hrs:"<<t1.gethrs()<<"n";
cout<<"mins:"<<t1.getmins()<<"n";
cout<<"secs:"<<t1.getsecs()<<"n";
cout<<"Total secs:"<<t1.totsecs<<"n";
}
void main()
{
clrscr();
TIME tm1, tm2;
char ch;
tm1.gettime(15,13,27);
tm2.gettime(7,48,38);
cout<<"Enter h to convert in hours format, or p for am/pm format: ";
cin>>ch;
cout<<"Converted times are:n";
cout<<"Time 1: ";
tm1.convert(tm1,ch);
cout<<"Time 2: ";
tm2.convert(tm2,ch);
tm1.sum(tm1, tm2);
prnvalues(tm2);
getch();}
/* C++ program demonstrates the working of a function returning an object */
#include<iostream.h>
#include<conio.h>
class DISTANCE
{
int feet, inches;
public:
void getdata(int f, int i)
{
feet=f;
inches=i;
}
void print(void)
{
cout<<feet<<" feet "<<inches<<" inches n";
}
DISTANCE sum(DISTANCE d2);
};
DISTANCE DISTANCE::sum(DISTANCE d2)
{
DISTANCE d3;
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches+d2.inches)%12;
return d3;
}
void main()
{
clrscr();
DISTANCE len1, len2, tot;
len1.getdata(17, 6);
len2.getdata(13, 8);
tot=len1.sum(len2);
cout<<"Length1: ";
len1.print();
cout<<"Length2: ";
len2.print();
cout<<"Total Length: ";
tot.print();
getch();
}
/* C++ program demonstrates the working of a Constructors and Destructors - Example Program */
#include<iostream.h>
#include<conio.h>
class SUBJECT
{
int days;
int subjectno;
public:
SUBJECT(int d=123, int sn=101);
void printsubject(void)
{
cout<<"Subject No: "<<subjectno<<"n";
cout<<"Days: "<<days<<"n";
}
};
SUBJECT::SUBJECT(int d, int sn)
{
cout<<"Constructing SUBJECTn";
days=d;
subjectno=sn;
}
class STUDENT
{
int rollno;
float marks;
public:
STUDENT()
{
cout<<"Constructing STUDENTn";
rollno=0;
marks=0.0;
}
void getvalue(void)
{
cout<<"Enter roll number and marks: ";
cin>>rollno>>marks;
}
void print(void)
{
cout<<"Roll No: "<<rollno<<"n";
cout<<"Marks: "<<marks<<"n";
}
};
class ADMISSION
{
SUBJECT sub;
STUDENT stud;
float fees;
public:
ADMISSION()
{
cout<<"Constructing ADMISSIONn";
fees=0.0;
}
void print(void)
{
stud.print();
sub.printsubject();
cout<<"Fees: "<<fees<<"n";
}
};
void main()
{
clrscr();
ADMISSION adm;
cout<<"nBack to main()n";
getch();
}
/* C++ program uses an overloaded constructor */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class DEPOSIT
{
long int principal;
int time;
float rate;
float totalamount;
public:
DEPOSIT(); // #1
DEPOSIT(long p, int t, float r); // #2
DEPOSIT(long p, int t); // #3
DEPOSIT(long p, float r); // #4
void calculateamount(void);
void display(void);
};
DEPOSIT::DEPOSIT()
{
principal = time = rate = 0.0;
}
DEPOSIT::DEPOSIT(long p, int t, float r)
{
principal = p;
time = t;
rate = r;
}
DEPOSIT::DEPOSIT(long p, int t)
{
principal = p;
time = t;
rate = 0.08;
}
DEPOSIT::DEPOSIT(long p, float r)
{
principal = p;
time = 2;
rate = r;
}
void DEPOSIT::calculateamount(void)
{
totalamount = principal + (principal*time*rate)/100;
}
void DEPOSIT::display(void)
{
cout<<"Principal Amount: Rs."<<principal<<"n";
cout<<"Period of investment: "<<time<<" yearsn";
cout<<"Rate of interest: "<<rate<<"n";
cout<<"Total Amount: Rs."<<totalamount<<"n";
}
void main()
{
clrscr();
DEPOSIT d1;
DEPOSIT d2(2000, 2, 0.07f);
DEPOSIT d3(4000, 1);
DEPOSIT d4(3000, 0.12f);
d1.calculateamount();
d2.calculateamount();
d3.calculateamount();
d4.calculateamount();
cout<<"Object 1n";
d1.display();
cout<<"nObject 2n";
d2.display();
cout<<"nObject 3n";
d3.display();
cout<<"nObject 4n";
d4.display();
getch();
}
/* C++ program illustrates the working of function overloading as compared to default arguments*/
#include<iostream.h>
#include<conio.h>
void amount(float prin, int time, float rate)
{
cout<<"Principal Amount: Rs."<<prin;
cout<<"tTime: "<<time<<" years";
cout<<"tRate: "<<rate;
cout<<"nInterest Amount: "<<(prin*time*rate);
}
void amount(float prin, int time)
{
cout<<"Principal Amount: Rs."<<prin;
cout<<"tTime: "<<time<<" years";
cout<<"tRate: 0.06";
cout<<"nInterest Amount: "<<(prin*time*0.06);
}
void amount(float prin, float rate)
{
cout<<"Principal Amount: Rs."<<prin;
cout<<"tTime: 2 years";
cout<<"tRate: "<<rate;
cout<<"nInterest Amount: "<<(prin*2*rate);
}
void amount(int time, float rate)
{
cout<<"Principal Amount: Rs.2000";
cout<<"tTime: "<<time<<" years";
cout<<"tRate: "<<rate;
cout<<"nInterest Amount: "<<(2000*time*rate);
}
void amount(float prin)
{
cout<<"Principal Amount: Rs."<<prin;
cout<<"tTime: 2 years";
cout<<"tRate: 0.06";
cout<<"nInterest Amount: "<<(prin*2*0.06);
}
void main()
{
clrscr();
cout<<"Result on amount(2000.0f)n";
amount(2000.0f);
cout<<"nnResult on amount(2500.0f, 3)n";
amount(2500.0f, 3);
cout<<"nnResult on amount(2300.0f, 3, 0.13f)n";
amount(2300.0f, 3, 0.13f);
cout<<"nnResult on amount(2000.0f, 0.14f)n";
amount(2000.0f, 0.14f);
cout<<"nnResult on amount(6, 0.07f)n";
amount(6, 0.07f);
getch();
}
/* C++ program to explain the concept of single inheritance */
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class EMPLOYEE
{
private:
char name[30];
unsigned long enumb;
public:
void getdata()
{
cout<<"Enter name: ";
gets(name);
cout<<"Enter Employee Number: ";
cin>>enumb;
}
void putdata()
{
cout<<"Name: "<<name<<"t";
cout<<"Emp. No: "<<enumb<<"t";
cout<<"Basic Salary: "<<basic;
}
protected:
float basic;
void getbasic()
{
cout<<"Enter Basic: ";
cin>>basic;
}
};
class MANAGER:public EMPLOYEE
{
private:
char title[30];
public:
void getdata()
{
EMPLOYEE::getdata();
getbasic();
cout<<"Enter Title: ";
gets(title);
}
void putdata()
{
EMPLOYEE::putdata();
cout<<"tTitle: "<<title<<"n";
}
};
void main()
{
clrscr();
MANAGER m1, m2;
cout<<"Manager 1n";
m1.getdata();
cout<<"nManager 2n";
m2.getdata();
cout<<"nttManager 1 Detailsn";
m1.putdata();
cout<<"nttManager 2 Detailsn";
m2.putdata();
getch();
}
Here is the sample run of the above C++ program:
/* C++ program illustrate the working of constructors and destructors in multiple
inheritance */
#include<iostream.h>
#include<conio.h>
class BASE1
{ protected:
int a;
public:
BASE1(int x)
{
a=x;
cout<<"Constructing BASE1n";
}
~BASE1()
{
cout<<"Destructing BASE1n";
}
};
class BASE2
{
protected:
int b;
public:
BASE2(int y)
{
b=y;
cout<<"Constructing BASE2n";
}
~BASE2()
{
cout<<"Destructing BASE2n";
}
};
class DERIVED:public BASE2, public BASE1
{
int c;
public:
DERIVED(int i, int j, int k):BASE2(i),BASE1(j)
{
c=k;
cout<<"Constructing DERIVEDn";
}
~DERIVED()
{
cout<<"Destructing DERIVEDn";
}
void show()
{
cout<<"1."<<a<<"t2."<<b<<"t3."<<c<<"n";
}
};
void main()
{
clrscr();
DERIVED obj(10,11,12);
obj.show();
getch();
}
/* C++ program demonstrates the concept of Pushing and Popping from the stack-array in C+
+ */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int pop(int [], int &);
int push(int [], int &, int);
void display(int [], int);
const int SIZE = 50;
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key to exit..n";
getch();
exit(1);
}
cout<<"nThe Stack now is:n";
display(stack, top);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
cout<<"Now the deletion of elements starts..n";
ch='y';
while(ch=='y' || ch=='Y')
{
res = pop(stack, top);
if(res==-1)
{
cout<<"nUnderflow..!!..Aborting..!!..Press a key to exit..n";
getch();
exit(2);
}
else
{
cout<<"nElement deleted is: "<<res<<endl;
cout<<"nThe Stack now is:n";
display(stack, top);
}
cout<<"Want to delete more ? (y/n).. ";
cin>>ch;
}
getch();
}
int push(int stack[], int &top, int elem)
{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
int pop(int stack[], int &top)
{
int ret;
if(top==-1)
{
return -1;
}
else
{
ret=stack[top];
top--;
}
return ret;
}
void display(int stack[], int top)
{
if(top==-1)
{
return;
}
cout<<stack[top]<<" <-- "<<"n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"n";
}
}
/* C++ Stack program demonstrates the concept Pushing and Popping from the linked-stack
in C++ */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
node *next;
} *top, *newptr, *save, *ptr;
node *create_new_node(int);
void push(node *);
void pop();
void display(node *);
void main()
{
clrscr();
int inf;
char ch='y';
top=NULL;
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n";
cout<<"Press any key to exit..n";
getch();
exit(1);
}
push(newptr);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
clrscr();
do
{
cout<<"The Stack now is: n";
display(top);
cout<<"nWant to pop an element ? (y/n).. ";
cin>>ch;
if(ch=='y' || ch=='Y')
{
pop();
}
cout<<"n";
}while(ch=='y' || ch=='Y');
getch();
}
node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
ptr->next = NULL;
return ptr;
}
void push(node *n)
{
if(top==NULL)
{
top=n;
}
else
{
save = top;
top = n;
n->next = save;
}
}
void pop()
{
if(top==NULL)
{
cout<<"nUnderflow..!!..Press any key to exit..n";
getch();
exit(2);
}
else
{
ptr = top;
top = top->next;
delete ptr;
}
}
void display(node *n)
{
while(n != NULL)
{
cout<<n->info<<" -> ";
n = n->next;
}
cout<<"!!n";
}
/* C++ Queue - Example Program of C++ Queue program demonstrates the concept of
Insertion and deletion in an array queue in C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int delete_from_queue(int []);
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;
}
clrscr();
cout<<"Now deletion of elements starts...n";
ch='y';
while(ch=='y' || ch=='Y')
{
check = delete_from_queue(queue);
if(check == -1)
{
cout<<"nUnderflow..!!..Aborting..!!..Pres a key to exit..n";
getch();
exit(2);
}
else
{
cout<<"nElement deleted is: "<<check<<"n";
cout<<"Now the Queue (Front...to...Rear) is:n";
display(queue, front, rear);
}
cout<<"nWant to delete 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;
}
int delete_from_queue(int queue[])
{
int retn;
if(front == -1)
{
return -1;
}
else
{
retn = queue[front];
if(front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
return retn;
}
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";
}
/* C++ Queue - Example Program of C++ Queue to demonstrates the concept of
Insertion and deletion from the linked queue in C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
node *next;
} *front, *newptr, *save, *ptr, *rear;
node *create_new_node(int);
void insert(node *);
void delete_node_queue();
void display(node *);
void main()
{
clrscr();
front = rear = NULL;
int inf;
int count=0;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n";
cout<<"Press any key to exit..n";
getch();
exit(1);
}
insert(newptr);
cout<<"nNow the Queue (Front...to...Rear) is:n";
display(front);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
clrscr();
do
{
cout<<"The Linked-Queue now is (Front...to...Rear) is:n";
display(front);
if(count==0)
{
cout<<"nWant to delete ? (y/n).. ";
count++;
}
else
{
cout<<"nWant to delete more ? (y/n).. ";
}
cin>>ch;
if(ch=='y' || ch=='Y')
{
delete_node_queue();
}
cout<<"n";
}while(ch=='y' || ch=='Y');
getch();
}
node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
ptr->next = NULL;
return ptr;
}
void insert(node *n)
{
if(front == NULL)
{
front = rear = n;
}
else
{
rear->next = n;
rear = n;
}
}
void delete_node_queue()
{
if(front == NULL)
{
cout<<"nOverflow..!!..Press a key to exit..n";
getch();
exit(2);
}
else
{
ptr = front;
front = front->next;
delete ptr;
}
}
void display(node *n)
{
while(n != NULL)
{
cout<<n->info<<" -> ";
n = n->next;
}
cout<<"!!n";
}
/* C++ Pointers and Arrays. This C++ program demonstrates the concept of close
association between arrays and pointers in C++. */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *iptr[5];
int fa=65, fb=66, fc=67, fd=68, fe=69;
int i;
// initialize array pointers by making them point to 5 different ints
iptr[0] = &fa;
iptr[1] = &fb;
iptr[2] = &fc;
iptr[3] = &fd;
iptr[4] = &fe;
// now prints the values being pointed to by the pointers
for(i=0; i<5; i++)
{
cout<<"The pointer iptr["<<i<<"] points to "<<*iptr[i]<<"n";
}
cout<<"n";
// now print the addresses stored in the array
cout<<"The base address of the array iptr of pointers is "<<iptr<<"n";
for(i=0; i<5; i++)
{
cout<<"The address stored in iptr["<<i<<"] is "<<iptr[i]<<"n";
}
getch();
}
Here is the sample run of the above C++ program
/* C++ program to accept string in a pointer array */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *names[] = {"Sachin", "Dhoni", "Sehwag", "Raina", "Yuvraj"};
int len=0;
len=strlen(names[1]); // length of 2nd string
cout<<"Originally:ntstring 2 is ";
cout.write(names[1],len).put('n');
cout<<"tand string 4 is ";
cout.write(names[3],len).put('n');
// now exchange the position of string 2 and 4
char *tptr;
tptr = names[1];
names[1] = names[3];
names[3] = tptr;
// now print the exchanged string
cout<<"nExchanged:ntstring 2 is ";
cout.write(names[1],len).put('n');
cout<<"tand string 4 is ";
cout.write(names[3],len).put('n');
getch();
}
Here is the sample output of the above C++ program
/* C++ Pointers and Functions. This C++ program demonstrates about functions returning
pointers in C++ */
#include<iostream.h>
#include<conio.h>
int *biger(int &, int &);
void main()
{
clrscr();
int num1, num2, *c;
cout<<"Enter two integersn";
cin>>num1>>num2;
c = biger(num1, num2);
cout<<"The bigger value = "<<*c;
getch();
}
int *biger(int &x, int &y)
{
if(x>y)
{
return(&x);
}
else
{
return(&y);
}
}
* C++ program to demonstrates the structure pointer in C++ */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct emp
{
int empno;
char empname[20];
float empbasic;
float empexperience;
};
void display(emp *e);
void increase(emp *e);
void main()
{
clrscr();
emp mgr, *eptr;
cout<<"Enter employee number: ";
cin>>mgr.empno;
cout<<"Enter name: ";
gets(mgr.empname);
cout<<"Enter basic pay: ";
cin>>mgr.empbasic;
cout<<"Enter experience (in years): ";
cin>>mgr.empexperience;
eptr = &mgr;
cout<<"nEmployee details before increase()n";
display(eptr);
increase(eptr);
cout<<"nEmployee details after increase()n";
display(eptr);
getch();
}
void display(emp *e)
{
int len=strlen(e->empname);
cout<<"Employee number: "<<e->empno;
cout<<"nName: ";
cout.write(e->empname, len);
cout<<"tBasic: "<<e->empbasic;
cout<<"tExperience: "<<e->empexperience<<" yearsn";
}
void increase(emp *e)
{
if(e->empexperience >= 5)
{
e->empbasic = e->empbasic + 15000;
}
}
/* C++ Pointers and Objects.This C++ program demonstrates about the “this” pointer in C+
+*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Salesman
{
char name[1200];
float total_sales;
public:
Salesman(char *s, float f)
{
strcpy(name, "");
strcpy(name, s);
total_sales = f;
}
void prnobject(void)
{
cout.write(this->name, 26); // use of this pointer
cout<<" has invoked prnobject().n";
}
};
void main()
{
clrscr();
Salesman Rajat("Rajat", 21450), Ravi("Ravi", 23190), Vikrant("Vikrant", 19142);
/* above statement creates three objects */
Rajat.prnobject();
Vikrant.prnobject();
Ravi.prnobject();
getch();
}
Above C++ program will produce the following output :
/* C++ program add two 3*3 matrices to form the third matrix */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
cout<<"Enter matrix 1 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter matrix 2 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Adding the two matrix to form the third matrix .....n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
cout<<"The two matrix added successfully...!!";
cout<<"The new matrix will be :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
/* C++ Program ask to the user to enter any two 3*3 array elements to subtract them
i.e., Matrix1 - Matrix2, then display the subtracted result of the two matrices
(Matrix3)*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j;
cout<<"Enter 3*3 Array 1 Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr1[i][j];
}
}
cout<<"Enter 3*3 Array 2 Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr2[i][j];
}
}
cout<<"Subtracting array (array1-array2) ... n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arr3[i][j]=arr1[i][j]-arr2[i][j];
}
}
cout<<"Result of Array1 - Array2 is :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<arr3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
/* C++ Program ,ask to the user to enter any 3*3 array/matrix element to transpose and display the transpose of the
matrix */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[3][3], i, j, arrt[3][3];
cout<<"Enter 3*3 Array Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr[i][j];
}
}
cout<<"Transposing Array...n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arrt[i][j]=arr[j][i];
}
}
cout<<"Transpose of the Matrix is :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<arrt[i][j];
}
cout<<"n";
}
getch();
}
When the above C++ program is compile and executed, it will produce the following result:
/* C++ Program ask to the user to enter the two 3*3 matrix elements, to multiply them
to form a new matrix which is the multiplication result of the two entered 3*3
matrices, then display the result */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
cout<<"Enter first matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter second matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Multiplying two matrices...n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
{
sum = sum + mat1[i][k] * mat2[k][j];
}
mat3[i][j] = sum;
}
}
cout<<"nMultiplication of two Matrices : n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
/* C++ Program accept the string and print Length of String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20], len;
cout<<"Enter a string : ";
gets(str);
len=strlen(str);
cout<<"Length of the string is "<<len;
getch();
}
/* C++ Program accept two string and Compare Two String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str1[100], str2[100];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
Getch();
}
/* C++ Program to accept the string and Delete Vowels from String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20];
int len, i, j;
cout<<"Enter a string : ";
gets(str);
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
cout<<"After deleting the vowels, the string will be : "<<str;
getch();
}
/* C++ Program accept the string and Delete Words from Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i, j = 0, k = 0, count = 0;
char str[100], str1[10][20], word[20];
cout<<"Enter the String : ";
gets(str);
/* Converting the string into 2D Array */
for (i=0; str[i]!='0'; i++)
{
if (str[i]==' ')
{
str1[k][j] = '0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '0';
cout<<"Enter a word to be delete : ";
cin>>word;
/* Comparing the string with the given word */
for (i=0; i<k+1; i++)
{
if (strcmp(str1[i], word) == 0)
{
for (j=i; j<k+1; j++)
{
strcpy(str1[j], str1[j + 1]);
k--;
}
}
}
cout<<"The new String after deleting the word : n";
for (i=0; i<k+1; i++)
{
cout<<str1[i]<<" ";
}
getch();
}
/* C++ Program - Count Word in Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char strs[100], countw=0, strw[15], i;
cout<<"Write a sentence : ";
gets(strs);
int len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
countw++;
}
}
cout<<"Total number of words in the sentence is "<<countw+1;
getch();
}
/* C++ Program - Read and Display File */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifile;
char s[100], fname[20];
cout<<"Enter file name to read and display its content (like file.txt) : ";
cin>>fname;
ifile.open(fname);
if(!ifile)
{
cout<<"Error in opening file..!!";
getch();
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<"n";
ifile.close();
getch();
}
/* C++ Program to Merge Two Files */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifiles1, ifiles2;
ofstream ifilet;
char ch, fname1[20], fname2[20], fname3[30];
cout<<"Enter first file name (with extension like file1.txt) : ";
gets(fname1);
cout<<"Enter second file name (with extension like file2.txt) : ";
gets(fname2);
cout<<"Enter name of file (with extension like file3.txt) which will store the
contents of the two files (fname1 and fname1) : ";
gets(fname3);
ifiles1.open(fname1);
ifiles2.open(fname2);
if(ifiles1==NULL || ifiles2==NULL)
{
perror("Error Message ");
cout<<"Press any key to exit...n";
getch();
exit(EXIT_FAILURE);
}
ifilet.open(fname3);
if(!ifilet)
{
perror("Error Message ");
cout<<"Press any key to exit...n";
getch();
exit(EXIT_FAILURE);
}
while(ifiles1.eof()==0)
{
ifiles1>>ch;
ifilet<<ch;
}
while(ifiles2.eof()==0)
{
ifiles2>>ch;
ifilet<<ch;
}
cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
ifiles1.close();
ifiles2.close();
ifilet.close();
getch();
}
programming in C++ report

programming in C++ report

  • 1.
    ……………..PUBLIC SENIOR SECONDARY SCHOOL,……………. PROGRAMMINING In SUBMITTEDTO SUBMITTED BY ……………………….. ………………….. (COMPUTER SCIENCE)
  • 2.
    ACKNOWLEDGEMENT I would liketo convey my heartful thanks to ……………………… (Computer Science) who always gave valuable suggestions & guidance for completion of my project. He helped me to understand & remember important details of the project. My project has been a success only because of his guidance. I am especially indented & I am also beholden to my friends. And finally I thank to the members of my family for their support & encouragement.
  • 3.
    CERTIFICATE This is tocertify that ………………. of class XII of ……………PUBLIC SENIOR SECONDARY SCHOOL , …………… has completed his project under my supervision. He has taken proper care & shown sincerity in completion of this project. I certify that this project is up to my expectation & as per the guideline issued by CBSE. ………………………. (Computer Science faculty )
  • 4.
    INDEX S.NO. PROGRAMS SIGNATURE 1C++ Program - Find Largest Element in Array 2 C++ Program to accept the 10 numbers in an array and search array using - Linear Search 3 C++ Program to accept the numbers in an array and Reverse Array 4 C++ Program to accept the numbers in an array and Insert new Element in Array 5 C++ Program to accept the numbers in an array and Delete Element from Array 6 C++ Program to accept the numbers in an array arr1, arr2 and Merge Two Arrays in third array merge 7 C++ Program to accept the numbers in an array and sort them using Bubble Sort 8 C++ Program to accept the numbers in an array and sort them using Selection Sort 9 C++ Program to accept the numbers in an array and sort them Insertion Sort 10 C++ Function Overloading - This C++ program demonstrates the concept of function overloading in C++ practically. 11 C++ Function Overloading - This C++ program demonstrates the working of default arguments in C++ 12 C++ Function Overloading - This C++ program illustrates the working of function overloading as compared to default arguments in C++ 13 C++ Function Overloading - C++ Program Example demonstrating function overloading in C++ 14 C++ Function Overloading - Example program demonstrating function overloading in C++ 15 C++ Classes and Objects - This C++ program stores price list of 5 items and to print the largest price as well as the sum of all prices using class in C++ 16 C++ Program to create a Class student with rollno,name,marks and grade and using Object invoke read() and display() 17 C++ Classes program to illustrates the call by reference mechanism on objects 18 C++ program demonstrates the working of a function returning an object 19 C++ program demonstrates the working of a Constructors and Destructors - Example Program 20 C++ program uses an overloaded constructor 21 C++ program illustrates the working of function overloading as compared to default arguments 22 C++ program to explain the concept of single inheritance 23 C++ program illustrate the working of constructors and destructors in multiple inheritance 24 C++ program demonstrates the concept of Pushing and Popping from the stack-array in C++ 25 C++ Stack program demonstrates the concept Pushing and Popping from the linked-stack in C++ 26 C++ Queue - Example Program of C++ Queue program demonstrates the concept of Insertion and deletion in an array queue in C++ 27 C++ Queue - Example Program of C++ Queue to demonstrates the concept of Insertion and deletion from the linked queue in C++ 28 C++ Pointers and Arrays. This C++ program demonstrates the concept of close association between arrays and pointers in C++. 29 C++ program to accept string in a pointer array
  • 5.
    30 C++ Pointersand Functions. This C++ program demonstrates about functions returning pointers in C++ 31 C++ program to demonstrates the structure pointer in C++ 32 C++ Pointers and Objects.This C++ program demonstrates about the “this” pointer in C++ 33 C++ program add two 3*3 matrices to form the third matrix 34 C++ Program ask to the user to enter any two 3*3 array elements to subtract them i.e., Matrix1 - Matrix2, then display the subtracted result of the two matrices (Matrix3)*/ 35 C++ Program ,ask to the user to enter any 3*3 array/matrix element to transpose and display the transpose of the matrix */ 36 C++ Program ask to the user to enter the two 3*3 matrix elements, to multiply them to form a new matrix which is the multiplication result of the two entered 3*3 matrices, then display the result */ 37 C++ Program accept the string and print Length of String 38 C++ Program accept two string and Compare Two String 39 C++ Program to accept the string and Delete Vowels from String 40 C++ Program accept the string and Delete Words from Sentence 41 C++ Program - Count Word in Sentence 42 C++ Program - Read and Display File 43 C++ Program - Merge Two Files
  • 6.
    /* C++ Program- Find Largest Element in 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 ...nn"; large=arr[0]; for(i=0; i<size; i++) { if(large<arr[i]) { large=arr[i]; } } cout<<"Largest Number = "<<large; getch(); }
  • 7.
    /* C++ Programto accept the 10 numbers in an array and search array using - Linear Search */ #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(); }
  • 8.
    /* C++ Programto accept the numbers in an array and Reverse Array */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr[50], size, i, j, temp; cout<<"Enter array size : "; cin>>size; cout<<"Enter array elements : "; for(i=0; i<size; i++) { cin>>arr[i]; } j=i-1; // now j will point to the last element i=0; // and i will be point to the first element while(i<j) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; } cout<<"Now the Reverse of the Array is : n"; for(i=0; i<size; i++) { cout<<arr[i]<<" "; } getch(); }
  • 9.
    /* C++ Programto accept the numbers in an array and Insert new Element in 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();
  • 10.
    /* C++ Programto accept the numbers in an array and Delete Element from 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(); }
  • 11.
    /* C++ Programto accept the numbers in an array arr1, arr2 and Merge Two Arrays in third array merge */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100]; cout<<"Enter Array 1 Size : "; cin>>size1; cout<<"Enter Array 1 Elements : "; for(i=0; i<size1; i++) { cin>>arr1[i]; } cout<<"Enter Array 2 Size : "; cin>>size2; cout<<"Enter Array 2 Elements : "; for(i=0; i<size2; i++) { cin>>arr2[i]; } for(i=0; i<size1; i++) { merge[i]=arr1[i]; } size=size1+size2; for(i=0, k=size1; k<size && i<size2; i++, k++) { merge[k]=arr2[i]; } cout<<"Now the new array after merging is :n"; for(i=0; i<size; i++) { cout<<merge[i]<<" "; } getch(); }
  • 12.
    /* C++ Programto accept the numbers in an array and sort them 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(); }
  • 13.
    /* C++ Programto accept the numbers in an array and sort them using Selection 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=0; i<size; i++) { for(j=i+1; j<size; j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } cout<<"Now the Array after sorting is :n"; for(i=0; i<size; i++) { cout<<arr[i]<<" "; } getch(); }
  • 14.
    /* C++ Programto accept the numbers in an array and sort them 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(); }
  • 15.
    /* C++ FunctionOverloading - This C++ program demonstrates the concept of function overloading in C++ practically. */ #include<iostream.h> #include<conio.h> class printData { public: void print(int i) // function 1 { cout<<"Printing int: "<<i<<"n"; } void print(double f) // function 2 { cout<<"Printing float: "<<f<<"n"; } void print(char* c) { cout<<"Printing characters (string): "<<c<<"n"; } }; void main() { clrscr(); printData pdobj; pdobj.print(5); // called print() to print integer pdobj.print(50.434); // called print() to print float pdobj.print("C++ Function Overloading"); // called print() to print string getch(); }
  • 16.
    /* C++ FunctionOverloading - This C++ program demonstrates the working of default arguments in C++ */ #include<iostream.h> #include<conio.h> void amount(float pri, int tim=2, float rat=0.06); void amount(float pri, int tim, float rat) { cout<<"ntPrincipal Amount = "<<pri; cout<<"ntTime = "<<tim; cout<<"ntRate = "<<rat; cout<<"ntInterest Amount = "<<(pri*tim*rat)<<"n"; } void main() { clrscr(); cout<<"Results on amount(2000)"; amount(2000); cout<<"nResults on amount(2500, 3)"; amount(2500, 3); cout<<"nResults on amount(2300, 3, 0.11)"; amount(2300, 3, 0.11); cout<<"nResults on amount(2500, 0.12)"; amount(2500, 0.12); getch(); }
  • 17.
    /* C++ FunctionOverloading - This C++ program illustrates the working of function overloading as compared to default arguments in C++ */ #include<iostream.h> #include<conio.h> void amount(float pr, int ti, float ra) { cout<<"ntPrincipal Amount = "<<pr; cout<<"ttTime = "<<ti<<" years"; cout<<"tRate = "<<ra; cout<<"ntInterest Amount = "<<(pr*ti*ra)<<"n"; } void amount(float pr, int ti) { cout<<"ntPrincipal Amount = "<<pr; cout<<"ttTime = "<<ti; cout<<"tRate = 0.04"; cout<<"ntInterest Amount = "<<(pr*ti*0.04)<<"n"; } void amount(float pr, float ra) { cout<<"ntPrincipal Amount = "<<pr; cout<<"ttTime = 2 years"; cout<<"tRate = "<<ra; cout<<"ntInterest Amount = "<<(pr*2*ra)<<"n"; } void amount(int ti, float ra) { cout<<"ntPrincipal Amount = 2000"; cout<<"ttTime = "<<ti; cout<<"tRate = "<<ra; cout<<"ntInterest Amount = "<<(2000*ti*ra)<<"n"; } void amount(float pr) { cout<<"ntPrincipal Amount = "<<pr; cout<<"ttTime = 2 years"; cout<<"tRate = 0.04"; cout<<"ntInterest Amount = "<<(pr*2*0.04)<<"n"; } void main() { clrscr(); cout<<"Results on amount(2000.0F)"; amount(2000.0F); cout<<"nResults on amount(2500.0F, 3)"; amount(2500.0F, 3); cout<<"nResults on amount(2300.0F, 3, 0.11F)"; amount(2300.0F, 3, 0.11F); cout<<"nResults on amount(2, 0.12F)"; amount(2, 0.12F); cout<<"nResults on amount(6, 0.07F)"; amount(6, 0.07F); getch(); }
  • 19.
    /* C++ FunctionOverloading - C++ Program Example demonstrating function overloading in C++ */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> float calarea(float a, float b, float c) { float s, are; s = (a+b+c)/2; are = sqrt(s*(s-a)*(s-b)*(s-c)); return are; } float calarea(float a, float b) { return a*b; } float calarea(float a) { return a*a; } void main() { clrscr(); int choice, s1, s2, s3, a; do { cout<<"nArea Calculation Main Menun"; cout<<"1.Trianglen"; cout<<"2.Squaren"; cout<<"3.Rectanglen"; cout<<"4.Exitn"; cout<<"Enter your choice (1-4): "; cin>>choice; cout<<"n"; switch(choice) { case 1: cout<<"Enter three sides: "; cin>>s1>>s2>>s3; a = calarea(s1, s2, s3); cout<<"Area = "<<a; break; case 2: cout<<"Enter a side: "; cin>>s1; a = calarea(s1); cout<<"Area = "<<a; break; case 3: cout<<"Enter length and breadth: "; cin>>s1>>s2; a = calarea(s1, s2); cout<<"Area = "<<a; break; case 4: cout<<"Exiting...press any key..."; getch(); exit(1); default:cout<<"Wrong choice..!!"; } cout<<"n"; }while(choice>0 && choice<=4); getch(); }
  • 21.
    /* C++ FunctionOverloading - Example program demonstrating function overloading in C++*/ #include<iostream.h> #include<conio.h> #include<stdlib.h> int divide(int num, int den) { if(den==0) { return -1; } if((num%den)==0) { return 1; } else { return 0; } } int divide(int a) { int j = a/2, flag = 1, i; for(i=2; (i<=j) && (flag); i++) { if(a%i == 0) { flag = 0; } } return flag; } void main() { clrscr(); int choice, res, a, b; do { cout<<"1.Check for divisibilityn"; cout<<"2.Check for Primen"; cout<<"3.Exitn"; cout<<"Enter your choice(1-3): "; cin>>choice; cout<<"n"; switch(choice) { case 1: cout<<"Enter numerator and denominator: "; cin>>a>>b; res = divide(a, b); if(res == -1) { cout<<"Divide by zero error..!!n"; break; } cout<<((res) ? "It is" : "It is not")<<"n"; break; case 2: cout<<"Enter the number: "; cin>>a; res = 0; res = divide(a); cout<<((res) ? "It is" : "It is not")<<"n"; break; case 3: cout<<"Exiting...press any key...";
  • 22.
  • 23.
    * C++ Classesand Objects - This C++ program stores price list of 5 items and to print the largest price as well as the sum of all prices using class in C++ */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class ITEM { int itemcode[5]; float itprice[5]; public: void initialize(void); float largest(void); float sum(void); void displayitems(void); }; void ITEM::initialize(void) { for(int i=0; i<5; i++) { cout<<"Item No.: "<<(i+1); cout<<"nEnter item code: "; cin>>itemcode[i]; cout<<"Enter item price: "; cin>>itprice[i]; cout<<"n"; } } float ITEM::largest(void) { float larg=itprice[0]; for(int i=1; i<5; i++) { if(larg<itprice[i]) { larg=itprice[i]; } } return larg; } float ITEM::sum(void) { float sum=0; for(int i=0; i<5; i++) { sum = sum + itprice[i]; } return sum; } void ITEM::displayitems(void) { cout<<"nCodetPricen"; for(int i=0; i<5; i++) { cout<<itemcode[i]<<"t"; cout<<itprice[i]<<"n"; } } void main() { clrscr(); ITEM order; order.initialize();
  • 24.
    float tot, big; intch=0; do { cout<<"nMain Menun"; cout<<"1.Display Largest Pricen"; cout<<"2.Display Sum of Pricesn"; cout<<"3.Display Item Listn"; cout<<"4.Exitn"; cout<<"Enter your choice(1-4): "; cin>>ch; switch(ch) { case 1: big=order.largest(); cout<<"Largest Price = "<<big; break; case 2: tot=order.sum(); cout<<"Sum of Prices = "<<tot; break; case 3: order.displayitems(); break; case 4: cout<<"Exiting...press any key..."; getch(); exit(1); default:cout<<"nWrong choice..!!"; break; } cout<<"n"; }while(ch>=1 && ch<=4); getch(); }
  • 25.
    /* C++ Programto create a Class student with rollno,name,marks and grade and using Object invoke read() and display() */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> class STUDENT { private: int rollno; char name[40]; float marks; char grade; public: void read() // mutator { cout<<"nEnter rollno: "; cin>>rollno; cout<<"Enter name: "; gets(name); cout<<"Enter marks: "; cin>>marks; } void display() // accessor { calculategrade(); cout<<"Roll no.: "<<rollno<<"n"; cout<<"Name: "<<name<<"n"; cout<<"Marks: "<<marks<<"n"; cout<<"Grade: "<<grade<<"n"; } int getrollno() // accessor { return rollno; } float getmarks() // accessor { return marks; } void calculategrade() // mutator { if(marks>=80) { grade = 'A'; } else if(marks>=60) { grade = 'B'; } else if(marks>=40) { grade = 'C'; } else { grade = 'F'; } } }; void main() { clrscr();
  • 26.
    STUDENT tw[5]; for(int i=0;i<5; i++) { cout<<"nEnter details for Student "<<i+1<<": "; tw[i].read(); } int choice, rno, pos=-1, highmarks=0; do { cout<<"nMain Menun"; cout<<"1.Specific Studentn"; cout<<"2.Toppern"; cout<<"3.Exitn"; cout<<"Enter youce choice(1-3): "; cin>>choice; switch(choice) { case 1: cout<<"Enter roll no of student whose details you want to know/see: "; cin>>rno; for(i=0; i<5; i++) { if(tw[i].getrollno()==rno) { tw[i].display(); break; } } if(i==5) { cout<<"Invalid rollno..!!"; } break; case 2: for(i=0; i<5; i++) { if(tw[i].getmarks()>highmarks) { pos=i; highmarks=tw[i].getmarks(); } } tw[pos].display(); break; case 3: cout<<"Exiting..press a key.."; getch(); exit(1); default: cout<<"Wrong choice..!!"; break; } }while(choice>=1 && choice<=3); getch(); }
  • 28.
    /* C++ Classesprogram to illustrates the call by reference mechanism on objects */ #include<iostream.h> #include<conio.h> #include<string.h> class TIME { int hrs, mins, secs; char suf[4]; public: int totsecs; void gettime(int h, int m, int s) { hrs=h; mins=m; secs=s; totsecs=(hrs*60)+(mins*60)+secs; strcpy(suf, "Hrs"); } void puttime(void) { cout<<"Time is: "<<hrs<<":"<<mins<<":"<<secs<<" "<<suf<<"n"; } char *getsuf() { return suf; } void convert(TIME &t, char ch); void sum(TIME &t1, TIME &t2); int gethrs() { return hrs; } int getmins() { return mins; } int getsecs() { return secs; } }; void TIME::convert(TIME &t, char ch) { switch(ch) { case 'h': if(strcmp(t.suf, "Hrs")!=0) { t.hrs=(strcmp(t.suf, "am")==0)?t.hrs:t.hrs+12; strcpy(t.suf,"Hrs"); } cout<<"Time in hours is: "<<t.hrs<<":"<<t.mins<<":"<<t.secs<<" "<<t.suf<<"n"; break; case 'p': if(strcmp(t.suf,"Hrs")==0) { (t.hrs>12)?strcpy(t.suf,"pm"):strcpy(t.suf,"am"); t.hrs=((t.hrs>12)?(t.hrs-12):t.hrs); } cout<<"Time in am/pm is: "<<t.hrs<<":"<<t.mins<<":"<<t.secs<<" "<<t.suf<<"n"; break; default: cout<<"Wrong choice..!!";
  • 29.
    break; } } void TIME::sum(TIME &t1,TIME &t2) { int h, m, s, sq, mq; if(strcmp(t1.getsuf(),"pm")==0) { convert(t1,'h'); } if(strcmp(t2.getsuf(),"pm")==0) { convert(t2,'h'); } sq=(t1.secs+t2.secs)/60; s=(t1.secs+t2.secs)%60; mq=(sq+t1.mins+t2.mins)/60; m=(sq+t1.mins+t2.mins)%60; h=mq+t1.hrs+t2.hrs; if(h==24) h=0; cout<<"Total time is: "<<h<<":"<<m<<":"<<s<<"Hrsn"; } void prnvalues(TIME &t1) { cout<<"hrs:"<<t1.gethrs()<<"n"; cout<<"mins:"<<t1.getmins()<<"n"; cout<<"secs:"<<t1.getsecs()<<"n"; cout<<"Total secs:"<<t1.totsecs<<"n"; } void main() { clrscr(); TIME tm1, tm2; char ch; tm1.gettime(15,13,27); tm2.gettime(7,48,38); cout<<"Enter h to convert in hours format, or p for am/pm format: "; cin>>ch; cout<<"Converted times are:n"; cout<<"Time 1: "; tm1.convert(tm1,ch); cout<<"Time 2: "; tm2.convert(tm2,ch); tm1.sum(tm1, tm2); prnvalues(tm2); getch();}
  • 30.
    /* C++ programdemonstrates the working of a function returning an object */ #include<iostream.h> #include<conio.h> class DISTANCE { int feet, inches; public: void getdata(int f, int i) { feet=f; inches=i; } void print(void) { cout<<feet<<" feet "<<inches<<" inches n"; } DISTANCE sum(DISTANCE d2); }; DISTANCE DISTANCE::sum(DISTANCE d2) { DISTANCE d3; d3.feet=feet+d2.feet+(inches+d2.inches)/12; d3.inches=(inches+d2.inches)%12; return d3; } void main() { clrscr(); DISTANCE len1, len2, tot; len1.getdata(17, 6); len2.getdata(13, 8); tot=len1.sum(len2); cout<<"Length1: "; len1.print(); cout<<"Length2: "; len2.print(); cout<<"Total Length: "; tot.print(); getch(); }
  • 31.
    /* C++ programdemonstrates the working of a Constructors and Destructors - Example Program */ #include<iostream.h> #include<conio.h> class SUBJECT { int days; int subjectno; public: SUBJECT(int d=123, int sn=101); void printsubject(void) { cout<<"Subject No: "<<subjectno<<"n"; cout<<"Days: "<<days<<"n"; } }; SUBJECT::SUBJECT(int d, int sn) { cout<<"Constructing SUBJECTn"; days=d; subjectno=sn; } class STUDENT { int rollno; float marks; public: STUDENT() { cout<<"Constructing STUDENTn"; rollno=0; marks=0.0; } void getvalue(void) { cout<<"Enter roll number and marks: "; cin>>rollno>>marks; } void print(void) { cout<<"Roll No: "<<rollno<<"n"; cout<<"Marks: "<<marks<<"n"; } }; class ADMISSION { SUBJECT sub; STUDENT stud; float fees; public: ADMISSION() { cout<<"Constructing ADMISSIONn"; fees=0.0; } void print(void) { stud.print(); sub.printsubject(); cout<<"Fees: "<<fees<<"n"; } }; void main()
  • 32.
  • 33.
    /* C++ programuses an overloaded constructor */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class DEPOSIT { long int principal; int time; float rate; float totalamount; public: DEPOSIT(); // #1 DEPOSIT(long p, int t, float r); // #2 DEPOSIT(long p, int t); // #3 DEPOSIT(long p, float r); // #4 void calculateamount(void); void display(void); }; DEPOSIT::DEPOSIT() { principal = time = rate = 0.0; } DEPOSIT::DEPOSIT(long p, int t, float r) { principal = p; time = t; rate = r; } DEPOSIT::DEPOSIT(long p, int t) { principal = p; time = t; rate = 0.08; } DEPOSIT::DEPOSIT(long p, float r) { principal = p; time = 2; rate = r; } void DEPOSIT::calculateamount(void) { totalamount = principal + (principal*time*rate)/100; } void DEPOSIT::display(void) { cout<<"Principal Amount: Rs."<<principal<<"n"; cout<<"Period of investment: "<<time<<" yearsn"; cout<<"Rate of interest: "<<rate<<"n"; cout<<"Total Amount: Rs."<<totalamount<<"n"; } void main() { clrscr(); DEPOSIT d1; DEPOSIT d2(2000, 2, 0.07f); DEPOSIT d3(4000, 1); DEPOSIT d4(3000, 0.12f); d1.calculateamount();
  • 34.
  • 35.
    /* C++ programillustrates the working of function overloading as compared to default arguments*/ #include<iostream.h> #include<conio.h> void amount(float prin, int time, float rate) { cout<<"Principal Amount: Rs."<<prin; cout<<"tTime: "<<time<<" years"; cout<<"tRate: "<<rate; cout<<"nInterest Amount: "<<(prin*time*rate); } void amount(float prin, int time) { cout<<"Principal Amount: Rs."<<prin; cout<<"tTime: "<<time<<" years"; cout<<"tRate: 0.06"; cout<<"nInterest Amount: "<<(prin*time*0.06); } void amount(float prin, float rate) { cout<<"Principal Amount: Rs."<<prin; cout<<"tTime: 2 years"; cout<<"tRate: "<<rate; cout<<"nInterest Amount: "<<(prin*2*rate); } void amount(int time, float rate) { cout<<"Principal Amount: Rs.2000"; cout<<"tTime: "<<time<<" years"; cout<<"tRate: "<<rate; cout<<"nInterest Amount: "<<(2000*time*rate); } void amount(float prin) { cout<<"Principal Amount: Rs."<<prin; cout<<"tTime: 2 years"; cout<<"tRate: 0.06"; cout<<"nInterest Amount: "<<(prin*2*0.06); } void main() { clrscr(); cout<<"Result on amount(2000.0f)n"; amount(2000.0f); cout<<"nnResult on amount(2500.0f, 3)n"; amount(2500.0f, 3); cout<<"nnResult on amount(2300.0f, 3, 0.13f)n"; amount(2300.0f, 3, 0.13f); cout<<"nnResult on amount(2000.0f, 0.14f)n"; amount(2000.0f, 0.14f); cout<<"nnResult on amount(6, 0.07f)n"; amount(6, 0.07f); getch(); }
  • 37.
    /* C++ programto explain the concept of single inheritance */ #include<iostream.h> #include<stdio.h> #include<conio.h> class EMPLOYEE { private: char name[30]; unsigned long enumb; public: void getdata() { cout<<"Enter name: "; gets(name); cout<<"Enter Employee Number: "; cin>>enumb; } void putdata() { cout<<"Name: "<<name<<"t"; cout<<"Emp. No: "<<enumb<<"t"; cout<<"Basic Salary: "<<basic; } protected: float basic; void getbasic() { cout<<"Enter Basic: "; cin>>basic; } }; class MANAGER:public EMPLOYEE { private: char title[30]; public: void getdata() { EMPLOYEE::getdata(); getbasic(); cout<<"Enter Title: "; gets(title); } void putdata() { EMPLOYEE::putdata(); cout<<"tTitle: "<<title<<"n"; } }; void main() { clrscr(); MANAGER m1, m2; cout<<"Manager 1n"; m1.getdata(); cout<<"nManager 2n"; m2.getdata(); cout<<"nttManager 1 Detailsn"; m1.putdata(); cout<<"nttManager 2 Detailsn"; m2.putdata();
  • 38.
    getch(); } Here is thesample run of the above C++ program:
  • 39.
    /* C++ programillustrate the working of constructors and destructors in multiple inheritance */ #include<iostream.h> #include<conio.h> class BASE1 { protected: int a; public: BASE1(int x) { a=x; cout<<"Constructing BASE1n"; } ~BASE1() { cout<<"Destructing BASE1n"; } }; class BASE2 { protected: int b; public: BASE2(int y) { b=y; cout<<"Constructing BASE2n"; } ~BASE2() { cout<<"Destructing BASE2n"; } }; class DERIVED:public BASE2, public BASE1 { int c; public: DERIVED(int i, int j, int k):BASE2(i),BASE1(j) { c=k; cout<<"Constructing DERIVEDn"; } ~DERIVED() { cout<<"Destructing DERIVEDn"; } void show() { cout<<"1."<<a<<"t2."<<b<<"t3."<<c<<"n"; } }; void main() { clrscr(); DERIVED obj(10,11,12); obj.show(); getch(); }
  • 41.
    /* C++ programdemonstrates the concept of Pushing and Popping from the stack-array in C+ + */ #include<iostream.h> #include<stdlib.h> #include<conio.h> int pop(int [], int &); int push(int [], int &, int); void display(int [], int); const int SIZE = 50; void main() { clrscr(); int stack[SIZE], item, top=-1, res; char ch='y'; while(ch=='y' || ch=='Y') { cout<<"Enter item for insertion: "; cin>>item; res = push(stack, top, item); if(res == -1) { cout<<"Overflow..!!..Aborting..Press a key to exit..n"; getch(); exit(1); } cout<<"nThe Stack now is:n"; display(stack, top); cout<<"nWant to enter more ? (y/n).. "; cin>>ch; } cout<<"Now the deletion of elements starts..n"; ch='y'; while(ch=='y' || ch=='Y') { res = pop(stack, top); if(res==-1) { cout<<"nUnderflow..!!..Aborting..!!..Press a key to exit..n"; getch(); exit(2); } else { cout<<"nElement deleted is: "<<res<<endl; cout<<"nThe Stack now is:n"; display(stack, top); } cout<<"Want to delete more ? (y/n).. "; cin>>ch; } getch(); } int push(int stack[], int &top, int elem) { if(top == SIZE-1) { return -1; } else
  • 42.
    { top++; stack[top] = elem; } return0; } int pop(int stack[], int &top) { int ret; if(top==-1) { return -1; } else { ret=stack[top]; top--; } return ret; } void display(int stack[], int top) { if(top==-1) { return; } cout<<stack[top]<<" <-- "<<"n"; for(int i=top-1; i>=0; i--) { cout<<stack[i]<<"n"; } }
  • 44.
    /* C++ Stackprogram demonstrates the concept Pushing and Popping from the linked-stack in C++ */ #include<iostream.h> #include<stdlib.h> #include<conio.h> struct node { int info; node *next; } *top, *newptr, *save, *ptr; node *create_new_node(int); void push(node *); void pop(); void display(node *); void main() { clrscr(); int inf; char ch='y'; top=NULL; while(ch=='y' || ch=='Y') { cout<<"Enter information for the new node.. "; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n"; cout<<"Press any key to exit..n"; getch(); exit(1); } push(newptr); cout<<"nWant to enter more ? (y/n).. "; cin>>ch; } clrscr(); do { cout<<"The Stack now is: n"; display(top); cout<<"nWant to pop an element ? (y/n).. "; cin>>ch; if(ch=='y' || ch=='Y') { pop(); } cout<<"n"; }while(ch=='y' || ch=='Y'); getch(); } node *create_new_node(int x) { ptr = new node; ptr->info = x;
  • 45.
    ptr->next = NULL; returnptr; } void push(node *n) { if(top==NULL) { top=n; } else { save = top; top = n; n->next = save; } } void pop() { if(top==NULL) { cout<<"nUnderflow..!!..Press any key to exit..n"; getch(); exit(2); } else { ptr = top; top = top->next; delete ptr; } } void display(node *n) { while(n != NULL) { cout<<n->info<<" -> "; n = n->next; } cout<<"!!n"; }
  • 47.
    /* C++ Queue- Example Program of C++ Queue program demonstrates the concept of Insertion and deletion in an array queue in C++ */ #include<iostream.h> #include<conio.h> #include<stdlib.h> int delete_from_queue(int []); 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; } clrscr(); cout<<"Now deletion of elements starts...n"; ch='y'; while(ch=='y' || ch=='Y') { check = delete_from_queue(queue); if(check == -1) { cout<<"nUnderflow..!!..Aborting..!!..Pres a key to exit..n"; getch(); exit(2); } else { cout<<"nElement deleted is: "<<check<<"n"; cout<<"Now the Queue (Front...to...Rear) is:n"; display(queue, front, rear); } cout<<"nWant to delete more ? (y/n)... "; cin>>ch; }
  • 48.
    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; } int delete_from_queue(int queue[]) { int retn; if(front == -1) { return -1; } else { retn = queue[front]; if(front == rear) { front = rear = -1; } else { front++; } } return retn; } 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"; }
  • 50.
    /* C++ Queue- Example Program of C++ Queue to demonstrates the concept of Insertion and deletion from the linked queue in C++ */ #include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int info; node *next; } *front, *newptr, *save, *ptr, *rear; node *create_new_node(int); void insert(node *); void delete_node_queue(); void display(node *); void main() { clrscr(); front = rear = NULL; int inf; int count=0; char ch='y'; while(ch=='y' || ch=='Y') { cout<<"Enter information for the new node.. "; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n"; cout<<"Press any key to exit..n"; getch(); exit(1); } insert(newptr); cout<<"nNow the Queue (Front...to...Rear) is:n"; display(front); cout<<"nWant to enter more ? (y/n).. "; cin>>ch; } clrscr(); do { cout<<"The Linked-Queue now is (Front...to...Rear) is:n"; display(front); if(count==0) { cout<<"nWant to delete ? (y/n).. "; count++; } else { cout<<"nWant to delete more ? (y/n).. "; } cin>>ch; if(ch=='y' || ch=='Y') { delete_node_queue();
  • 51.
    } cout<<"n"; }while(ch=='y' || ch=='Y'); getch(); } node*create_new_node(int x) { ptr = new node; ptr->info = x; ptr->next = NULL; return ptr; } void insert(node *n) { if(front == NULL) { front = rear = n; } else { rear->next = n; rear = n; } } void delete_node_queue() { if(front == NULL) { cout<<"nOverflow..!!..Press a key to exit..n"; getch(); exit(2); } else { ptr = front; front = front->next; delete ptr; } } void display(node *n) { while(n != NULL) { cout<<n->info<<" -> "; n = n->next; } cout<<"!!n"; }
  • 53.
    /* C++ Pointersand Arrays. This C++ program demonstrates the concept of close association between arrays and pointers in C++. */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int *iptr[5]; int fa=65, fb=66, fc=67, fd=68, fe=69; int i; // initialize array pointers by making them point to 5 different ints iptr[0] = &fa; iptr[1] = &fb; iptr[2] = &fc; iptr[3] = &fd; iptr[4] = &fe; // now prints the values being pointed to by the pointers for(i=0; i<5; i++) { cout<<"The pointer iptr["<<i<<"] points to "<<*iptr[i]<<"n"; } cout<<"n"; // now print the addresses stored in the array cout<<"The base address of the array iptr of pointers is "<<iptr<<"n"; for(i=0; i<5; i++) { cout<<"The address stored in iptr["<<i<<"] is "<<iptr[i]<<"n"; } getch(); } Here is the sample run of the above C++ program
  • 54.
    /* C++ programto accept string in a pointer array */ #include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); char *names[] = {"Sachin", "Dhoni", "Sehwag", "Raina", "Yuvraj"}; int len=0; len=strlen(names[1]); // length of 2nd string cout<<"Originally:ntstring 2 is "; cout.write(names[1],len).put('n'); cout<<"tand string 4 is "; cout.write(names[3],len).put('n'); // now exchange the position of string 2 and 4 char *tptr; tptr = names[1]; names[1] = names[3]; names[3] = tptr; // now print the exchanged string cout<<"nExchanged:ntstring 2 is "; cout.write(names[1],len).put('n'); cout<<"tand string 4 is "; cout.write(names[3],len).put('n'); getch(); } Here is the sample output of the above C++ program
  • 55.
    /* C++ Pointersand Functions. This C++ program demonstrates about functions returning pointers in C++ */ #include<iostream.h> #include<conio.h> int *biger(int &, int &); void main() { clrscr(); int num1, num2, *c; cout<<"Enter two integersn"; cin>>num1>>num2; c = biger(num1, num2); cout<<"The bigger value = "<<*c; getch(); } int *biger(int &x, int &y) { if(x>y) { return(&x); } else { return(&y); } }
  • 56.
    * C++ programto demonstrates the structure pointer in C++ */ #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> struct emp { int empno; char empname[20]; float empbasic; float empexperience; }; void display(emp *e); void increase(emp *e); void main() { clrscr(); emp mgr, *eptr; cout<<"Enter employee number: "; cin>>mgr.empno; cout<<"Enter name: "; gets(mgr.empname); cout<<"Enter basic pay: "; cin>>mgr.empbasic; cout<<"Enter experience (in years): "; cin>>mgr.empexperience; eptr = &mgr; cout<<"nEmployee details before increase()n"; display(eptr); increase(eptr); cout<<"nEmployee details after increase()n"; display(eptr); getch(); } void display(emp *e) { int len=strlen(e->empname); cout<<"Employee number: "<<e->empno; cout<<"nName: "; cout.write(e->empname, len); cout<<"tBasic: "<<e->empbasic; cout<<"tExperience: "<<e->empexperience<<" yearsn"; } void increase(emp *e) { if(e->empexperience >= 5) { e->empbasic = e->empbasic + 15000; } }
  • 58.
    /* C++ Pointersand Objects.This C++ program demonstrates about the “this” pointer in C+ +*/ #include<iostream.h> #include<conio.h> #include<string.h> class Salesman { char name[1200]; float total_sales; public: Salesman(char *s, float f) { strcpy(name, ""); strcpy(name, s); total_sales = f; } void prnobject(void) { cout.write(this->name, 26); // use of this pointer cout<<" has invoked prnobject().n"; } }; void main() { clrscr(); Salesman Rajat("Rajat", 21450), Ravi("Ravi", 23190), Vikrant("Vikrant", 19142); /* above statement creates three objects */ Rajat.prnobject(); Vikrant.prnobject(); Ravi.prnobject(); getch(); } Above C++ program will produce the following output :
  • 59.
    /* C++ programadd two 3*3 matrices to form the third matrix */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int mat1[3][3], mat2[3][3], i, j, mat3[3][3]; cout<<"Enter matrix 1 elements :"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>mat1[i][j]; } } cout<<"Enter matrix 2 elements :"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>mat2[i][j]; } } cout<<"Adding the two matrix to form the third matrix .....n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { mat3[i][j]=mat1[i][j]+mat2[i][j]; } } cout<<"The two matrix added successfully...!!"; cout<<"The new matrix will be :n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<mat3[i][j]<<" "; } cout<<"n"; } getch(); }
  • 61.
    /* C++ Programask to the user to enter any two 3*3 array elements to subtract them i.e., Matrix1 - Matrix2, then display the subtracted result of the two matrices (Matrix3)*/ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j; cout<<"Enter 3*3 Array 1 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr1[i][j]; } } cout<<"Enter 3*3 Array 2 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr2[i][j]; } } cout<<"Subtracting array (array1-array2) ... n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j]=arr1[i][j]-arr2[i][j]; } } cout<<"Result of Array1 - Array2 is :n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<arr3[i][j]<<" "; } cout<<"n"; } getch(); }
  • 62.
    /* C++ Program,ask to the user to enter any 3*3 array/matrix element to transpose and display the transpose of the matrix */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr[3][3], i, j, arrt[3][3]; cout<<"Enter 3*3 Array Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr[i][j]; } } cout<<"Transposing Array...n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arrt[i][j]=arr[j][i]; } } cout<<"Transpose of the Matrix is :n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<arrt[i][j]; } cout<<"n"; } getch(); } When the above C++ program is compile and executed, it will produce the following result:
  • 63.
    /* C++ Programask to the user to enter the two 3*3 matrix elements, to multiply them to form a new matrix which is the multiplication result of the two entered 3*3 matrices, then display the result */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k; cout<<"Enter first matrix element (3*3) : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>mat1[i][j]; } } cout<<"Enter second matrix element (3*3) : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>mat2[i][j]; } } cout<<"Multiplying two matrices...n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { sum=0; for(k=0; k<3; k++) { sum = sum + mat1[i][k] * mat2[k][j]; } mat3[i][j] = sum; } } cout<<"nMultiplication of two Matrices : n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<mat3[i][j]<<" "; } cout<<"n"; } getch(); }
  • 65.
    /* C++ Programaccept the string and print Length of String */ #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char str[20], len; cout<<"Enter a string : "; gets(str); len=strlen(str); cout<<"Length of the string is "<<len; getch(); }
  • 66.
    /* C++ Programaccept two string and Compare Two String */ #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char str1[100], str2[100]; cout<<"Enter first string : "; gets(str1); cout<<"Enter second string : "; gets(str2); if(strcmp(str1, str2)==0) { cout<<"Both the strings are equal"; } else { cout<<"Both the strings are not equal"; } Getch(); }
  • 67.
    /* C++ Programto accept the string and Delete Vowels from String */ #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char str[20]; int len, i, j; cout<<"Enter a string : "; gets(str); len=strlen(str); for(i=0; i<len; i++) { if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U') { for(j=i; j<len; j++) { str[j]=str[j+1]; } len--; } } cout<<"After deleting the vowels, the string will be : "<<str; getch(); }
  • 68.
    /* C++ Programaccept the string and Delete Words from Sentence */ #include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i, j = 0, k = 0, count = 0; char str[100], str1[10][20], word[20]; cout<<"Enter the String : "; gets(str); /* Converting the string into 2D Array */ for (i=0; str[i]!='0'; i++) { if (str[i]==' ') { str1[k][j] = '0'; k++; j=0; } else { str1[k][j]=str[i]; j++; } } str1[k][j] = '0'; cout<<"Enter a word to be delete : "; cin>>word; /* Comparing the string with the given word */ for (i=0; i<k+1; i++) { if (strcmp(str1[i], word) == 0) { for (j=i; j<k+1; j++) { strcpy(str1[j], str1[j + 1]); k--; } } } cout<<"The new String after deleting the word : n"; for (i=0; i<k+1; i++) { cout<<str1[i]<<" "; } getch(); }
  • 70.
    /* C++ Program- Count Word in Sentence */ #include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); char strs[100], countw=0, strw[15], i; cout<<"Write a sentence : "; gets(strs); int len=strlen(strs); for(i=0; i<len; i++) { if(strs[i]==' ') { countw++; } } cout<<"Total number of words in the sentence is "<<countw+1; getch(); }
  • 71.
    /* C++ Program- Read and Display File */ #include<iostream.h> #include<conio.h> #include<string.h> #include<fstream.h> #include<stdlib.h> void main() { clrscr(); ifstream ifile; char s[100], fname[20]; cout<<"Enter file name to read and display its content (like file.txt) : "; cin>>fname; ifile.open(fname); if(!ifile) { cout<<"Error in opening file..!!"; getch(); exit(0); } while(ifile.eof()==0) { ifile>>s; cout<<s<<" "; } cout<<"n"; ifile.close(); getch(); }
  • 72.
    /* C++ Programto Merge Two Files */ #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> #include<stdlib.h> void main() { clrscr(); ifstream ifiles1, ifiles2; ofstream ifilet; char ch, fname1[20], fname2[20], fname3[30]; cout<<"Enter first file name (with extension like file1.txt) : "; gets(fname1); cout<<"Enter second file name (with extension like file2.txt) : "; gets(fname2); cout<<"Enter name of file (with extension like file3.txt) which will store the contents of the two files (fname1 and fname1) : "; gets(fname3); ifiles1.open(fname1); ifiles2.open(fname2); if(ifiles1==NULL || ifiles2==NULL) { perror("Error Message "); cout<<"Press any key to exit...n"; getch(); exit(EXIT_FAILURE); } ifilet.open(fname3); if(!ifilet) { perror("Error Message "); cout<<"Press any key to exit...n"; getch(); exit(EXIT_FAILURE); } while(ifiles1.eof()==0) { ifiles1>>ch; ifilet<<ch; } while(ifiles2.eof()==0) { ifiles2>>ch; ifilet<<ch; } cout<<"The two files were merged into "<<fname3<<" file successfully..!!"; ifiles1.close(); ifiles2.close(); ifilet.close(); getch(); }