Q7: Fibonacci using Constructor
#include <iostream>
using namespace std;
class Fibonacci {
int n;
public:
Fibonacci(int x) {
n = x;
int a = 0, b = 1, c;
cout << "Fibonacci Series: " << a << " " << b << " ";
for (int i = 2; i < n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
}
};
int main() {
int num;
cout << "Enter number of terms: ";
cin >> num;
Fibonacci f(num);
return 0;
}
Q8: Single Inheritance - Area of Circle
#include <iostream>
using namespace std;
class Circle {
protected:
float radius;
public:
void getRadius() {
cout << "Enter radius: ";
cin >> radius;
}
};
class Area : public Circle {
public:
void computeArea() {
float area = 3.1416 * radius * radius;
cout << "Area of Circle = " << area << endl;
}
};
int main() {
Area a;
a.getRadius();
a.computeArea();
return 0;
}
Q9: Multiple Inheritance - Percentage
#include <iostream>
using namespace std;
class Student {
public:
int marks1, marks2;
void getMarks() {
cout << "Enter marks of two subjects: ";
cin >> marks1 >> marks2;
}
};
class Sports {
public:
int sportsMarks;
void getSportsMarks() {
cout << "Enter sports marks: ";
cin >> sportsMarks;
}
};
class Result : public Student, public Sports {
public:
void display() {
int total = marks1 + marks2 + sportsMarks;
float percent = (total / 300.0) * 100;
cout << "Total Marks = " << total << "\nPercentage = " << percent << "%" << endl;
}
};
int main() {
Result r;
r.getMarks();
r.getSportsMarks();
r.display();
return 0;
}
Q10: Hierarchical Inheritance
#include <iostream>
using namespace std;
class Number {
protected:
int num;
public:
void getNumber() {
cout << "Enter a number: ";
cin >> num;
}
};
class Square : public Number {
public:
void showSquare() {
cout << "Square = " << num * num << endl;
}
};
class Cube : public Number {
public:
void showCube() {
cout << "Cube = " << num * num * num << endl;
}
};
int main() {
Square s;
Cube c;
s.getNumber();
s.showSquare();
c.getNumber();
c.showCube();
return 0;
}
Q11: Constructors (Default, Parameterized, Copy)
#include <iostream>
using namespace std;
class Demo {
int a;
public:
Demo() {
a = 10;
cout << "Default Constructor: a = " << a << endl;
}
Demo(int x) {
a = x;
cout << "Parameterized Constructor: a = " << a << endl;
}
Demo(Demo &d) {
a = d.a;
cout << "Copy Constructor: a = " << a << endl;
}
};
int main() {
Demo d1;
Demo d2(20);
Demo d3(d2);
return 0;
}
Q12: Count Characters in File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("example.txt");
char ch;
int count = 0;
if (!file) {
cout << "File not found!" << endl;
return 1;
}
while (file.get(ch)) {
count++;
}
file.close();
cout << "Total characters in file: " << count << endl;
return 0;
}
Q13: Decimal to Binary
#include <iostream>
using namespace std;
int main() {
int decimal, binary[32], i = 0;
cout << "Enter a decimal number: ";
cin >> decimal;
while (decimal > 0) {
binary[i++] = decimal % 2;
decimal = decimal / 2;
}
cout << "Binary: ";
for (int j = i - 1; j >= 0; j--)
cout << binary[j];
cout << endl;
return 0;
}
Q14: String Class with == Operator
#include <iostream>
#include <cstring>
using namespace std;
class String {
char str[100];
public:
void getString() {
cout << "Enter string: ";
cin >> str;
}
bool operator==(String s) {
return strcmp(str, s.str) == 0;
}
};
int main() {
String s1, s2;
s1.getString();
s2.getString();
if (s1 == s2)
cout << "Strings are equal." << endl;
else
cout << "Strings are not equal." << endl;
return 0;
}