0% found this document useful (0 votes)
6 views5 pages

C++ Programs: Sorting, Swapping, Search

The document contains multiple C++ programs demonstrating basic programming concepts. It includes examples of sorting an array in ascending order, swapping two numbers using pointers, implementing a binary search technique, calculating the ratio and reciprocal of two numbers, and computing the area and circumference of a circle. Each program is structured with input prompts, processing logic, and output displays.

Uploaded by

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

C++ Programs: Sorting, Swapping, Search

The document contains multiple C++ programs demonstrating basic programming concepts. It includes examples of sorting an array in ascending order, swapping two numbers using pointers, implementing a binary search technique, calculating the ratio and reciprocal of two numbers, and computing the area and circumference of a circle. Each program is structured with input prompts, processing logic, and output displays.

Uploaded by

urfavharsh143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Ascending order Array

#include <iostream.h>

#include <conio.h>

int main() {

clrscr(); // Clear screen in Turbo C++

int a[10], n, i, j, temp;

// Input number of elements

cout << "Enter how many numbers: ";

cin >> n;

// Input array elements

cout << "Enter " << n << " elements:\n";

for (i = 0; i < n; i++) {

cin >> a[i];

// Sorting logic (Ascending Order)

for (i = 0; i < n; i++) {

for (j = i + 1; j < n; j++) {

if (a[i] > a[j]) { // Swap if previous element is bigger

temp = a[i];

a[i] = a[j];

a[j] = temp;

// Display sorted array

cout << "Sorted Array:\n";

for (i = 0; i < n; i++) {

cout << a[i] << "\n";

getch(); // Wait for key press

return 0; }
2. Swapping 2 numbers

#include <iostream.h>

#include <conio.h>

// Function to swap two numbers using pointers

void swapNumbers(int *x, int *y) {

int temp = *x; // store value of x in temp

*x = *y; // put value of y into x

*y = temp; // put temp (original x) into y

int main() {

clrscr(); // Clear screen in Turbo C++

int a, b;

// Input two numbers

cout << "Enter first number: ";

cin >> a;

cout << "Enter second number: ";

cin >> b;

cout << "\nBefore swapping: a = " << a << ", b = " << b << "\n";

swapNumbers(&a, &b); // Send addresses of a and b

cout << "After swapping: a = " << a << ", b = " << b << "\n";

getch(); // Wait for key press

return 0;

}
3. Binary Search Technique

#include <iostream.h>

#include <conio.h>

int main() {

clrscr(); // Clear screen in Turbo C++

int n, key;

cout << "Enter number of elements: ";

cin >> n;

int a[10]; // Max 10 elements

cout << "Enter " << n << " elements in ascending order:\n";

for(int i = 0; i < n; i++)

cin >> a[i];

cout << "Enter element to search: ";

cin >> key;

int low = 0, high = n - 1, mid;

int found = 0; // Use int instead of bool

while(low <= high) {

mid = (low + high) / 2;

if(a[mid] == key) {

found = 1; // 1 = true

break; // element found }

else if(a[mid] < key)

low = mid + 1; // search right half

else

high = mid - 1; // search left half }

if(found)

cout << "Element " << key << " found at position " << mid + 1 << "\n";

else

cout << "Element not found\n";

getch(); // Wait for key press

return 0;}
4. Ratio and Reciprocal

#include <iostream.h>

#include <conio.h>

int main() {

clrscr(); // Clear screen in Turbo C++

float a, b;

// Input two numbers

cout << "Enter first number: ";

cin >> a;

cout << "Enter second number: ";

cin >> b;

// Calculate ratio

if(b != 0)

cout << "Ratio (a:b) = " << a/b << " : 1\n";

else

cout << "Cannot calculate ratio (division by zero)\n";

// Calculate reciprocal

if(a != 0)

cout << "Reciprocal of a = " << 1/a << "\n";

else

cout << "Reciprocal of a is not defined (division by zero)\n";

if(b != 0)

cout << "Reciprocal of b = " << 1/b << "\n";

else

cout << "Reciprocal of b is not defined (division by zero)\n";

getch(); // Wait for key press

return 0;

}
6. Area and circumference of circle.

#include <iostream.h>

#include <conio.h>

class Circle {

float radius;

public:

void input() {

cout << "Enter radius of the circle: ";

cin >> radius;

void calculateArea() {

float area = 3.1416 * radius * radius;

cout << "Area of the circle = " << area << "\n";

void calculateCircumference() {

float circumference = 2 * 3.1416 * radius;

cout << "Circumference of the circle = " << circumference << "\n";

};

int main() {

clrscr(); // Clear screen in Turbo C++

Circle c;

[Link]();

[Link]();

[Link]();

getch(); // Wait for key press

return 0;

You might also like