0% found this document useful (0 votes)
14 views21 pages

print oop1

The document contains multiple C++ code examples demonstrating various programming concepts. These include size of data types, number conversions, temperature conversions, summation of natural numbers, matrix input/output, vowel counting, swapping numbers, inline functions, area calculations, student and employee details management, and calculations involving numbers divisible by nine. Each example is self-contained and illustrates specific functionalities in C++.

Uploaded by

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

print oop1

The document contains multiple C++ code examples demonstrating various programming concepts. These include size of data types, number conversions, temperature conversions, summation of natural numbers, matrix input/output, vowel counting, swapping numbers, inline functions, area calculations, student and employee details management, and calculations involving numbers divisible by nine. Each example is self-contained and illustrates specific functionalities in C++.

Uploaded by

wexiva7911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1a

Code:

#include<iostream>

using namespace std;

int main(){

cout << "Size of char : " << sizeof(char) << " byte " << endl;

cout << "Size of int : " << sizeof(int) << " bytes " << endl;

cout << "Size of float : " << sizeof(float) << " bytes" << endl;

cout << "Size of double : " << sizeof(double) << " bytes " << endl;

cout << "Size of short int : " << sizeof(short int) << " bytes " << endl;

cout << "Size of long int : " << sizeof(long int) << " bytes " << endl;

return 0;

Output:
1B

Code:

#include <iostream>

#include <iomanip> // For manipulator functions

using namespace std;

int main() {

int value;

// Taking input

cout << "Enter a numeric value: ";

cin >> value;

// Display in decimal, hexadecimal, and octal format

cout << "Decimal: " << dec << value << endl;

cout << "Hexadecimal: " << hex << value << endl;

cout << "Octal: " << oct << value << endl;

return 0;

Output:
1C

Code:

#include<iostream>

using namespace std;

int main(){

float celsius, fahrenheit;

// Celsius to Fahrenheit conversion

cout << "Enter temperature in Celsius: ";

cin >> celsius;

fahrenheit = (celsius * 9.0 / 5.0) + 32.0;

cout << celsius << " °C is " << fahrenheit << " °F" << endl;

// Fahrenheit to Celsius conversion

cout << "Enter temperature in Fahrenheit: ";

cin >> fahrenheit;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0;

cout << fahrenheit << " °F is " << celsius << " °C" << endl;

return 0;

Output:
2A

Code:

#include<iostream>

using namespace std;

int main(){

int n, sum = 0;

cout<<"Enter a nautral number: ";

cin>>n;

cout<<"Natural numbers up to "<<n<<" are :";

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

cout<<i<< " ";

sum += i;

cout<<endl;

cout<<" sum of natural numbers from 1 to "<<n<<" is "<<sum<<endl;

return 0;

Output:
2B

Code:

#include <iostream>

using namespace std;

// Function to read a matrix

void readMatrix(int rows, int cols) {

int matrix[rows][cols]; // Declare matrix with dynamic size

cout << "Enter elements of the matrix:\n";

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

for (int j = 0; j < cols; j++) {

cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

cin >> matrix[i][j];

cout << "Matrix is:\n";

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

{ for (int j = 0; j < cols; j++)

cout << matrix[i][j] << " ";

cout << endl;

int main() {

int m, n;

cout << "Enter number of rows: ";


cin >> m;

cout << "Enter number of columns: ";

cin >> n;

readMatrix(m, n);

return 0;

Output:
2C

Code

#include <iostream>

#include <cctype> // For tolower() function

using namespace std;

int countVowels(const string& str) {

int count = 0;

for (int i = 0; i < str.length(); i++) {

char lowerCh = tolower(str[i]); // Convert character to lowercase

if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u') {

count++;

return count;

int main() {

string input;

cout << "Enter a string: ";

getline(cin, input);

int vowelCount = countVowels(input);

cout << "Number of vowels in the string: " << vowelCount << endl;

return 0;

}
Output:
3A

#include <iostream>

using namespace std;

class SwapNumbers {

public:

// Call by value function

void swapByValue(int a, int b)

{ int temp = a;

a = b;

b = temp;

cout << "After swap (Call by Value): a = " << a << ", b = " << b << endl;

// Call by reference function

void swapByReference(int &a, int &b)

{ int temp = a;

a = b;

b = temp;

cout << "After swap (Call by Reference): a = " << a << ", b = " << b << endl;

};

int main() {

int x, y;

SwapNumbers obj;

// Input values

cout << "Enter two numbers: ";

cin >> x >> y;


cout << "Before swap: x = " << x << ", y = " << y << endl;

// Call by value

obj.swapByValue(x, y);

cout << "After Call by Value in main: x = " << x << ", y = " << y << endl; // No change in main

// Call by reference

obj.swapByReference(x, y);

cout << "After Call by Reference in main: x = " << x << ", y = " << y << endl; // Swapped in main

return 0;

Output:
3B

Code:

#include <iostream>

using namespace std;

// Inline function to calculate square

inline int square(int num) {

return num * num;

// Inline function to calculate cube

inline int cube(int num) {

return num * num * num;

int main() {

int number;

// Input the number

cout << "Enter a number: ";

cin >> number;

// Calculate and display square and cube

cout << "Square of " << number << " is: " << square(number) << endl;

cout << "Cube of " << number << " is: " << cube(number) << endl;

return 0;

}
Output:
3C

#include <iostream>

using namespace std;

class AreaCalculator {

public:

// Function to calculate area of a rectangle

double area(double length, double width) {

return length * width;

// Function to calculate area of a triangle

double area(double base, double height, int) {

return 0.5 * base * height;

// Function to calculate surface area of a sphere

double area(double radius) {

return 4 * 3.1416 * radius * radius; // 4πr^2

};

int main() {

AreaCalculator calc;

double length, width, base, height, radius;

// Input for rectangle

cout << "Enter length and width of the rectangle: ";

cin >> length >> width;

cout << "Area of rectangle: " << calc.area(length, width) << endl;
// Input for triangle

cout << "Enter base and height of the triangle: ";

cin >> base >> height;

cout << "Area of triangle: " << calc.area(base, height, 0) << endl;

// Input for sphere

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

cin >> radius;

cout << "Surface area of sphere: " << calc.area(radius) << endl;

return 0;

Output:
4A

#include <iostream>

using namespace std;

class Student {

private:

string name;

int rollNumber;

long prn;

public:

// Method to enter student details

void enterDetails() {

cout << "Enter student's name: ";

getline(cin, name);

cout << "Enter roll number: ";

cin >> rollNumber;

cout << "Enter PRN: ";

cin >> prn;

// Method to display student details

void displayDetails() {

cout << "\nStudent Details:" << endl;

cout << "Name: " << name << endl;

cout << "Roll Number: " << rollNumber << endl;

cout << "PRN: " << prn << endl;

};
int main() {

Student student;

// Enter student details

student.enterDetails();

// Display student details

student.displayDetails();

return 0;

Output:
4B

#include <iostream>

using namespace std;

class Employee {

private:

string name;

double basicSalary, ta, da, grossSalary;

public:

// Method to input employee details

void inputDetails();

// Method to calculate TA and DA

void calculateAllowances();

// Method to calculate Gross Salary

void calculateGrossSalary();

// Method to display employee details

void displayDetails();

};

// Member function defined outside the class using scope resolution operator

void Employee::inputDetails() {

cout << "Enter employee's name: ";

getline(cin, name);

cout << "Enter basic salary: ";

cin >> basicSalary;

}
// Member function defined outside the class using scope resolution operator

void Employee::calculateAllowances() {

ta = 0.15 * basicSalary; // TA is 15% of basic salary

da = 0.30 * basicSalary; // DA is 30% of basic salary

// Member function defined outside the class using scope resolution operator

void Employee::calculateGrossSalary() {

grossSalary = basicSalary + ta + da;

// Member function defined outside the class using scope resolution operator

void Employee::displayDetails() {

cout << "\nEmployee Details:" << endl;

cout << "Name: " << name << endl;

cout << "Basic Salary: " << basicSalary << endl;

cout << "TA (15%): " << ta << endl;

cout << "DA (30%): " << da << endl;

cout << "Gross Salary: " << grossSalary << endl;

int main() {

Employee emp;

// Input employee details

emp.inputDetails();

// Calculate TA and DA

emp.calculateAllowances();
// Calculate gross salary

emp.calculateGrossSalary();

// Display employee details

emp.displayDetails();

return 0;

Output:
4C

#include <iostream>

using namespace std;

class DivisibleByNine {

private:

int count;

int sum;

public:

// Constructor to initialize count and sum

DivisibleByNine() : count(0), sum(0) {}

// Friend function to calculate numbers divisible by 9

friend void calculate(DivisibleByNine& obj);

};

void calculate(DivisibleByNine& obj) {

// Loop through numbers between 100 and 200

for (int i = 100; i <= 200; i++) {

if (i % 9 == 0) {

obj.count++;

obj.sum += i;

// Display the results

cout << "Number of integers divisible by 9 between 100 and 200: " << obj.count << endl;

cout << "Sum of these integers: " << obj.sum << endl;

}
int main() {

DivisibleByNine obj;

calculate(obj); // Call the friend function

return 0;

Output:

You might also like