Sunday, March 29, 2020

CPP Basics Questions and answers


C++ Questions and Answers

Learn By Examples

Question 1
Write a C++ program to input three marks and print Total, Average, and Pass or Fail (average<40 is fail)

Answer
#include <iostream>
using namespace std; 

int main()
{
int m1, m2, m3,temp,total;
float average;
char grade;
cout << "Enter Marks 1?";
cin >> m1;
cout << "Enter Marks 2?";
cin >> m2;
cout << "Enter Marks 3?";
cin >> m3;
total = m1 + m2 + m3;
average = total / 3;
if (average > 40)
grade = 'P';
else
grade = 'F';
cout << "marks 1:" << m1 << "\tMarks 2:" << m2 << "\tMarks 3:" << m3<<endl;
cout<< "Total =" << total << "\tAverage=" << average << "\tGrade=" << grade<<endl;
return 0;
}

Question 2
Write a C++ program to input a radius of a Circle and print the area.



Answer 1
#include <iostream>
using namespace std; 

int main()
{
const float pi = 22.0 / 7.0;
float radious,area;
cout << "Enter a radious of a Circle?";
cin >> radious;
radious = float(radious);
area = pi * radious * radious;
cout << "Radious :" << radious<<endl;
cout << "Area :" << area << endl;;
return 0;
}

Answer 2
By Using OOP concepts.

#include <iostream>
using namespace std;

class MCircle
{
float  rr;

public:
//defualt constructor;
MCircle()
{
rr = 1.0;
}
//constructor with parameter
MCircle(int r)
{
rr = r;
}
MCircle(float r)
{
rr = r;
}


void setR(float r)
{
rr = r;
}

int gerR()
{
return rr;
}

double FindArea()
{
double area;
const float pi = 22.0 / 7.0;
area = pi * rr * rr;
return area;
}
};



int main()
{
int r;
MCircle c1, c2; //two different object
cout << "Enter radious for a circle" << endl;
cin >> r;
c1.setR(r);
cout << "Circle area=" << c1.FindArea() << endl;
cout <<"radious of c1:"<< c1.gerR() << endl;
cout << "Test constructor" << endl;
MCircle r2; //create object by calling default Constructor
cout <<"Area of the Circle"<< r2.FindArea() << endl;
}


Question  3
Write a C++ program to print prime numbers between 1 and 100.

Answer
#include <iostream>
using namespace std; 

int main()
{
int x, y;
bool p;
x = 2;
while (x <= 100)
{
p = true;
y = 2;
do
{
if (x % y == 0)
p = false;
y = y + 1;
} while (y < x);
if ((p == true) || (x==2))
cout << x<<endl;
x = x + 1;
}
return 0;
}

Question  4
Write a C++ program to input 3 numbers and print those numbers in an ascending order.

Answer 1
#include <iostream>
using namespace std; 

int main()
{
int n1, n2, n3,temp;
cout << "Enter first number?";
cin >> n1;
cout << "Enter second number?";
cin >> n2;
cout << "Enter third number?";
cin >> n3;
if (n1 > n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
if (n2 > n3)
{
temp = n2;
n2 = n3;
n3 = temp;
}
if (n1 > n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
cout << n1 << "\t"<<n2 << "\t" << n3;
return 0;
}

Answer 2
Using a Function with call by reference parameter

#include <iostream>
using namespace std;


void swap(int &n1, int &n2);
int main()
{
    int n1, n2, n3, temp;
    cout << "Enter first number?";
    cin >> n1;
    cout << "Enter second number?";
    cin >> n2;
    cout << "Enter third number?";
    cin >> n3;
    if (n1 > n2)
    {
        swap(n1, n2);
    }

    if (n2 > n3)
    {
        swap(n2, n3);
    }

    if (n1 > n2)
    {
        swap(n1, n2);
    } 
    cout << n1 << "\t" << n2 << "\t" << n3;
    return 0;
}
//Function for call by reference
void swap(int& n1, int& n2)
{
    int temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
}

Question  5

The class Employee has attributes employee number, employee basic salary. The constructor is used assign number and basic salary of an employee. The function display() displays number and name of the employee. The destructor is used to release the memory.  write an object oriented program in c++.

Answer


#include <iostream>
using namespace std;
//Pointers in C++

class Employee
{
string *num=new string();
string *name=new string();
float *salary=new float();
public:
Employee()
{
*num = "00000";
*name ="";
*salary = 0.0;
}
Employee(string num1, string name1, float sal)
{
*num = string(num1);
*name =string(name1);
*salary =sal;
}
void Display()
{
cout << "Number :" << *num << "\tName:" << *name << "\tSalary:" << *salary<<endl;
}

~Employee()
{
delete num;
delete name;
delete salary;

}

};

int main()
{
Employee e1("00001","Gannesh",7500.00);
e1.Display();
Employee e2("00002", "Nanthan", 17500.00);
e2.Display();
}

Question  6

A shop sells apples boxes for their customers. The Apple box has number of apples and all the apples in a particular box have the same price. Each apple box has different number of apples. Calculate() function calculates the total value of the apple box and Display() function display the details of the apple box.Write an object oriented program.


Answer

#include <iostream>
using namespace std;
class AppleBox
{
int num;
float price;
public:
AppleBox()
{
num = 0;
price = 0.0;
}
AppleBox(int n, float p)
{
num = n;
price = p;
}

double Calculate()
{
return num * price;
}
void Display()
{
cout << "Num of Apples:" << num << "\tPrice :" << price << "\tTotal value :" << Calculate()<<endl;
}
};
int main()
{
AppleBox a1(23,40.00);
a1.Display();
AppleBox a2(25, 60.50);
a2.Display();

}


Question  6

In a hotel, rooms are given for their customers. Every room has a room price for a day and number of days used. But luxury room has a service charge additionally and it is varied for each luxury room. you should identify classes, member of each class and relationship between classes.  (set price for a day, number of days used and service charge. Calculate and display the total charge )



Answer

#include <iostream>
using namespace std;
class Room
{
protected:
float roomPrice;
int dayUsed;
public:
void setRoomPrice(float r)
{
roomPrice=r;
}
void setDayUsed(int d)
{
dayUsed = d;
}
double totalCharged()
{
return roomPrice * dayUsed;
}
void Display()
{
cout << "Normal Room Days Used :" << dayUsed << "\tRate:" << roomPrice << "\tTotal Amt :"<<totalCharged() << endl;
}

};

class LuxuryRoom :public Room
{
float servicedCharge;
public:
void setServiced(float s)
{
servicedCharge = s;
}
double totalCharged()
{
return (dayUsed * roomPrice) + servicedCharge;
}
void Display()
{
cout << "Normal Room Days Used :" << dayUsed << "\tRate:" << roomPrice <<"\tService:"<<servicedCharge<< "\tTotal Amt :" << totalCharged() << endl;
}

};

int main()
{
Room r1;
r1.setDayUsed(5);
r1.setRoomPrice(1500.00);
r1.Display();
LuxuryRoom lr;
lr.setDayUsed(10);
lr.setRoomPrice(5000.00);
lr.setServiced(1200.00);
lr.Display();
}

No comments:

Post a Comment