0% found this document useful (0 votes)
32 views25 pages

CP Exp All

The document contains multiple programming experiments in C++ and C, showcasing various algorithms and data structures. Key topics include string manipulation, sorting algorithms (insertion and bubble sort), priority queues, Fibonacci sequence generation, and palindrome checking. Each experiment includes code snippets, input prompts, and expected outputs.

Uploaded by

amit129806
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)
32 views25 pages

CP Exp All

The document contains multiple programming experiments in C++ and C, showcasing various algorithms and data structures. Key topics include string manipulation, sorting algorithms (insertion and bubble sort), priority queues, Fibonacci sequence generation, and palindrome checking. Each experiment includes code snippets, input prompts, and expected outputs.

Uploaded by

amit129806
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
You are on page 1/ 25

Experiment No 5

Input :

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int main()

char base[48] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

char c[100];

int i, l;

prin ("\n\tEnter String: ");

fgets(c, sizeof(c), stdin);

c[strcspn(c, "\n")] = '\0'

l = strlen(c);

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

char ch = toupper(c[i]);

int j;

for (j = 0; j < 47; j++)

if ((base[j] == ch) && ch != '`' && ch != 'Q' && ch != 'A' && ch != 'Z')

prin ("%c", base[j - 1]);

break;

}
else if (ch == ' ')

prin (" ");

break;

return 0;

Output :
Experiment No 1

Input :

#include <iostream>

using namespace std;

int cycle(long int n) {

int i = 1;

while (n != 1) {

if (n % 2 == 0) {

n = n / 2;

} else {

n = (3 * n) + 1;

i++;

return i;

int main() {

int a, b;

cout << "Enter pairs of numbers (Ctrl+C to exit):" << endl;

while (cin >> a >> b) {

int low = min(a, b);


int high = max(a, b);

int maxCycle = 0;

for (int i = low; i <= high; i++) {

int currentCycle = cycle(i);

if (currentCycle > maxCycle) {

maxCycle = currentCycle;

cout << a << " " << b << " " << maxCycle << endl;

return 0;

Output :
Experiment No 6

Inser on sort :

Input :

#include <iostream>

using namespace std;

void inser onSort(int arr[], int n)

for (int I = 1; I < n; i++) // Start from 1, first element is already “sorted”

int key = arr[i];

int j = I – 1;

while (j >= 0 && arr[j] > key)

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

void print(int arr[], int n)

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

cout << arr[i] << “ “;


cout << endl;

int main()

int arr[] = {5, 2, 3, 9, 1};

int size = sizeof(arr) / sizeof(arr[0]);

cout << “Before sor ng: “;

print(arr, size);

inser onSort(arr, size);

cout << “A er sor ng: “;

print(arr, size);

return 0;

Output :
Bubble sort :

Input :

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int n)

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

for (int j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

// Swap adjacent elements

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

void print(int arr[], int n)

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


cout << arr[i] << " ";

cout << endl;

int main()

int arr[] = {5, 2, 3, 4, 1};

int size = sizeof(arr) / sizeof(arr[0]);

cout << "Before sor ng: ";

print(arr, size);

bubbleSort(arr, size);

cout << "A er sor ng: ";

print(arr, size);

return 0;

Output :
Experiment No -2
Program:
#include <bits/stdc++.h>

using namespace std;

int main()

string s[5] = {" ", " -- ", " |", "| ", "| |"};

string digit[10][5] = {

{s[1], s[4], s[0], s[4], s[1]}, // 0

{s[0], s[2], s[0], s[2], s[0]}, // 1

{s[1], s[2], s[1], s[3], s[1]}, // 2

{s[1], s[2], s[1], s[2], s[1]}, // 3

{s[0], s[4], s[1], s[2], s[0]}, // 4

{s[1], s[3], s[1], s[2], s[1]}, // 5

{s[1], s[3], s[1], s[4], s[1]}, // 6

{s[1], s[2], s[0], s[2], s[0]}, // 7

{s[1], s[2], s[1], s[4], s[1]}, // 8

{s[1], s[4], s[1], s[2], s[1]}, // 9

};

string num;

cout << "Enter a Number : ";

cin >> num;

int d = 0;

for (int col = 0; col < 5; col++)

for (int index = 0; index < num.length(); index++)

d = (int)num[index] - int('0');
cout << digit[d][col] << " ";

cout << endl;

return 0;

Output:
Experiment No.3

Q1.Write a program for jolly jumper.

Program :

#include <iostream>

using namespace std;

bool isJollyJumper(int arr[], int n) {

if (n == 1) {

return true;

bool diff[n - 1] = {false};

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

int diff_value = arr[i] - arr[i - 1];

if (diff_value < 0) {

diff_value = -diff_value;

if (diff_value >= 1 && diff_value < n) {

diff[diff_value - 1] = true;

} else {

return false;

for (int i = 0; i < n - 1; ++i) {

if (!diff[i]) {

return false;

}
}

return true;

int main() {

int n;

cout << "Enter the number of elements in the sequence: ";

cin >> n;

int arr[n];

cout << "Enter the sequence: ";

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

cin >> arr[i];

if (isJollyJumper(arr, n)) {

cout << "The sequence is a Jolly Jumper!" << endl;

}else {

cout << "The sequence is not a Jolly Jumper!" << endl;

return 0;

Output:
Q2.Write a program for simple calculator using template.

Program:

#include <iostream>

using namespace std;

template<class T>

T cal(T a, T b)

cout<<"addition is= "<<a+b<<endl;

cout<<"substraction is= "<<a-b<<endl;

cout<<"multiplication is= "<<a*b<<endl;

cout<<"division is= "<<a/b<<endl;

int main()

int a,b;

cout<<"Enter a two numbers:";

cin>>a>>b;

cal(a,b);

return 0;

Output:
Q3.Write a program swapping two numbers using template .

Program:

#include<iostream>

using namespace std;

template <class T>

void Swap(T &x, T &y) {

T temp;

temp = x;

x = y;

y = temp;

int main()

int x, y;

cout<<"Enter a number:"<<endl;

cin>>x;

cout<<"Enter a number:"<<endl;

cin>>y;

cout << "Before Swap:";


cout << "\nx value is:" << x;

cout << "\ny value is:" << y;

Swap(x, y);

cout << "\n\nAfter Function Templates:\n";

cout << "\nx value is:" << x;

cout << "\ny value is:" << y;

return 0;

Output:
Experiment No-4
Program:
#include<iostream>

#define N 20

using namespace std;

int Q[N], Pr[N];

int r = -1, f = -1;

void enqueue(int data, int p) // Enqueue function to insert data and its priority in queue

int i;

if ((f == 0) && (r == N - 1)) {

cout << "Queue is full" << endl;

return;

// If queue is empty

if (f == -1) {

f = r = 0;

Q[r] = data;

Pr[r] = p;

return;

// If elements exist but rear is at the end

if (r == N - 1) {

// Shift elements to the beginning

for (i = f; i <= r; i++) {

Q[i - f] = Q[i];

Pr[i - f] = Pr[i];

}
r = r - f;

f = 0;

// Find position to insert based on priority (descending)

for (i = r; i >= f; i--) {

if (p > Pr[i]) {

Q[i + 1] = Q[i];

Pr[i + 1] = Pr[i];

} else {

break;

Q[i + 1] = data;

Pr[i + 1] = p;

r++;

void print() // Print the data of the queue

if (f == -1) {

cout << "Queue is empty" << endl;

return;

for (int i = f; i <= r; i++) {

cout << "Element = " << Q[i] << " | Priority = " << Pr[i] << endl;

void dequeue() // Remove the data from front

{
if (f == -1) {

cout << "Queue is Empty" << endl;

return;

cout << "Deleted Element = " << Q[f] << " | Its Priority = " << Pr[f] << endl;

if (f == r) {

// Last element removed

f = r = -1;

} else {

f++;

int main()

int opt, n, i, data, p;

cout << "Priority Queue Program using Arrays" << endl;

do {

cout << "\nEnter Your Choice:\n";

cout << "1. Insert data into Queue\n";

cout << "2. Show data in Queue\n";

cout << "3. Delete data from Queue\n";

cout << "0. Exit\n";

cin >> opt;

switch (opt) {

case 1:

cout << "Enter the number of data elements: ";

cin >> n;

cout << "Enter your data and priority (higher number = higher priority):" << endl;
for (i = 0; i < n; i++) {

cin >> data >> p;

enqueue(data, p);

break;

case 2:

print();

break;

case 3:

dequeue();

break;

case 0:

cout << "Exiting..." << endl;

break;

default:

cout << "Incorrect Choice" << endl;

} while (opt != 0);

return 0;

Output:
Experiment No 7

Input :

#include <iostream>

using namespace std;

int main() {

long int fibb1 = 0, fibb2 = 1, sum;

int a, b, count = 0;

cout << "Enter lower and upper limits (a & b): ";

cin >> a >> b;

cout << "\nFibonacci numbers between " << a << " and " << b << " are:\n";

while (true) {

sum = fibb1 + fibb2;

if (sum >= b) // Stop if sum exceeds or reaches upper limit

break;

if (sum > a) {

cout << sum << "\t";

count++;

fibb1 = fibb2;

fibb2 = sum;

cout << "\n\nTotal numbers in range = " << count << endl;

return 0;

}
Output :
Experiment No 8

Input :

#include <stdio.h>

long long reverseNumber(long long num) {

long long reversed = 0;

while (num > 0) {

reversed = reversed * 10 + (num % 10);

num /= 10;

return reversed;

int isPalindrome(long long num) {

return num == reverseNumber(num);

void reverseAndAdd(long long num, int *itera ons, long long *palindrome) {

*itera ons = 0;

while (!isPalindrome(num)) {

long long reversed = reverseNumber(num);

num += reversed;

(*itera ons)++;

if (*itera ons > 1000) { // Safety cap

*palindrome = -1; // Indicate failure


return;

*palindrome = num;

int main() {

int t;

scanf("%d", &t);

while (t--) {

long long num;

scanf("%lld", &num);

int itera ons;

long long palindrome;

reverseAndAdd(num, &itera ons, &palindrome);

if (palindrome == -1) {

prin ("Palindrome not found within 1000 itera ons\n");

} else {

prin ("%d %lld\n", itera ons, palindrome);

return 0;

}
Output :

You might also like