0% found this document useful (0 votes)
12 views8 pages

C++ Programs Code Tantra

The document contains multiple C++ programming experiments demonstrating various concepts such as palindrome testing, searching elements in an array, recursion for factorial calculation, call by value, call by reference, call by address, function overloading for volume calculation, salary and bonus calculation based on sales, and an object counting system using multiple inheritance. Each experiment includes code snippets and explanations of the functionality. The programs cover fundamental programming principles and object-oriented programming concepts.

Uploaded by

arvindhaari
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)
12 views8 pages

C++ Programs Code Tantra

The document contains multiple C++ programming experiments demonstrating various concepts such as palindrome testing, searching elements in an array, recursion for factorial calculation, call by value, call by reference, call by address, function overloading for volume calculation, salary and bonus calculation based on sales, and an object counting system using multiple inheritance. Each experiment includes code snippets and explanations of the functionality. The programs cover fundamental programming principles and object-oriented programming concepts.

Uploaded by

arvindhaari
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

C++ programs

EXP-1
1.1.1. Palindrome Test

#include <iostream>
using namespace std;

int main()
{
int num, r, rev=0,temp;
cout<<"Enter the Number=";
cin>>num;
temp=num;
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
num=num/10;
}
if(temp==rev)
cout<<"Number is Palindrome";
else
cout<<"Number is not Palindrome";
return 0;
}

EXP-2

1.1.2. Search An Element In A Given Array

#include <iostream>
using namespace std;
int main()
{
int arr[10],i,num,n,cnt=0,pos;
cout<<"Enter Array Size : ";
cin>>n;
cout<<"Enter Array Elements:\n";
for(i=0; i<n; i++)
{
cout<<"";
cin>>arr[i];
}
cout<<"Enter Element to be searched:";
cin>>num;
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
cnt=1;
pos=i+1;
break;
}
}
if(cnt==0)
{
cout<<"Element Not Found";
}
else
{
cout<<"Element "<<num<<" Found At Position "<<pos;
}
return 0;
}

EXP-3

1.1.3. Factorial Using Recursion

#include <iostream>
using namespace std;

unsigned long int factorial(int n);

int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;
}

unsigned long int factorial(int n) {


if (n > 1)
return n * factorial(n - 1);
else
return 1;
}
EXP-4

1.1.4. Call by Value Demonstration

#include <iostream>
using namespace std;

// Value of x gets copied into 'a' (call by value)


void increment(int a) {
a++;
cout << "Value in Function increment:" << a;
}

int main() {
int x;

cout << "Enter a number: ";


cin >> x;

increment(x);

cout << "\nValue in Function main:" << x;

return 0;
}

1.1.5. Call by Reference Demonstration

include <iostream>
using namespace std;

// Function demonstrating call by reference


void increment(int &a) {
a++;
cout << "Value in Function increment:" << a;
}

int main() {
int x;

cout << "Enter a number: ";


cin >> x;

increment(x);

cout << "\nValue in Function main:" << x << endl; // NEW LINE added here

return 0;
}
1.1.6. Call by Address Demonstration

include <iostream>
using namespace std;

// Function demonstrating call by address (pointer)


void increment(int *a) {
(*a)++;
cout << "Value in Function increment: " << *a;
}

int main() {
int x;

cout << "Enter a number: ";


cin >> x;

increment(&x);

cout << "\nValue in Function main: " << x << endl;

return 0;
}

EXP-5

1.1.7. Volume Calculation using Function Overloading

#include <iostream>
#include <cmath>

const double PI = 3.14159;

double volume(double radius, double height) {


return PI * pow(radius, 2) * height;
}

// Function overloading to calculate the volume of a cuboid


double volume(double length, double width, double height) {
return length * width * height;
}

int main() {
double radius, height, length, width;

std::cout << "Enter the radius and height of the cylinder: ";
std::cin >> radius >> height;
double cylinderVolume = volume(radius, height);
std::cout << "Volume of the cylinder: " << cylinderVolume << std::endl;
std::cout << "Enter the length, width, and height of the cuboid: ";
std::cin >> length >> width >> height;
double cuboidVolume = volume(length, width, height);
std::cout << "Volume of the cuboid: " << cuboidVolume << std::endl;

return 0;
}

EXP-6

1.1.8. Salary and Bonus Calculation Based on Sales Performance

#include <iostream>
using namespace std;
class MedicalRepresentative {
private:

double sales;
double salary;
double bonus;
double total;

public:

MedicalRepresentative(double s) : sales(s), salary(0), bonus(0), total(0) {


if (sales > 150000) {
salary = 30000;
bonus = 0.10 * sales;
} else {
salary = 20000;
bonus = 0.05 * sales;
}
total = salary + bonus;
}

void display() {
cout << salary << endl;
cout << bonus << endl;
cout << total << endl;
}

// Destructor
~MedicalRepresentative() {
// Destructor logic (if needed)
}

};
int main() {
double sales;

// Input sales amount


std::cin >> sales;

// Create MedicalRepresentative object


MedicalRepresentative mr(sales);

// Display salary and bonus


mr.display();

return 0;
}

EXP-7

1.1.9. Object Counting System in C++: Managing Counts with Multiple Inheritance

#include <iostream>

class A {
public:
A() {
++countA;
}

static int countA;

virtual void count() {


std::cout << "Count of A objects: " << countA << std::endl;
}

static int getCountA() {


return countA;
}
};
int A::countA = 0;

class B {
public:
static int countB;

B() {
++countB;
}
virtual void count() {
std::cout << "Count of B objects: " << countB << std::endl;
}

static int getCountB() {


return countB;
}
};
int B::countB = 0;

// Write your code here

class C : public A, public B {


public:
static int countC;

C() {
++countC;
}

void count() override {


std::cout << "Count of C objects: " << countC << std::endl;
}

static int getCountC() {


return countC;
}
};
int C::countC = 0;

// Write your code here

void displayCounts() {
std::cout << "count A: " << A::getCountA() << std::endl;
std::cout << "count B: " << B::getCountB() << std::endl;
std::cout << "count C: " << C::getCountC() << std::endl;
}

int main() {
int choice;
while (true) {
std::cout << "1. object A\n";
std::cout << "2. object B\n";
std::cout << "3. object C\n";
std::cout << "4. Display\n";
std::cout << "5. Exit\n";
std::cout << "Enter:";
std::cin >> choice;

switch (choice) {
case 1: new A(); break;
case 2: new B(); break;
case 3: new C(); break;
case 4: displayCounts(); break;
case 5: return 0;
default: std::cout << "Invalid choice.\n";
}
}
}

You might also like