0% found this document useful (0 votes)
129 views38 pages

Project

The document contains 11 code snippets demonstrating various C++ programming concepts: 1. A program to check if a string is a palindrome. 2. A program to calculate the area of different shapes using function overloading. 3. A program implementing function overloading to add integers and floats. 4. A program to calculate simple interest using function overloading. 5. A program implementing linear search on an array. 6. A program implementing binary search on a sorted array. 7. A program implementing bubble sort on an array. 8. A program implementing selection sort on an array. 9. A program implementing insertion sort on an array. 10

Uploaded by

Paras baweja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views38 pages

Project

The document contains 11 code snippets demonstrating various C++ programming concepts: 1. A program to check if a string is a palindrome. 2. A program to calculate the area of different shapes using function overloading. 3. A program implementing function overloading to add integers and floats. 4. A program to calculate simple interest using function overloading. 5. A program implementing linear search on an array. 6. A program implementing binary search on a sorted array. 7. A program implementing bubble sort on an array. 8. A program implementing selection sort on an array. 9. A program implementing insertion sort on an array. 10

Uploaded by

Paras baweja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

1.

Write a program in C++ to check whether a string is Palindrome or


not.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{
clrscr();
char str[10];
int i,j,n,flag=0;
cout<<endl<<"Enter any string:"<<endl;
gets(str);
n=strlen(str);
for(i=0,j=n-1;i<=n/2;i++,j--)
{
if(str[i]!=str[j])
{ flag=1;
break;
}
}
if(flag==1)
{
cout<<"String is not Palindrome"<<endl;
}
else
{
cout<<"String is Palindrome"<<endl;
}
getch();
}
OUTPUT
2. Write a program in C++ to calculate area of Circle, Triangle and
Rectangle using Function.
include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#define pi 3.14
//circle area
void area(float r)
{
cout<<endl<<" Area Of The Circle : "<<pi*r*r;
}
//rectangle area
void area(float l,float b)
{
cout<<endl<<" Area Of The Rectangle : "<<l*b;
}
//triangle area
void area(float a,float b,float c)
{
float s=(a+b+c)/2;
float ar=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<endl<<" Area Of The Triangle : "<<ar;
}
void main()
{
clrscr();
float r,ln,br,a,b,c;
int ch;
do
{
cout<<endl<<"-*- AREA OF DIFFERENT SHAPES USING FUNCTION OVERLOADING -
*-";
cout<<endl<<" Area Of Calculation Menu ";
cout<<endl<<" 1.CIRCLE AREA ";
cout<<endl<<" 2.RECTANGLE AREA ";
cout<<endl<<" 3.TRIANGLE AREA ";
cout<<endl<<" 4.EXIT ";
cout<<endl<<" Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:cout<<endl<<" Enter The Radius Of A Circle : ";
cin>>r;
area(r);
break;
case 2:cout<<endl<<" Enter The Length Of The Rectangle : ";
cin>>ln;
cout<<endl<<" Enter The Breadth Of The Rectangle : ";
cin>>br;
area(ln,br);
break;
case 3:cout<<" Enter First Side Of The Triangle : ";
cin>>a;
cout<<" Enter Second Side Of The Triangle : ";
cin>>b;
cout<<" Enter Third Side Of The Triangle : ";
cin>>c;
area(a,b,c);
break;
case 4:exit(0);
}
}while(1);
getch();
}

OUTPUT

FOR CIRCLE
FOR RECTANGLE

FOR TRIANGLE
3. Write a program in C++ to implement Function Overloading.

#include <iostream.h>
#include <conio.h>
long add(long, long);
float add(float, float);

void main()
{ clrscr();
long a, b, x;
float c, d, y;

cout << "Enter two integers\n";


cin >> a >> b;

x = add(a, b);

cout << "Sum of integers: " << x << endl;

cout << "Enter two floating point numbers\n";


cin >> c >> d;

y = add(c, d);
cout << "Sum of floats: " << y << endl;
getch();
}
long add(long x, long y)
{
long sum;

sum = x + y;

return sum;
}
float add(float x, float y)
{
float sum;

sum = x + y;

return sum;
}
OUTPUT
4. Write a program in C++ to calculate simple interest amount using
function overloading.

#include<iostream>
#include <conio.h>
void amt(float prin,float rate,int time)
{
cout<<"Principal amount : "<<prin<<" ";
cout<<"Rate : "<<rate<<" ";
cout<<"Time : "<<time<<" ";
cout<<"Total interest : "<<prin*rate*time<<endl;
}
void amt(float prin,int time)
{
amt(prin,.05F,time);
}
void amt(float prin,float rate)
{
amt(prin,rate,5);
}
void amt(int time,float rate)
{
amt(5000.0F,rate,time);
}
void amt(float rate)
{
amt(5000.0F,rate,5);
}
void amt(int time)
{
amt(5000.0F,.05,time);
}
void main()
{
amt(.09F);
amt(2);
amt(9000.0F,8);
amt(7,.02);
amt(2000.0F,.03F,6);
getch();
}
OUTPUT
5. Write a program in C++ to do linear search using array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, num, n,c=0,pos;
cout<<"Enter Size of Array:"<<"\t";
cin>>n;
cout<<"Enter Array Elements:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the no. to be searched:"<<"\t";
cin>>num;
for(i=0;i<n;i++)
{
if(a[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found....!!!!!";
}
else
{
cout<<num<<"found at position"<<pos;
}
getch();
}
OUTPUT
6. Write a program in C++ to do binary search using array.
#include <iostream>
using namespace std;

int main()
{
int count, i, arr[30], num, first, last, middle;
cout<<"how many elements would you like to enter?:";
cin>>count;

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


{
cout<<"Enter number "<<(i+1)<<": ";
cin>>arr[i];
}
cout<<"Enter the number that you want to search:";
cin>>num;
first = 0;
last = count-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < num)
{
first = middle + 1;

}
else if(arr[middle] == num)
{
cout<<num<<" found in the array at the location "<<middle+1<<"\n";
break;
}
else {
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<num<<" not found in the array";
}
return 0;
}

OUTPUT
7. Write a program in C++ to implement 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();
}
OUTPUT
8. Write a program in C++ to implement selection sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp, index, small, count=0;
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-1); i++)
{
small = arr[i];
for(j=(i+1); j<size; j++)
{
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
count=0;
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
cout<<arr[i]<<" ";
getch();
}
OUTPUT
9. Write a program in C++ to implement insertion sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
OUTPUT
10. Write a program to implement the concept of Class and object in
C++.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class student
{
private:
int roll;
char name[20];
int marks[5];
char stream[20];
void findstream(float);
public:
void getdata();
void putdata();
}s;
void student::getdata()
{
cout<<"Enter Roll No. : ";
cin>>roll;
cout<<"Enter Name : ";
cin>>name;
cout<<"Enter Marks"<<endl;
float total=0;
for(int i=1;i<=5;i++)
{
cout<<"Subject "<<i<<": ";
cin>>marks[i];
total=total+marks[i];
}
findstream(total);
}
void student::putdata()
{
cout<<"----------------------------------------"<<endl
<<"****************************************"<<endl
<<"----------------------------------------"<<endl
<<"Roll No. : "<<roll<<endl
<<"Name : "<<name<<endl
<<"Stream : "<<stream<<endl;
for(int i=0;i<=5;i++)
{
cout<<marks[i]<<"\n";
}
cout<<"----------------------------------------"<<endl
<<"****************************************"<<endl
<<"----------------------------------------";
}
void student::findstream(float total)
{
if(total>=96 && total<=100)
{
strcpy( stream,"Computer Science");
}
else if(total>=91 && total<=95.9)
{
strcpy(stream,"Electronics");
}
else if(total>=86 && total<=90.9)
{
strcpy( stream,"Mechanical");
}
else if(total>=81 && total<=85.9)
{
strcpy(stream,"Electrical");
}
else if(total>=76 && total<=80.9)
{
strcpy(stream,"Chemical");
}
else
{
strcpy(stream,"Civil");
}
}
void main()
{
clrscr();
s.getdata();
s.putdata();
getch();
}

OUTPUT
11. Write a program in C++ to implement concept of Constructor and
Destructor.

#include <iostream.h>
#include <conio.h>
class Line
{
public:
void setLength( int len );
double getLength( void );
Line();
~Line();

private:
double length;
};
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl; OUTPUT
}
void Line::setLength( int len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
void main()
{
Line line;

line.setLength(6.0);
cout << "Length of line : " <<
line.getLength() <<endl;

getch();
}

12. Write a program in C++ to implement multiple inheritnce in C++.

#include<iostream.h>
#include<conio.h>
class baseclass1
{
protected:
int acc;
char name[20];
public:
void enter1()
{
cout<<" Enter The Accession Number : ";
cin>>acc;
cout<<" Enter The Name Of The Author : ";
cin>>name;
}
};
class baseclass2
{
protected:
char title[30],pname[20];
int year;
public:
void enter2()
{
cout<<" Enter The Title Of Book : ";
cin>>title;
cout<<" Enter The Year Of Publication : ";
cin>>year;
cout<<" Enter The Publisher Name : ";
cin>>pname;
}
};
class derivedclass : public baseclass1,public baseclass2
{
protected:
float cost;
public:
void enter3()
{
cout<<" Enter The Cost Of Book : ";
cin>>cost;
}
void show();
};
void derivedclass :: show()
{
cout<<endl<<"---------- Details Of The Book ----------";
cout<<endl<<" Accession Number : "<<acc;
cout<<endl<<" Author Name : "<<name;
cout<<endl<<" Title Of The Book : "<<title;
cout<<endl<<" Year Of Publication : "<<year;
cout<<endl<<" Publisher Name : "<<pname;
cout<<endl<<" Cost Of Book : "<<cost;
}
void main()
{
clrscr();
derivedclass d;
d.enter1();
d.enter2();
d.enter3();
d.show();
getch();
}

OUTPUT

13. Write a program in C++ to implement multilevel inheritance.

#include<iostream.h>
#include<conio.h>
class baseclass
{
protected:
char name[20],gender[10];
int age;
public:
void enter1()
{
cout<<" Enter The Name Of The Patient : ";
cin>>name;
cout<<" Enter The Gender Of The Patient : ";
cin>>gender;
cout<<" Enter The Age Of The Patient : ";
cin>>age;
}
};
class derivedclass1 : public baseclass
{
protected:
int bedno;
char nature[50];
public:
void enter2()
{
cout<<" Enter The Bed Number Of The Patient : ";
cin>>bedno;
cout<<" Enter The Nature Of Illnes Of The Patient : ";
cin>>nature;
}
};
class derivedclass2 : public derivedclass1
{
protected:
int date;
public:
void enter3()
{
cout<<" Enter The Date Of Admission Of The Patient : ";
cin>>date;
}
void show();
};
void derivedclass2::show()
{
cout<<endl<<"---------- DETAIL OF THE PATIENT ----------";
cout<<endl<<" Name : "<<name;
cout<<endl<<" Gender : "<<gender;
cout<<endl<<" Age : "<<age;
cout<<endl<<" Bed Number : "<<bedno;
cout<<endl<<" Nature Of Illness : "<<nature;
cout<<endl<<" Date Of Admission : "<<date;
}
void main()
{
clrscr();
derivedclass2 d2;
d2.enter1();
d2.enter2();
d2.enter3();
d2.show();
getch();
}

OUTPUT

14. Write a program in C++ to calculate frequency of “to” in Text file


“notes.txt”.
#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<string.h>
#include<iostream.h> OUTPUT
void main()
{
clrscr();
char word[20];
int count=0;
ifstream fil("notes.txt");
if(!fil)
{
cout<<"could not open file";
exit(-1);
}
else
{
while(!fil.eof())
{
fil>>word;
if(strcmp(word,"to")==0)
count++;
}
}
fil.close();

cout<<"count of to in the file


is:"<<count;
getch();
}

15. Write a program in C++ to understand pointers.


#include<iostream.h>
#include<conio.h>
char*match(char,char*);
void main()
{ clrscr();
char str[80], ch, *pt;
cout << "Enter Any string [below 80 chars] :\n ";
cin.getline(str,80);
cout<<"enter a character to nbe searched for\n";
cin>>ch;
pt=NULL;
pt = match(ch,str);
if(*pt)
{ cout<<endl;
for( ;(*pt!='\0');pt++)
cout<<*pt;
}
else
cout<<"no match found\n";
cout<<endl;
getch();
}
char*match(char c,char* p)
{
while ((c != *p) && (*p))
p++;
return(p);
}

OUTPUT

16. Write a program in C++ to calculate sum of 2 matrices using Array.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";


cin >> r;

cout << "Enter number of columns (between 1 and 100): ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

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


for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
getch();
}

OUTPUT
17. Write a program in C++ to implement stack using array.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
class Stack {
private:
int item, i;
int arr_stack[MAX_SIZE];
int top;

public:

Stack() {
top = 0;

void push() {
if (top == MAX_SIZE)
cout << "\n## Stack is Full!";
else {
cout << "\nEnter The Value to be pushed : ";
cin>>item;
cout << "\n## Position : " << top << ", Pushed Value :" << item;
arr_stack[top++] = item;
}
}

void pop() {
if (top == 0)
cout << "\n## Stack is Empty!";
else {
--top;
cout << "\n## Position : " << top << ", Popped Value :" << arr_stack[top];
}
}

void display()
{
cout << "\n## Stack Size : " << top;
for (i = (top - 1); i >= 0; i--)
cout << "\n## Position : " << i << ", Value :" << arr_stack[i];
}
};

int main()
{
clrscr();
int choice, exit_p = 1;
Stack obj;
do {
cout << "\n\nStack Main Menu";

cout << "\t1.Push \t2.Pop \t3.Display \tOthers to exit";


cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3: OUTPUT
obj.display();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);

return 0;
}

18. Write a program in C++ to implement queue using array.


#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 100
int main()

{
clrscr();
int item, choice, i;
int arr_queue[MAX_SIZE];
int rear = 0;
int front = 0;
int exit = 1;
do {
cout << "\nQueue Main Menu";

cout << "\t1.Insert \t2.Remove \t3.Display \tOthers to exit";


cout << "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
if (rear == MAX_SIZE)
cout << "\n## Queue Reached Max!!";
else {
cout << "\nEnter The Value to be Insert : ";
cin>>item;
cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;
arr_queue[rear++] = item;
}
break;
case 2:
if (front == rear)
cout << "\n## Queue is Empty!";
else {
cout << "\n## Position : " << front << " , Remove Value :" <<
arr_queue[front];
front++;
}
break;
case 3:
cout << "\n## Queue Size : " << (rear - front);
for (i = front; i < rear; i++)
cout << "\n## Position : " << i << " , Value : " << arr_queue[i];
break;
default:
exit = 0;
break;
}
} while (exit);

return 0;
}

OUTPUT

19. Write a program in C++ to implement Circular Queue.


#include<iostream.h>
#include<conio.h>
include<process.h>
class queue
{int data[10];
int front,rear;
public:
queue()
{front=-1;
rear=-1;
}
void add();
void remove();
void display();
};
void queue::add()
{if((rear+1==front)||(rear==9&&front==0))
{cout<<"Overflow ";
}
else
{if((rear==-1) &&(front==-1))
{rear=0;
front=0;
}
else if(rear==9)
{rear=0;
}
else
{rear++;
}
cout<<"Enter the element ";
cin>>data[rear];
}
}
void queue::remove()
{if(front==-1&&rear==-1)
{cout<<"Underflow ";
}
else
{if(front==9)
{front=0;
}
else if(front==rear)
{front=-1;
rear=-1;
}
else
{front++;
}
}
}
void queue::display()
{int i=0,n=9;
if(rear==-1)
{cout<<"No elements.."<<endl;
}
else
{ if(rear>front)
{for(i=0;i<front;i++)
{cout<<"_";
}
for(i=front;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<n;i++)
{cout<<"_";
}
}
else
{for(i=0;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<front;i++)
{cout<<"_";
}
for(i=front;i<n;i++)
{cout<<data[i];
}
}}
}
void main()
{clrscr();
int ch;
queue queue;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:queue.add();
goto X;
case 2:queue.remove();
goto X;
case 3:queue.display();
goto X;
case 4:exit(0);
}
getch();
}

OUTPUT

You might also like