0% found this document useful (0 votes)
24 views17 pages

Slips

Uploaded by

sujalvora73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

Slips

Uploaded by

sujalvora73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Slip 1

q1]
#include<iostream>
using namespace std;
inline int Max(int a,int b)
{
return (a>b)?a:b;
}

inline int Min(int a,int b)


{
return (a<b)?a:b;
}

int main()
{
int n1,n2;
cout<<"Enter two numbers: ";
cin>>n1>>n2;
int max=Max(n1,n2);
int min=Min(n1,n2);
cout<<"Maximum:"<<max<<endl;
cout<<"Minimum:"<<min<<endl;
return 0;
}

Slip 2
q1]
#include<iostream>
using namespace std;
float cylinder(int radius,int height);
float sphere(int radius);
float cone(int radius,int height);

int main()
{
float radius,height;
cout<<"\n Enter Height And Radius of a cylinder:";
cin>>radius>>height;
cout<<"\n Enter Radius Of Sphere:";
cin>>radius;
cout<<"\n Enter Radius And Height of a cone:";
cin>>radius>>height;
cout<<"Volume of a cylinder is:"<<cylinder(radius,height)<<endl;
cout<<"Volume of a Sphere is:"<<sphere(radius)<<endl;
cout<<"Volume of a Cone is:"<<cone(radius,height)<<endl;
return 0;
}

float cylinder(int radius,int height)


{
return(3.14*radius*radius*height);
}

float sphere(int radius)


{
return((4*3.14*radius*radius*radius)/3);
}
float cone(int radius,int height)
{
return(1/3*3.14*radius*radius*height);
}

Slip 3

q1]
#include <iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int n1,n2;
cout<<"Enter the first number: ";
cin>>n1;
cout<<"Enter the second number: ";
cin>>n2;
cout<<"Before swapping:<<endl;
cout<<"Number1="<<n1<<endl;
cout<<"Number2="<<n2<<endl;
swap(n1,n2);
cout<<"After swapping:<<endl;
cout<<"Number1="<<n1<<endl;
cout<<"Number2="<<n2<<endl;
return 0;
}

Slip 4
q1]
#include<iostream>
using namespace std;
class Worker
{
char name[10];
int hr;
public:
void accept()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter hours:";
cin>>hr;
}
void calculate(int rate=50)
{
cout<<"Salary of worker is Rs."<<(hr*rate)<<endl;
}
};

int main()
{
Worker ob;
ob.accept();
ob.calculate();
return 0;
}

Slip 6
q1]

#include<iostream.h>
class square
{
private:
int s;
public:
void get()
{
cout<<"Enter Side Of The Square:";
cin>>s;
}
int cal()
{
return s*s;
}
void disp()
{
cout<<"Sqaure Area:"<<cal()<<endl;
}
void compare(int s,int r);
};
class rectangle
{
private:
int l,w;
public:
void get1()
{
cout<<"Enter Length and Width Of The Rectangle:";
cin>>l>>w;
}
int cal1()
{
return l*w;
}
void disp1()
{
cout<<"Area Of Rectangle:"<<cal1()<<endl;
}
void compare(int s,int r);
};
void compare(int s,int r)
{
if(s>r)
{
cout<<"Square Area Is Greater Than Rectangle Area.";
}
else
{
cout<<"Sqaure Area Is Less Than Rectangle Area.";
}
}
int main()
{
int s_area,r_area;
square s1;
rectangle r1;
s1.get();
s_area=s1.cal();
s1.disp();
r1.get1();
r_area=r1.cal1();
r1.disp1();
compare(s_area,r_area);
return 0;
}
Slip-7

q1]

#include <iostream>
#include <cstring>
using namespace std;

class mstring
{
private:
char str[50];
public:
mstring(const char *c)
{
strncpy(str,c,sizeof(str));
str[sizeof(str)-1]='\0';
}
int replace(char ch1,char ch2='*')
{
int c=0;
for (int i=0;str[i]!='\0';i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
c++;
}
}
return c;
}
void display()
{
cout<<"Updated String:"<<str<<endl;
}
};

int main()
{
char str[50];
cout<<"Enter a string: ";
cin.getline(str,50);
mstring s(str);
char ch1;
cout << "Enter character to replace: ";
cin >> ch1;
int r=s.replace(ch1);
s.display();
return 0;
}

Slip-9

q1]
#include<iostream>
using namespace std;
class person
{
public:
char Name[50];
char Address[50];
float salary;
float tax_amt;
void get()
{
cout<<"Enter name:";
cin>>Name;
cout<<"Enter address:";
cin>>Address;
cout<<"Enter salary:";
cin>>salary;
}
void tax()
{
if(salary<=20000)
tax_amt=0;
else if(salary<=40000)
tax_amt=0.05*salary;
else
tax_amt=0.1*salary;
}
void Display()
{
cout<<"Name:"<<Name<<endl;
cout<<"Address:"<<Address<<endl;
cout<<"Salary:"<<salary<<endl;
cout<<"Tax Amount:"<<tax_amt<<endl;
}
};
int main()
{
person p;
p.get();
p.tax();
p.Display();
return 0;
}

Slip-10
q1]
#include<iostream>
using namespace std;
class Account
{
private:
int Acc_number;
char Acc_type;
float Balance;
public:
void Accept()
{
cout<<"Enter account number: ";
cin>>Acc_number;
cout<<"Enter account type(S for Savings,C for Current):";
cin>>Acc_type;
cout<<"Enter balance: ";
cin>>Balance;
}
void Display()
{
cout<<"Account Number:"<<Acc_number<<endl;
cout<<"Account Type:"<<Acc_type<<endl;
cout<<"Balance:"<<Balance<<endl;
}
};
int main()
{
int n;
cout<<"Enter the number of accounts: ";
cin>>n;
Account *accounts=new Account[n];
for(int i=0;i<n;i++)
{
cout<<"\n Enter details for Account:"<<i+1<<endl;
accounts[i].Accept();
}
cout<<"\n Account Details:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"\n Account:"<<i+1<<endl;
accounts[i].Display();
}
delete[] accounts;
return 0;
}

Slip-11

q1]

#include <iostream>
using namespace std;
class MyArray
{
private:
int *arr;
int size;
public:
MyArray(int s):size(s)
{
arr=new int[size];
}
~MyArray()
{
delete[] arr;
}
void accept()
{
cout<<"Enter elements:"<<endl;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
}
void sum()
{
int sum=0;
for(int i=0;i<size;i++)
{
sum=sum+arr[i];
}
cout<<"Sum of array elements:"<<sum<<endl;
}
};

int main()
{
int size;
cout<<"Enter the size of the array: ";
cin>>size;
MyArray m(size);
m.accept();
m.sum();
return 0;
}

Slip-12

q1]

#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int day;
int month;
int year;
public:
Date():day(1),month(1),year(2000) {}
Date(int d,int m,int y):day(d),month(m),year(y){}
void Display()
{
cout<<day<<"-"<<Get(month)<<"-"<<year<<endl;
}
private:
string Get(int m)
{
switch(m)
{
case 1:return "Jan";
case 2:return "Feb";
case 3:return "Mar";
case 4:return "Apr";
case 5:return "May";
case 6:return "Jun";
case 7:return "Jul";
case 8:return "Aug";
case 9:return "Sep";
case 10:return "Oct";
case 11:return "Nov";
case 12:return "Dec";
}
}
};
int main()
{
Date d(4,1,2021);
cout<"Input Date:";
d.Display();
return 0;
}

Slip-13

q1]
#include<iostream>
#include<string>
using namespace std;
class Product
{
private:
int product_id;
string product_name;
int qty;
float price;
static int object_count;
public:
Product():product_id(0),qty(0),price(0.0)
{
object_count++;
}
Product(int id,string name,int q,float p)
:product_id(id),product_name(name),qty(q),price(p)
{
object_count++;
}
~Product()
{
object_count--;
}
void Accept()
{
cout<<"Enter product ID:";
cin>>product_id;
cin.ignore();
cout<<"Enter product name:";
getline(cin,product_name);
cout<<"Enter quantity:";
cin>>qty;
cout<<"Enter price: ";
cin>>price;
}
void Display()
{
cout<<"Product ID:"<<product_id<<endl;
cout<<"Product Name:"<<product_name<<endl;
cout<<"Quantity:"<<qty<<endl;
cout<<"Price:"<<price <<endl;
}
static int Get()
{
return object_count;
}
};
int Product::object_count=0;
int main()
{
int num;
cout<<"Enter the number of products: ";
cin>>num;
cin.ignore();
Product *products=new Product[num];
for(int i=0;i<num;i++)
{
cout<<"\n Enter details for Product"<<i+1<<":"<<endl;
products[i].Accept();
}
cout<<"\n Product Details:\n";
for(int i=0;i<num;i++)
{
cout<<"\n Product"<<i+1<<endl;
products[i].Display();
}
cout<<"\n Number of objects created for Product class:"<<Product::Get()<<endl;
delete[] products;
return 0;
}

Slip-14
q1]

#include <iostream>
using namespace std;
inline float Diameter(float radius)
{
return 2*radius;
}
inline float Circumference(float radius)
{
return 2*3.14*radius;
}
inline float Area(float radius)
{
return 3.14*radius*radius;
}
int main()
{
float radius;
cout<<"Enter the radius of the circle: ";
cin>>radius;
cout<<"Diameter:"<<Diameter(radius)<<endl;
cout<<"Circumference:"<<Circumference(radius)<<endl;
cout<<"Area:"<<Area(radius)<<endl;
return 0;
}

Slip-16
q1]
#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std;
class Machine
{
public:
int Machine_id;
char Machine_Name[50];
int Price;
Machine(int id,const char *name,int p)
{
Machine_id=id;
strcpy(Machine_Name,name);
Price=p;
}
void disp()
{
cout<<setw(3)<<"Machine ID:"<<setw(3)<<Machine_id<<endl;
cout<<setw(3)<<"Machine Name:"<<setw(3)<<Machine_Name<<endl;
cout<<setw(3)<<"Price:"<<setprecision(2)<<fixed<<Price<<endl;
}
};
int main()
{
int Price,Machine_id;
char Machine_Name[50];
cout<<"Machine ID:";
cin>>Machine_id;
cout<<"Machine Name:";
cin>>Machine_Name;
cout<<"Price:";
cin>>Price;
Machine m1(Machine_id,Machine_Name,Price);
m1.disp();
Machine m2=m1;
m2.disp();
return 0;
}
Slip-19
q1]

#include<iostream>
using namespace std;
class Distance
{
private:
int meter,centimeter;
public:
Distance(int m,int cm)
{
meter=m;
centimeter=cm;
}
Distance Larger(Distance *ptr)
{
if((this->meter*100+this->centimeter)>=(ptr->meter*100+ptr-
>centimeter))
return *this;
else
return *ptr;
}
void disp()
{
cout<<endl<<"Meter:"<<meter<<endl<<"Centimeter:"<<centimeter<<endl;
}
};
int main()
{
int m1,m2,c1,c2;
cout<<"Enter First Distance:";
cin>>m1>>c1;
cout<<"Enter Second Distance:";
cin>>m2>>c2;
Distance d1(m1,c1);
Distance d2(m2,c2);
cout<<"First Distance:";
d1.disp();
cout<<"Second Distance:";
d2.disp();
Distance l=d1.Larger(&d2);
cout<<"Larger Distance Is:";
l.disp();
return 0;
}

Slip-20
q1]
#include <iostream>
using namespace std;
class Number
{
private:
int num;
public:
Number(int n)
{
num=n;
}
Number pre()
{
++num;
return num;
}
Number post()
{
Number temp=num;
num++;
return temp;
}
void display()
{
cout<<"Number:"<<num<<endl;
}
};
int main()
{
Number num(5);
cout<<"Original Number: ";
num.display();
num.pre();
cout<<"After pre-increment: ";
num.display();
num.post();
cout<<"After post-increment: ";
num.display();
return 0;
}
q2]
#include<iostream>
#include<string.h>
using namespace std;
class MobileInventory
{
private:
char Model[50];
char Company[50];
char Color[20];
int Price;
int Quantity;
public:
MobileInventory(const char *Model,const char *Company,const char *Color, int
Price, int Quantity) {
strcpy(this->Model,Model);
strcpy(this->Company,Company);
strcpy(this->Color,Color);
this->Price=Price;
this->Quantity=Quantity;
}
void accept()
{
cout<< "Enter Model:";
cin>>Model;
cout<<"Enter Company:";
cin>>Company;
cout<<"Enter Color:";
cin>>Color;
cout<<"Enter Price:";
cin>>Price;
cout<<"Enter Quantity:";
cin>>Quantity;
}
void sell(int q)
{
if(q<=Quantity)
{
Quantity=Quantity-q;
cout<<q<<" Mobile sold successfully."<<endl;
}
else
{
cout<<"Insufficient stock."<<endl;
}
}
void purchase(int p)
{
Quantity=Quantity+p;
cout<<p<<" Mobile purchased successfully."<<endl;
}
void display()
{
cout<<"Model:"<<Model<<endl;
cout<<"Company:"<<Company<<endl;
cout<<"Color:"<<Color<<endl;
cout<<"Price:"<<Price<<endl;
cout<<"Quantity:"<<Quantity<<endl;
}
};
int main()
{
char Model[50],Company[50],Color[20];
int Price,Quantity;
MobileInventory mobile(Model,Company,Color,Price,Quantity);
mobile.accept();
cout<<"\n Mobile Inventory Details:"<<endl;
mobile.display();
int qs;
cout<<"\n Enter the number of mobiles to sell: ";
cin>>qs;
mobile.sell(qs);
int qp;
cout<<"\n Enter the number of mobiles to purchase: ";
cin>>qp;
mobile.purchase(qp);
cout<<"\n Updated Mobile Inventory Details:"<<endl;
mobile.display();
return 0;
}

Slip-22

q1]

#include<iostream>
template<typename T>
T square(T num)
{
return num*num;
}
template<typename T>
T cube(T num)
{
return num*num*num;
}
int main()
{
cout<<"Square of 5:"<<square(5)<<endl;
cout<<"Cube of 3:"<<cube(3)<<endl;
return 0;
}

q2]

#include <iostream>
#include <cstring>
class MyString
{
private:
char *str;
public:
MyString(const char *s="")
{
str=new char[strlen(s)+1];
strcpy(str,s);
}
~MyString()
{
delete[] str;
}
void display()
{
cout<<str;
}
MyString operator+(const MyString &other)
{
MyString result;
delete[] result.str;
result.str=new char[strlen(str)+strlen(other.str)+1];
strcpy(result.str,str);
strcat(result.str,other.str);
return result;
}
};
int main()
{
MyString str1("Hello, ");
MyString str2("world!");
MyString result=str1 + str2;
cout<<"Concatenated string: ";
result.display();
return 0;
}

slip-25

q1]

#include<iostream>
#include<algorithm>
using namespace std;
class statistics
{
public:
inline float mean(int a,int b,int c)
{
return (a+b+c)/3.0f;
}
inline float median(int a,int b,int c)
{
if((a==b&&b==c)||(a==b)||(b==c))
return b;
else
return c;
}
inline float mode(int a,int b,int c)
{
int arr[]={a,b,c};
sort(arr,arr+3);
return arr[1];
}
};
int main()
{
int n1,n2,n3;
cout<<"Enter Three Numbers:";
cin>>n1>>n2>>n3;
statistics stats;
int mean=stats.mean(n1,n2,n3);
int median=stats.median(n1,n2,n3);
int mode=stats.mode(n1,n2,n3);
cout<<"Mean:"<<mean<<endl;
cout<<"Median:"<<median<<endl;
cout<<"Mode:"<<mode<<endl;
}

Slip-26

q1]
#include <iostream>
using namespace std;
int average(int a,int b,int c)
{
return (a+b+c)/3;
}

float average(float a,float b,float c)


{
return (a+b+c)/3;
}
int main()
{
int int1,int2,int3;
cout<<"Enter three integer numbers:";
cin>>int1>>int2>>int3;
float float1,float2,float3;
cout<<"Enter three float numbers:";
cin>>float1>>float2>>float3;
float intAvg=average(int1,int2,int3);
cout<<"Average of integer numbers:"<<intAvg<<endl;
float floatAvg=average(float1,float2,float3);
cout<<"Average of float numbers:"<<floatAvg<<endl;
return 0;
}

Slip-27
q1]
#include <iostream>
class College
{
private:
char College_Id[30];
char College_Name[30];
int Establishment_Year;
char University_Name[30];
public:
friend istream &operator>>(istream &in,College &college)
{
cout<<"Enter College ID:";
in.getline(college.College_Id,30);
cout<<"Enter College Name: ";
in.getline(college.College_Name,30);
cout<<"Enter Establishment Year:";
in>>college.Establishment_Year;
in.ignore();
cout<<"Enter University Name:";
in.getline(college.University_Name,30);
return in;
}
friend ostream &operator<<(ostream &out,const College &college)
{
out<<"College ID:"<<college.College_Id<<endl;
out<<"College Name:"<<college.College_Name<<endl;
out<<"Establishment Year:"<<college.Establishment_Year<<endl;
out<<"University Name:"<<college.University_Name<<endl;
return out;
}
};
int main()
{
College college;
cout<<"Enter College Information:"<<endl;
cin>>college;
cout<<"\n College Information:"<<endl;
cout<<college;
return 0;
}

Slip-29
q1]
#include <iostream>
class E_Bill
{
private:
char Cust_Name[50];
int Meter_ID;
int No_of_Units;
float Total_Charges;
public:
void acceptCustomerInfo()
{
cout<<"Enter Customer Name:";
cin.getline(Cust_Name,50);
cout<<"Enter Meter ID:";
cin>>Meter_ID;
cout<<"Enter Number of Units: ";
cin>>No_of_Units;
calculateCharges();
}
void calculateCharges()
{
if (No_of_Units<= 100)
Total_Charges=1*No_of_Units;
else if(No_of_Units<=300)
Total_Charges=100+2*(No_of_Units-100);
else
Total_Charges=500+5*(No_of_Units-300);
Total_Charges=max(Total_Charges, 150);
if(Total_Charges>250)
Total_Charges=Total_Charges*1.15;
}
void displayCustomerInfo()
{
cout<<"\n Customer Name:"<<Cust_Name<<endl;
cout<<"Meter ID:"<<Meter_ID<<endl;
cout<<"Number of Units:"<< No_of_Units<<endl;
cout<<"Total Charges: Rs."<<Total_Charges<<endl;
}
};
int main()
{
E_Bill customer;
customer.acceptCustomerInfo();
cout<<"\n Customer Bill Details:"<<endl;
customer.displayCustomerInfo();
return 0;
}

You might also like