0% found this document useful (0 votes)
5 views

C++_Practical_Questions_1_to_16

The document contains multiple C++ code examples demonstrating various programming concepts such as nested loops for multiplication tables, object-oriented programming for employee details, recursion for factorial calculation, multi-level inheritance, operator overloading for complex numbers, and basic file handling. Each example includes a brief description of its functionality, showcasing fundamental programming techniques. Overall, it serves as a practical guide for learning C++ programming through hands-on code snippets.
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)
5 views

C++_Practical_Questions_1_to_16

The document contains multiple C++ code examples demonstrating various programming concepts such as nested loops for multiplication tables, object-oriented programming for employee details, recursion for factorial calculation, multi-level inheritance, operator overloading for complex numbers, and basic file handling. Each example includes a brief description of its functionality, showcasing fundamental programming techniques. Overall, it serves as a practical guide for learning C++ programming through hands-on code snippets.
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/ 8

1.

Multiplication table using nested for loop

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cout << i * j << "\t";
}
cout << endl;
}
return 0;
}

2. Get employee details using OOP

#include <iostream>
using namespace std;

class Employee {
int id;
string name;
public:
void getDetails() {
cout << "Enter ID and Name: ";
cin >> id >> name;
}
void showDetails() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};

int main() {
Employee emp;
emp.getDetails();
emp.showDetails();
return 0;
}

3. Parameterized constructor

#include <iostream>
using namespace std;

class Student {
int roll;
public:
Student(int r) {
roll = r;
}
void display() {
cout << "Roll number: " << roll << endl;
}
};

int main() {
Student s1(101);
s1.display();
return 0;
}

4. Factorial using recursion

#include <iostream>
using namespace std;

int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n - 1);
}

int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial: " << factorial(n) << endl;
return 0;
}

5. Multi-level inheritance

#include <iostream>
using namespace std;

class A {
public:
void showA() {
cout << "Class A\n";
}
};

class B : public A {
public:
void showB() {
cout << "Class B\n";
}
};

class C : public B {
public:
void showC() {
cout << "Class C\n";
}
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}

6. Operator overloading

#include <iostream>
using namespace std;

class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0): real(r), imag(i) {}

Complex operator + (Complex const &obj) {


return Complex(real + obj.real, imag + obj.imag);
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2;
c3.display();
return 0;
}

7. Sort numbers

#include <iostream>
using namespace std;

int main() {
int a[5], i, j, temp;
cout << "Enter 5 numbers: ";
for (i = 0; i < 5; i++) cin >> a[i];

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


for (j = i + 1; j < 5; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

cout << "Sorted numbers: ";


for (i = 0; i < 5; i++) cout << a[i] << " ";
return 0;
}

8. Addition of matrices

#include <iostream>
using namespace std;

int main() {
int a[2][2], b[2][2], sum[2][2];
cout << "Enter first matrix:\n";
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> a[i][j];

cout << "Enter second matrix:\n";


for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> b[i][j];

cout << "Sum:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
cout << sum[i][j] << " ";
}
cout << endl;
}

return 0;
}

9. Length of string without using string functions

#include <iostream>
using namespace std;

int main() {
char str[100];
int length = 0;
cout << "Enter a string: ";
cin >> str;

while (str[length] != '\0') {


length++;
}

cout << "Length: " << length << endl;


return 0;
}

10. Convert decimal to octal and hexadecimal

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;

cout << "Octal: " << oct << num << endl;
cout << "Hexadecimal: " << hex << num << endl;
return 0;
}

11. Switch case example

#include <iostream>
using namespace std;

int main() {
int choice;
cout << "Enter choice (1-3): ";
cin >> choice;

switch (choice) {
case 1: cout << "Option 1 selected.\n"; break;
case 2: cout << "Option 2 selected.\n"; break;
case 3: cout << "Option 3 selected.\n"; break;
default: cout << "Invalid choice.\n";
}

return 0;
}

12. Prime numbers from 1 to 100

#include <iostream>
using namespace std;

bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n / 2; i++)
if (n % i == 0) return false;
return true;
}

int main() {
cout << "Prime numbers from 1 to 100:\n";
for (int i = 1; i <= 100; i++) {
if (isPrime(i))
cout << i << " ";
}
return 0;
}

13. Sum of individual digits

#include <iostream>
using namespace std;

int main() {
int num, sum = 0, digit;
cout << "Enter a number: ";
cin >> num;

while (num > 0) {


digit = num % 10;
sum += digit;
num /= 10;
}

cout << "Sum of digits: " << sum << endl;


return 0;
}

14. Check if a matrix is identity

#include <iostream>
using namespace std;

int main() {
int mat[3][3], flag = 1;
cout << "Enter 3x3 matrix:\n";
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> mat[i][j];

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


for (int j = 0; j < 3; j++) {
if ((i == j && mat[i][j] != 1) || (i != j && mat[i][j] != 0)) {
flag = 0;
break;
}
}

if (flag)
cout << "It is an identity matrix.\n";
else
cout << "Not an identity matrix.\n";

return 0;
}
15. Find number of days in a month

#include <iostream>
using namespace std;

int main() {
int month;
cout << "Enter month number (1-12): ";
cin >> month;

switch (month) {
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
cout << "31 days\n"; break;
case 4: case 6: case 9: case 11:
cout << "30 days\n"; break;
case 2:
cout << "28 or 29 days (leap year dependent)\n"; break;
default:
cout << "Invalid month\n";
}

return 0;
}

16. File handling in C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
string data;

// Write to file
ofstream outFile("sample.txt");
if (outFile.is_open()) {
cout << "Enter text to write into the file: ";
getline(cin, data);
outFile << data << endl;
outFile.close();
cout << "Data written to file.\n";
} else {
cout << "Unable to open file.\n";
}

// Read from file


ifstream inFile("sample.txt");
if (inFile.is_open()) {
cout << "Reading from file:\n";
while (getline(inFile, data)) {
cout << data << endl;
}
inFile.close();
} else {
cout << "Unable to read file.\n";
}

return 0;
}

You might also like