Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

C++ Practical File

The document contains 14 C++ programs with their source code. The programs cover topics like: 1) Calculating factorial of a number 2) Illustrating the working of object and class 3) Finding the discriminant of a quadratic equation 4) Bubble sorting an array 5) Calculating length of a string using pointers 6) Linear search in an array 7) Swapping two strings 8) Merging two arrays 9) Reversing an array 10) Drawing a pyramid pattern 11) Insertion sort of an array 12) Selection sort of an array 13) Deleting an element from an array 14) Printing Floyd's triangle pattern

Uploaded by

Preetish Chandra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

C++ Practical File

The document contains 14 C++ programs with their source code. The programs cover topics like: 1) Calculating factorial of a number 2) Illustrating the working of object and class 3) Finding the discriminant of a quadratic equation 4) Bubble sorting an array 5) Calculating length of a string using pointers 6) Linear search in an array 7) Swapping two strings 8) Merging two arrays 9) Reversing an array 10) Drawing a pyramid pattern 11) Insertion sort of an array 12) Selection sort of an array 13) Deleting an element from an array 14) Printing Floyd's triangle pattern

Uploaded by

Preetish Chandra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

TO FIND FACTORIAL OF A NUMBER

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i, fact=1;
cout<<"Enter a Number: ";
cin>> num;
for(i=num;i>0;i--)
{
fact=fact*i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
}

TO ILLUSTRATE THE WORKING OF OBJECT AND CLASS

#include<iostream.h>
#include<conio.h>
class Test
{
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
{
data1=d;
cout<<"Number: "<<data1;
}
float insertFloatData()
{
cout<<"\nEnter data: ";
cin>> data2;
return data2;
}
};
int main()
{
Test o1,o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
cout<<"You entered "<<secondDataOfObject2;
getch();
return 0;
}

3 TO FIND DISCRIMINANT FOR A QUADRATIC EQUATION

#include<iostream.h>
#include<conio.h>
#include<cmath.h>
int main()
{
float a,b,c,x1,x2,discriminant,realPart,imaginaryPart;
cout<<"Enter coefficients a,b and c: ";
cin>> a>>b>>c;
discriminant=b*b-4*a*c;
if(discriminant>0)
{
x1=(-b+sqrt(disctiminant))/(2*a);
x2=(-b-sqrt(disctiminant))/(2*a);
cout<<"Roots are real and different."<<endl;
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
else if(discriminant == 0)
{
cout<<"Roots are real and same."<<endl;
x1=(-b+sqrt(discriminant))/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
else
{
realPart=-b/(2*a);
imaginaryPart=sqrt(-discriminant)/(2*a);
cout<<"Roots are complex and different."<<endl;
cout<<"x1="<<realPart<<"+"<<imaginaryPart<<"i"<<endl;
cout<<"x2="<<realPart<<"-"<<imaginaryPart<<"i"<<endl;
}
getch();
return 0;
}

4 TO BUBBLE SORT AN ARRAY

#include<iostream,h>
#include<conio.h>
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array Elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after bubble sort: ";
for(i=0;i<n;++i)
cout<<" "<<a[i];
getch();
return 0;
}
PROGRAM - 5
PROGRAM TO CALCULATE LENGHT OF A STRING USING POINTER

#include<iostream.h>
#include<conio.h>
#define MAX_SIZE 100 // Maximum size of the string
using namespace std;
int main()
{
char text[MAX_ SIZE];
char * str = text;
int count = 0;

// Inputtin string from user


COUT<<"Enter any string: ";
cin>>text;

// Iterating though last element of the string


while(*(str++)!=' \ 0') count++;

cout<<"Length of "<<text<<" is "<<count;


getch();
return 0;
}

PROGRAM - 6
SEARCH AN ITEM IN AN 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 searched : ";
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 "<< os;
}
getch();
}

............../* C++ Program 7 - Swap Two Strings


*/.................................

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i=0, j=0;
char str1[20], str2[20], temp[20];
cout<<"Enter the First String : ";
gets(str1);
cout<<"Enter the Second String : ";
gets(str2);
cout<<"Strings before swapping are :\n";
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
while(str1[i]!='\0')
{
temp[j]=str1[i];
i++;
j++;
}
temp[j]='\0';
i=0, j=0;
while(str2[i]!='\0')
{
str1[j]=str2[i];
i++;
j++;
}
str1[j]='\0';
i=0, j=0;
while(temp[i]!='\0')
{
str2[j]=temp[i];
i++;
j++;
}
str2[j]='\0';
cout<<"Strings after swapping : \n";
cout<<"String 1 = "<<str1<<"\n";
cout<<"String 2 = "<<str2<<"\n";
getch();
}

................/* C++ Program 8 - Merge Two Arrays


*/...................................

#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 9 - 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();
}

10 PROGRAM TO DRAW PYRAMID PATTERN

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
cout<<"* ";
}
cout<<"\n";
}
getch();
}

............................../* C++ Program 11 - 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++ Program 12 - 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();
}

..................................../* C++ Program 13- 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 14- Print Floyd's Triangle


*/...........................

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int range, i, j, k=1;
cout<<"Enter the range (upto how many line ?) : ";
cin>>range;
cout<<"\nFloyd's Triangle :\n";
for(i=1; i<=range; i++)
{
for(j=1; j<=i; j++, k++)
{
cout<<k<<" ";
}
cout<<"\n";
}
getch();
}

............................./* C++ Program 15 - Binary Search


*/.......................................

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}

.............................../* C++ Program 16- Print ASCII Values of Characters


*/.............................

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int i;
for(i=1; i<255; i++)
{
ch=i;
cout<<i<<"-> "<<ch<<"\t";
}
getch();
}

........................../* C++ Program 17- Print Pascal Triangle


*/...................................
#include<iostream.h>
#include<conio.h>
long fact(int);
void main()
{
clrscr();
int i, n, c;
cout<<"Upto how many line (Enter number of rows) : ";
cin>>n;
for(i=0; i<n; i++)
{
for(c=0; c<=(n-i-2); c++)
{
cout<<" ";
}
for(c=0; c<=i; c++)
{
cout<<fact(i)/(fact(c)*fact(i-c));
cout<<" ";
}
cout<<"\n";
}
getch();
}

long fact(int n)
{
int c;
long res=1;
for(c=1; c<=n; c++)
{
res = res*c;
}
return (res);
}

............................./* C++ Program 18- Octal to Binary Conversion


*/..................................

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int i=0;
char octnum[1000];
cout<<"Enter any Octal Number : ";
cin>>octnum;
cout<<"Equivalent Binary Value = ";
while(octnum[i])
{
switch(octnum[i])
{
case '0' : cout<<"000";
break;
case '1' : cout<<"001";
break;
case '2' : cout<<"010";
break;
case '3' : cout<<"011";
break;
case '4' : cout<<"100";
break;
case '5' : cout<<"101";
break;
case '6' : cout<<"110";
break;
case '7' : cout<<"111";
break;
default : cout<<"\nInvalid Octal Digit "<<octnum[i];
break;
}
i++;
}
getch();
}

19 PROGRAM TO INSERT AND DELETE AN ELEMENT IN A QUEUE.

#include<iostream.h>
#include<conio.h>
#define maxq 10
#include<stdio.h>
int Q[10],FRONT=1,REAR,item,n,i,p,ch;
char choice;

void main()
{
clrscr();
void insert();
void del();
cout<<"Enter the number of elements to form the queue:";
cin>>n;
cout<<endl<<"Enter the elements: ";
for(i=1;i<=n;i++)
{
cin>>Q[i];
}
REAR=n;
cout<<"Front is"<<FRONT<<"\nRear is: "<<REAR;

start:
cout<<"\n1. Insertion";
cout<<"\n2. Deletion.";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insert();
cout<<"\nDo you want to continue?";
cin>>choice;
while(choice=='Y'||choice=='y')
goto start;
break;
case 2:
del();
cout<<"\nDo you want to continue?";
cin>>choice;
while(choice=='Y'||choice=='y')
goto start;
break;
default: cout<<"\nEnter right choice";
}
getch();
}
void insert()
{
cout<<"\nEnter the element to be inserted: ";
cin>>item;
if((FRONT==1)&&(REAR==maxq))
{
cout<<"\nOVERFLOW!!";
}
else if(FRONT==NULL && REAR==NULL)
{
FRONT=1;
REAR=1;
Q[REAR]=item;
}
else
{
REAR++;
Q[REAR]=item;
}
cout<<"\nThe elements in queue after insertion are: ";
for(i=1;i<=REAR;i++)
{
cout<<endl<<Q[i];
}
cout<<"\nAfter Insertion Front is:"<<FRONT<<"\nRear is: "<<REAR;

}
void del()
{
if((FRONT==NULL)&&(REAR==NULL))
{
cout<<"\nUNDERFLOW!!";
}
else if(FRONT==REAR)
{
FRONT=NULL;
REAR=NULL;
}
else
{
FRONT++;
}
cout<<"\nQueue after deletion is: ";
for(i=FRONT;i<=REAR;i++)
{
cout<<endl<<Q[i];
}
cout<<"\nAfter deletion:\nFront is:"<<FRONT<<"\nRear is: "<<REAR;
}
20 PUSHING IN STACK

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

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<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter 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;
}
void display(int stack[], int top)
{
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

21 POPPING FROM STACK


#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";
}
}

...................................................................................
...................................................................................
...
SQL STARTS...from prog 22 to 25

You might also like