C
C
#include <iostream>
int main() {
int num1,num2,num3;
cin>>num1>>num2;
cout<<"num3=num1+num2: "<<(num3=num1+num2)<<endl;
cout<<"num3=num1-num2: "<<(num3=num1-num2)<<endl;
cout<<"num3=num1/num2: "<<(num3=num1/num2)<<endl;
cout<<endl;
cout<<"num3=num1: "<<(num3=num1)<<endl;
cout<<"num1+=num2: "<<(num1+=num2)<<endl;
cout<<"num1*=num2: "<<(num1*=num2)<<endl; 2. Write a C++ program to accept any number between 1-7 from
user and print respective day, i.e, 1- Monday 2- Tuesday etc.
cout<<"num1 &= num2: "<<(num1 &= num2)<<endl;
#include<iostream>
cout<<endl;
using namespace std;
cout<<"Relational Operators";
int main() {
cout << "num1 == num2 is " << (num1 == num2) << endl;
int day;
cout << "num1 > num2 is " << (num1 > num2) << endl;
cout<<"Enter a number between 1 and 7: ";
cout << "num1 >= num2 is " << (num1 >= num2) << endl;
cin>>day;
cout << "num1 < num2 is " << (num1 < num2) << endl;
switch(day) {
cout << "num1 <= num2 is " << (num1 <= num2) << endl;
case 1: cout<<"Monday"; break;
cout << "num1 != num2 is " << (num1 != num2) << endl;
case 2: cout<<"Tuesday"; break;
cout<<endl;
case 3: cout<<"Wednesday"; break;
cout<<"Logicnum2l Operators";
case 4: cout<<"Thursday"; break;
cout << "num1 && num2 is " << (num1 && num2) <<
endl; case 5: cout<<"Friday"; break;
cout << "num1 ! num2 is " << (num1 > num2) << endl; case 6: cout<<"Saturday"; break;
cout << "!num2 is " << (!num2) << endl; case 7: cout<<"Sunday"; break;
default: cout<<"Invalid input! Please enter number between #include<iostream>
1 and 7.";
using namespace std;
}
int main() {
return 0;
int i = 1;
}
cout << "This is a while loop:" << endl;
while(i <= 5) {
i++;
3. Write a C++ program to find the sum and average of the array
}
elements. Accept the array
cout << "\nThis is a for loop:" << endl;
elements from the user.
for(int j = 1; j <= 5; j++) {
#include<iostream>
cout << "j = " << j << endl;
using namespace std;
}
int main() {
return 0;
int n, i, sum = 0;
}
float avg;
cin>>n;
int arr[n];
cin>>arr[i];
sum += arr[i];
}
5. Write a C++ program to find a maximum of two numbers using
avg = float(sum)/n; a user defined function.
cout<<"Sum of array elements: "<<sum<<endl; #include<iostream>
cout<<"Average of array elements: "<<avg<<endl; using namespace std;
return 0; int max(int a, int b) {
} return (a > b) ? a : b;
int main() {
cin>>num1>>num2;
return 0;
4. Write a C++ program to demonstrate the while loop and for }
loop. Use suitable examples.
int main() {
Room room1;
return a + b + c;
float num3 = 2.5f, num4 = 3.5f, num5 = 4.5f; using namespace std;
length = 5;
breadth = 7;
}
7. Write a C++ program to find the area and volume of the room
using classes and objects. Rectangle(int l, int b) {
#include<iostream> length = l;
class Room { }
double breadth; }
double height; };
}; return 0;
} Department(string c, string d, string p, string dept, string hod)
: College(c, d, p) {
departmentName = dept;
hodName = hod;
9. Create a class named “College” with following attributes:
}
a. College name
void displayDepartmentDetails() {
b. Director name
displayCollegeDetails();
c. Principal name
cout << "Director Name: " << directorName << endl;
Create a derived class named “Department” which inherits
“College” with following attributes: cout << "Department Name: " << departmentName << endl;
a. Department name cout << "HOD Name: " << hodName << endl;
b. HOD name }
protected:
string directorName;
public:
string principalName;
10. Create a class named “College” with following attributes:
College(string c, string d, string p) {
a. College name
collegeName = c;
b. Director name
directorName = d;
c. Principal name
principalName = p;
Create a derived class named “Department” which inherits
}
“College” with following
void displayCollegeDetails() {
attributes:
cout << "College Name: " << collegeName << endl;
a. Department name
cout << "Principal Name: " << principalName << endl;
b. HOD name
}
Create a “Student” class and inherit “Department” with following
}; features.
#include<iostream>
using namespace std; a. College name
departmentName = d; #include<iostream>
}; class College {
} collegeName = c;
}; protected:
stud.display(); public:
hodName = hod;
}
11. Create a class named “College” with following attributes: };
class Student : public Department { Create a derived class named “Department” which inherits
“College” with following attributes:
private:
a. Department name
string studentName;
b. HOD name
string studentClass;
Create a “Student” class and inherit “Department” with following
int rollNo;
features.
public:
a. Student name
Student(string c, string d, string p, string dept, string hod, string
b. Student class
s, string cls, int r) : Department(c, d, p, dept, hod) {
c. Student roll no.
studentName = s;
Create a class named “Library” with following features.
studentClass = cls;
a. Library_card_no
rollNo = r;
b. No_of_books_issued
}
Make the “Student” class inherit “Department” and “Library”.
void display() {
Create objects of “Student” class and demonstrate Multiple
cout<<"College Name: "<<collegeName<<endl; inheritance.
} public:
protected:
string departmentName;
string hodName;
public:
protected: return 0;
int libraryCardNo; }
int noOfBooksIssued;
public:
libraryCardNo = cardNo;
noOfBooksIssued = booksIssued;
};
13. Create a base class called shape to store two double type
class Student : public Department, public Library { values that could be used to compute the area of rectangular
private: and/or triangle. Two separate classes are derived from the base
class for the same. The input for the computation and the output
string studentName; is to be displayed in the base class. Make the function to display
as a virtual function and redefine this function in the derived class
string studentClass;
to suit their requirement.
int rollNo;
#include<iostream>
public:
using namespace std;
Student(string c, string d, string p, string dept, string hod, int
class Shape {
cardNo, int booksIssued, string s, string cls, int r) : Department(c,
d, p, dept, hod), Library(cardNo, booksIssued) { protected:
studentName = s; double a, b;
studentClass = cls; public:
rollNo = r; void getData(double x, double y) {
} a = x; b = y;
void display() { }
cout<<"College Name: "<<collegeName<<endl; virtual void displayArea() { }
cout<<"Director Name: "<<directorName<<endl; };
cout<<"Principal Name: "<<principalName<<endl; class Rectangle : public Shape {
cout<<"Department Name: "<<departmentName<<endl; public:
cout<<"HOD Name: "<<hodName<<endl; void displayArea() {
cout<<"Library Card No.: "<<libraryCardNo<<endl; cout << "Area of Rectangle is " << a * b << endl;
cout<<"No. of Books Issued: "<<noOfBooksIssued<<endl; }
cout<<"Student Name: "<<studentName<<endl; };
cout<<"Student Class: "<<studentClass<<endl; class Triangle : public Shape {
cout<<"Roll No.: "<<rollNo<<endl; public:
} void displayArea() {
}; cout << "Area of Triangle is " << 0.5 * a * b << endl;
int main() { }
Student stud("ABC College", "Mr. Suresh", "Mr. ramesh", };
"EXTC", "Mr. Mukesh", 12345, 2, "Anuj", "12th", 25);
int main() {
Rectangle rect;
Triangle tri;
rect.getData(5.0, 3.0);
tri.getData(5.0, 3.0);
rect.displayArea();
tri.displayArea();
return 0;