C++_Practical_Questions_1_to_16
C++_Practical_Questions_1_to_16
#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;
}
#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;
}
#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) {}
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];
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];
return 0;
}
#include <iostream>
using namespace std;
int main() {
char str[100];
int length = 0;
cout << "Enter a string: ";
cin >> str;
#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;
}
#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;
}
#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;
}
#include <iostream>
using namespace std;
int main() {
int num, sum = 0, digit;
cout << "Enter a number: ";
cin >> num;
#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];
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;
}
#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";
}
return 0;
}