Professional Documents
Culture Documents
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter value of a: ";
cin>>a;
cout<<"Enter value of b: ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"After swap a: "<<a<<"b: "<<b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter value of a: ";
cin>>a;
cout<<"Enter value of b: ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"After swap a: "<<a<<"b: "<<b;
getch();
}
Following C++ program ask to the user to enter the two number to swap them and display the output on the
screen :
/* C++ Program - Swap Two Numbers */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, swap;
cout<<"Enter two number : ";
cout<<"\nFirst Number : ";
cin>>num1;
cout<<"Second Number : ";
cin>>num2;
swap=num1;
num1=num2;
num2=swap;
cout<<"The value of first and second number after swapping
is \n";
cout<<"First Number = "<<num1<<"\n"<<"Second Number =
"<<num2;
getch();
}
5. #include <iostream>
using namespace std;
int main()
{
int x, y, temp;
cout << "Enter the value of x and y:" << endl;
cin >> x >> y;
cout << "Before swapping x=" << x << ", y=" << y << endl;
/*Swapping logic */
temp = x; x = y; y = temp;
cout << "After swapping x=" << x << ", y=" << y << endl;
return 0;
}
#include<iostream>
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;
}
}
return 0;
}
Output
//Bubble Sort
//Driver Function
int main()
{
int input_ar[] = {10, 50, 21, 2, 6, 66, 802, 75, 24, 170};
int n = sizeof (input_ar) / sizeof (input_ar[0]);
bubble_sort (input_ar, n);
cout << "Sorted Array : " << endl;
for (int i = 0; i < n; ++i)
cout << input_ar[i] << " ";
return 0;
}
Output :
$ g++ BubbleSort.cpp
$ ./a.out
Sorted Array :
2 6 10 21 24 50 66 75 170 802
/* C++ Program - Bubble Sort */
8. #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();
}
When the above C++ program is compile and executed, it will produce the following
result:
Bubble Sort:-
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int hold;
int array[5];
cout<<"Enter 5 numbers: "<<endl;
for(int i=0; i<5; i++) { cin>>array[i];
}
cout<<endl;
cout<<"Orignally entered array by the user is:
"<<endl;
for(int j=0; j<5; j++)
{
cout<<array[j];
cout<<endl;
}
cout<<endl;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
cout<<"Sorted Array is: "<<endl;
for(int i=0; i<5; i++)
{
cout<<array[i]<<endl;
}
getch();
}