1
1
1
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c,r1,r2,d;
cout<<"Input values of a, b and c:";
cin>>a>>b>>c;
d=pow(b,2)-(4*a*c);
if(d<0)
cout<<"No real roots";
else
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Write a C++ program to print even and odd elements row wise and column wise separately in a
5*3 matrix.
*/
#include<iostream>
using namespace std;
int main()
{
int m[5][3];
int e,o;
for(int i=0;i<5;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Enter value for ["<<i<<","<<j<<"]: ";
cin>>m[i][j];
}
}
for(int i=0;i<5;i++)
{
cout<<"Row "<<i+1<<":"<<endl;
e=0,o=0;
for(int j=0;j<3;j++)
{
if(m[i][j]%2==0)
e++;
else
o++;
}
cout<<"Even: "<<e<<" Odd: "<<o<<endl;
}
for(int j=0;j<3;j++)
{
cout<<"Column "<<j+1<<":"<<endl;
e=0,o=0;
for(int i=0;i<5;i++)
{
if(m[i][j]%2==0)
e++;
else
o++;
}
cout<<"Even: "<<e<<" Odd: "<<o<<endl;
}
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
An electricity board charges the following rates to domestic users to discourage large
consumption of energy For the first 100 units:- 60 P per unit For
the next 200 units:-80 P per unit Beyond
300 units:-90 P per unit.
All users are charged a minimum of Rs 50 if the total amount is more
than Rs 300 then an additional surcharge of 15% is added. WAP to
read the names of users and number of units consumed and display
the charges with names.
*/
#include<iostream>
using namespace std;
int main()
{
int u,b,s;
string n;
if(u<=100)
{
b=((60*u)/100)+50;
}
else if(u>300)
{
b=((600+16000+((u-300)*90))/100)+50;
if(b>300)
{
s=0.15*b;
b+=s;
}
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
W.A.P in C++by defining a class to represent a bank account. Include the following-
Data Members
Name of the depositor
Account number
Type of account (Saving, Current etc.)
Balance amount in the account
Member Functions
To assign initial values
Todeposit an amount
To withdraw an amount after checking the balance
To display name and balance
*/
#include<iostream>
#include<string>
using namespace std;
class bankAccount{
string name;
int acc;
string type;
double bal;
public:
void assign();
void deposit();
void withdraw();
void disp();
};
int main()
{
bankAccount a;
a.assign();
a.deposit();
a.withdraw();
a.disp();
return 0;
}
void bankAccount::assign()
{
cout<<"Enter name:"<<endl;
getline(cin,name);
cout<<"Type of account:"<<endl;
getline(cin,type);
cout<<"Enter account no.:"<<endl;
cin>>acc;
cout<<"Enter account balance:"<<endl;
cin>>bal;
}
void bankAccount::deposit()
{
double d;
cout<<"Enter amount to deposit:"<<endl;
cin>>d;
bal+=d;
}
void bankAccount::withdraw()
{
double w;
cout<<"Enter amount to wihdraw:"<<endl;
cin>>w;
if(bal>=w)
bal-=w;
else
cout<<"Low Balance\n";
}
void bankAccount::disp()
{
cout<<"\nName: "<<name<<"\nAccount no.: "<<acc<<"\nType: "<<type<<"\nBalance:
"<<bal;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Write a program to create a class employee having name , emp id, dept name, salary .
Where components of salary are as follows :
Basic salary
HRA=15% of basic salary
DA=10% of basic salary
TA= 5% of basic salary
TS = Misc.
Display all the relevant details of an emp along with total salary content.
*/
#include<iostream>
#include<string>
using namespace std;
class Employee{
string name,id,dep;
double basic,hra,da,ta,tot;
public:
void setData();
void disp();
friend void total(Employee[],int);
friend void idSearch(Employee[],int);
};
int main()
{
int n;
cout<<"Enter no. of employees: ";
cin>>n;
Employee emp[n];
cout<<"Input data:"<<endl;
for(int i=0;i<n;i++)
{
emp[i].setData();
}
for(int i=0;i<n;i++)
{
emp[i].disp();
}
total(emp,n);
idSearch(emp,n);
return 0;
}
void Employee::setData()
{
getline(cin,name);
cout<<"Enter employee name:"<<endl;
getline(cin,name);
cout<<"Enter department:"<<endl;
getline(cin,dep);
cout<<"Enter ID:"<<endl;
getline(cin,id);
cout<<"Enter basic salary:"<<endl;
cin>>basic;
hra=0.15*basic;
da=0.1*basic;
ta=0.05*basic;
tot=basic+hra+da+ta;
}
void Employee::disp()
{
cout<<"Name: "<<name<<"\nDepartment: "<<dep<<"\nID: "<<id<<"\nTotal Salary:
"<<tot<<endl;
}
class invoice{
string partNumber,partDescription;
int quant;
double price;
public:
invoice(string pn,string pd,int q=0, double p=0.0)
{
setPartNumber(pn);
setPartDescription(pd);
setQuantity(q);
setPrice(p);
}
double getInvoiceAmount()
{
return quant*price;
}
};
void invoiceTest();
int main()
{
invoiceTest();
return 0;
}
void invoiceTest()
{
string a,b;
int c;
double d;
cout<<"Input Part Number: ";
cin>>a;
cout<<"Input Part Description: ";
cin>>b;
cout<<"Input quantity: ";
cin>>c;
cout<<"Input price: ";
cin>>d;
if(c<0)
c=0;
if(d<0.0)
d=0.0;
invoice ob(a,b,c,d);
double x=ob.getInvoiceAmount();
cout<<"Invoice amount: "<<x<<endl;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Imagine a tollbooth with a class called TollBooth. The two data items
are of type unsigned int and double to hold the total number of cars
and total amount of money collected. A constructor initializes both
of these data members to 0. A member function called payingCar()increments the car total and
adds 0.5 to the cash total. Another
function called nonPay Car() increments the car total but adds
nothing to the cash total. Finally a member function called display()
shows the two totals. Include a program to test this class. This
program should allow the user to push one key to count a paying car
, and another to count a non paying car. Pushing the ESC key should
cause the program to print out the total number of cars and total cash
and then exit.
*/
#include<iostream>
using namespace std;
class Toolbooth
{
int car;
double money;
public:
Toolbooth()
{
car=0;
money=0;
}
void paying_car()
{
car++;
money=money+0.5;
}
void non_paying_car()
{
car++;
}
void display()
{
cout<<"Total Number of cars : "<<car<<endl;
cout<<"Money : "<<money<<endl;
cout<<" "<<endl;
}
};
int main()
{
Toolbooth b1;
int ch;
do
{
cout<<"Enter 1 - Paying car : \nEnter 2 - Non Paying Car : \nEnter 3 -Display : \nEnter 0 -
Exit : "<<endl;
cin>>ch;
switch(ch)
{
case 1:
b1.paying_car();
break;
case 2:
b1.non_paying_car();
break;
case 3:
b1.display();
break;
}
}while(ch!=0);
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Create a class called Time that has separate int member data for hours, minutes and seconds. One
constructor should initialize this
data to 0, and another should initialize it to fixed values. A member
function should display it in 11:59:59 format. A member function named add) should add two
objects of type time passed as arguments. A main () program should create two initialized values
together, leaving the result in the third time variable. Finally it should display the value of this
third variable.
*/
#include<iostream>
using namespace std;
class Time{
int hours,minutes,seconds;
public:
Time(){
hours=minutes=seconds=0;
}
Time(int x,int y, int z,int i){
hours=x;
minutes=y;
seconds=z;
cout<<"\nInputed time "<<i<<": "<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
Time add(Time &o,Time &t){
Time temp;
temp.seconds=t.seconds+o.seconds;
if(temp.seconds>=60){
temp.minutes=t.minutes+o.minutes+temp.seconds/60;
temp.seconds%=60;
}
if(temp.minutes>=60){
temp.hours=t.hours+o.hours+temp.minutes/60;
temp.minutes%=60;
}
return temp;
}
void display(){
cout<<"\nFinal time after addition:";
cout<<hours<<":"<<minutes<<":"<<seconds<<"\n\n";
}
};
int main(){
Time t1(5,31,40,1);
Time t2(6,35,30,2);
Time t3;
t3=t3.add(t1,t2);
t3.display();
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Create a class called Student that contains the data members like age ,name, enroll no, marks.
Create another class called Faculty that contains data members like faculty Name, faculty Code,
salary,dept, age, experience, gender. Create the function display) in both the classes to display
the respective infomation. The derived Class Person demonstrates multiple inheritance. The
program should be able to call both the base classes and displays their information. Remove the
ambiguity (When we have exactly same variables or same methods in both the base classes,
which one will be called?) by proper mechanism.
*/
#include<iostream>
using namespace std;
class Student{
protected:
int age,enroll_no,m[5];
string name;
void getInfo(){
cout<<"\n---------Enter student details:---------\n";
cout<<"\nEnter name:";
getline(cin,name);
cout<<"\nEnter age:";
cin>>age;
cout<<"\nEnter enrollment no:";
cin>>enroll_no;
cout<<"\nEnter marks in 5 subjects:";
for(int i=0;i<5;i++)
cin>>m[i];
}
void display(){
cout<<"\n---------Student Details are:---------\n";
cout<<"\nName:"<<name;
cout<<"\nAge:"<<age<<"\nEnrollment No:"<<enroll_no;
cout<<"\nTotal marks:"<<(m[0]+m[1]+m[2]+m[3]+m[4]);
}
};
class Faculty{
protected:
string facultyName,deptt;
int facultyCode,age,experience;
char gender;
float salary;
void getInfo(){
cout<<"\n---------Enter Faculty details:---------\n";
cout<<"\nEnter faculty name:";
cin.ignore();
getline(cin,facultyName);
cout<<"\nEnter department:";
cin.ignore();
cin>>deptt;
cout<<"\nEnter faculty code:";
cin>>facultyCode;
cout<<"\nEnter age:";
cin>>age;
cout<<"\nEnter year of experience:";
cin>>experience;
cout<<"\nEnter gender(M|F):";
cin>>gender;
cout<<"\nEnter salary in Rs:";
cin>>salary;
}
void display(){
cout<<"\n---------Faculty Details are:---------\n";
cout<<"\nName:"<<facultyName<<"\nFaculty Code:"<<facultyCode<<"\nAge:"<<age;
cout<<"\nGender:"<<gender<<"\nSalary:Rs"<<salary<<"\nDepartment:"<<deptt;
cout<<"\nYear of experience:"<<experience<<"\n\n";
}
};
class Person:protected Student,public Faculty{
public:
void getInfo(){
Student::getInfo();
Faculty::getInfo();
}
void display(){
Student::display();
Faculty::display();
}
};
int main(){
Person p;
p.getInfo();
p.display();
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Implement a c++ program to create a class called ConsDemo and overload SumDemo ()
function. SumDemo (int, char)→If passing character is ‘p’ then print square of passing number
otherwise cube of a number SumDemo (int, int, char)→If passing character is ‘a’ then print
addition of numbers otherwise print Ascii value of a passing character. SumDemo (string,
string)->Check whether passing strings are equal or not
*/
#include<iostream>
using namespace std;
class ConsDemo{
int n1,n2;
string str1,str2;
public:
void SumDemo(int a,char x){
n1=a;
if(x=='p')
cout<<"Square of "<<n1<<" is:"<<n1*n1;
else
cout<<"Cube of "<<n1<<" is:"<<n1*n1*n1;
}
void SumDemo(int a,int b,char x){
n1=a;
n2=b;
if(x=='a')
cout<<"\n\nSum of nos "<<n1<<" and "<<n2<<" is:"<<n1+n2;
else
cout<<"\n\nASCII value of "<<x<<" is:"<<(int)x;
}
void SumDemo(string s1,string s2){
str1=s1;
str2=s2;
if(str1==str2)
cout<<"\n\nTwo stings are same.";
else
cout<<"\n\nTwo strings are not same.\n\n";
}
};
int main(){
ConsDemo ob;
ob.SumDemo(10,'p');
ob.SumDemo(10,15,'r');
ob.SumDemo("Hello","Hello World");
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Create a Base class that consists of private, protected and public data members and member
functions.
Try using different access modifiers for inheriting Base class to the Derived class and create a
table that summarizes the above three modes (when derived in public, protected and private
modes) and shows the access specifier of the members of base class in the Derived class
Inheritance, Access Specifiers
*/
#include<iostream>
using namespace std;
class UserTwo;
class UserOne{
protected:
string Name,FatherName,MotherName;
char Gender;
public:
void InputInfo(int x){
cout<<"\n\nEnter Person "<<x<<" Details:\n";
cout<<"Enter Name:";
getline(cin,Name);
cout<<"Enter Gender(M|F):";
cin>>Gender;
cout<<"Enter Father Name:";
cin.ignore();
getline(cin,FatherName);
cout<<"Enter Mother Name:";
cin.ignore();
getline(cin,MotherName);
}
friend void UserChecker(UserOne &,UserTwo &);
};
class UserTwo:public UserOne{
friend void UserChecker(UserOne &,UserTwo &);
};
void UserChecker(UserOne &u1,UserTwo &u2){
if(u1.FatherName==u2.FatherName && u1.MotherName==u2.MotherName){
cout<<"We belongs to same family.\n";
if(u1.Gender=='M' && u2.Gender=='M'){
cout<<"\nWe are brothers.\n";
}
else if(u1.Gender=='F' && u2.Gender=='F'){
cout<<"\nWe are sisters.\n";
}
else{
cout<<"\nWe are brother and sister.\n";
}
}
else{
cout<<"We belonmgs to different family.\n";
}
}
int main(){
UserOne U1;
UserTwo U2;
U1.InputInfo(1);
U2.InputInfo(2);
UserChecker(U1,U2);
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Wap a program in C++ to find a given number (we are given a list of n-1 integer and these
integer are in the range of 1 to n .There are no duplicates in the list. One of the integer is missing
in the list find it.
*/
#include<iostream>
using namespace std;
int main()
{
int n,i,c=0,j;
cout<<"Enter n : ";
cin>>n;
int a[n-1];
cout<<"Enter numbers : "<<endl;
for(i=0; i<n; i++)
cin>>a[i];
for(i=1; i<=n; i++)
{
for(j=0; j<n-1; j++)
{
if(i==a[j])
{
c=1;
break;
}
}
if(c!=1)
{
cout<<"Missing Number is "<<i<<endl;
break;
}
c=0;
}
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Using the concept of operator overloading. Write a program to overload using with and without
friend Function.
a. Unary
b. Unary++ pre increment, post increment
C. Unary pre decrement, post decrement
*/
#include<iostream>
using namespace std;
class overload
{
int a;
public :
overload(int x)
{
a=x;
}
void display()
{
cout<<"\nNumber:- ";
cout<<a;
}
void operator-()
{
cout<<"\nNumber after - :- ";
cout<<-a;
}
void operator++()
{
int x=a;
cout<<"\nNumber after pre-increment :- ";
cout<<++x;
}
void operator++(int)
{
int x=a;
cout<<"\nNumber after post-increment :- ";
cout<<x++;
}
void operator--()
{
int x=a;
cout<<"\nNumber after pre-decrement :- ";
cout<<--x;
}
void operator--(int)
{
int x=a;
cout<<"\nNumber after post-decrement :- ";
cout<<x--;
}
/*void operator-(overload x)
{
cout<<"\nNumber after - :- ";
cout<<-(x.a);
}
void operator++(overload x)
{
int t=x.a;
cout<<"\nNumber after pre-increment :- ";
cout<<++t;
}
void operator--(overload x)
{
int t=x.a;
cout<<"\nNumber after pre-decrement :- ";
cout<<--t;
}
void operator--(overload x,int)
{
int t=x.a;
cout<<"\nNumber after post-decrement :- ";
cout<<t--;
}*/
int main()
{
int n;
cout<<"ENTER THE NUMBER :- ";
cin>>n;
overload c1(n);
c1.display();
-c1;
++c1;
c1++;
--c1;
c1--;
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
Create a class Complex having two int type variable named real & |
img denoting real and imaginary part respectively of a complex
number. Overload +, - operator to add, to subtract and to
compare two complex numbers being denoted by the two complex
type objects.
*/
#include <iostream>
using namespace std;
class complex1{
private:
int real,img;
public:
void setData(){
cout<<"Enter real part : ";
cin>>real;
cout<<"Enter imaginary part : ";
cin>>img;
}
complex1 operator+(complex1 &o){
complex1 temp;
temp.real=real+o.real;
temp.img=img+o.img;
return temp;
}
complex1 operator-(complex1 &o){
complex1 temp;
if(real>o.real)
{
temp.real=real-o.real;
temp.img=img-o.img;
}
else
{
temp.real=o.real-real;
temp.img=o.img-img;
}
return temp;
}
int operator==(complex1 &o)
{
if(real==o.real && img==o.img)
return 1;
else
return 0;;
}
void show()
{
if(img>=0)
cout<<real<<" +i"<<img<<endl;
else
cout<<real<<" -i"<<abs(img)<<endl;
}
};
int main()
{
complex1 ob1,ob2,ob3;
ob1.setData();
ob2.setData();
ob3=ob1+ob2;
cout<<"Addition : ";
ob3.show();
ob3=ob1-ob2;
cout<<"Subtraction : ";
ob3.show();
if(ob1==ob2)
cout<<"Equal"<<endl;
else
cout<<"Not equal"<<endl;
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
W.A.P in C++ to show the working of function overloading by using function named
calculate(Area) to calculate area of square rectangle and triangle using different signatures as
required.
*/
#include<iostream>
#include<math.h>
using namespace std;
class overload
{
public:
int area(int s)
{
return s*s;
}
int area(int l,int b)
{
return l*b;
}
double area(double r)
{
return (3.14*r*r);
}
double area(int a,int b,int c)
{
double s=(a+b+c)/2.0;
double area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
};
int main()
{
overload obj;
int ch,a,b,c;
double x;
cout<<"Enter your choice as:- \n";
cout<<"1- To find area of square\n";
cout<<"2- To find area of rectangle \n";
cout<<"3- To find area of circle\n";
cout<<"4- To find area of triangle\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the side of square:- ";
cin>>a;
cout<<"Area of square is:- "<<obj.area(a);
break;
case 2:
cout<<"Enter the sides of rectangle:- ";
cin>>a>>b;
cout<<"Area of rectangle is:- "<<obj.area(a,b);
break;
case 3:
cout<<"Enter the radius of circle:- ";
cin>>x;
cout<<"Area of circle is:- "<<obj.area(x);
break;
case 4:
cout<<"Enter the sides of triangle:- ";
cin>>a>>b>>c;
cout<<"Area of triangle is:- "<<obj.area(x);
break;
default:
cout<<"Wrong choice";
break;
}
return 0;
}
/*
NAME: Ankit Raj’
SECTION:F
ROLL NO:12
C++ program to implement different methods of List, Vector and Map in STL (Standard
Template Library)
*/
#include<iostream>
#include<list>
#include<vector>
#include<map>
int main()
{
vector<int> demo1={1,2,3,4,5};
list<float> demo2{6.6,7.7,8.8,9.9,10.0};
map<int,string> demo3{ {1,"DIVYANSHU"},{2,"NEGI"} };
cout<<"Displaying vector:"<<endl;
for(int i:demo1)
{
cout<<i<<endl;
}
cout<<"Displaying list:"<<endl;
for(float i:demo2)
{
cout<<i<<endl;
}
cout<<"Displaying map:"<<endl;
for(auto i:demo3)
{
cout<<i.first<<":"<<i.second<<endl;
}
return 0;
}