OOPs Lab File_220228_142350
OOPs Lab File_220228_142350
Dhiren M S
500097574
R290221089
—
B.Tech. Aerospace Engineering
1st Year Batch I
—
Ved Prakash Bhardwaj
Experiment – 1
1. Write a C++ program to print “Hello UPES” in the screen.
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello Upes!"<<endl;
return 0;
}
START
int main()
cout<<”Hello UPES!”<<endl;
END
2. Write a C++ program to convert an upper case character to lower case
character.
#include<iostream>
using namespace std;
int main()
{
char c;
cout<<"Enter Upper Case Character.\n";
cin>>c;
c=c+32;
cout<<"Character in Lower Case : "<<c<<endl;
return 0;
}
START
int main()
char c;
INPUT: c;
c=c+32;
END
3. Write a C++ program to check whether a number is even or odd.
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter The Number.\n";
cin>>n;
if(n%2==0)
{
cout<<"Even.\n";
}
else
{
cout<<"Odd.\n";
}
return 0;
}
START
int main()
int n;
INPUT: n
if(n%2==0)
True False
END
4. Write a program to read the values of a, b and c and display the value of
x, where x = a / (b-c) and test the program for the values a = 200, b = 70 &
c =25.
#include<iostream>
using namespace std;
int main()
{
int a,b,c,x;
cout<<"Enter a, b and c.\n";
cin>>a>>b>>c;
x=a/(b-c);
cout<<"x = "<<x<<endl;
return 0;
}
START
int main()
int a, b, c, x;
INPUT: a, b, c;
x=a/(b-c);
OUTPUT: “x = ” x
END
Experiment – 2
1. Write a C++ program that converts temperature in Fahrenheit and display
it in Celsius.
#include<iostream>
using namespace std;
int main()
{
float f,c;
cout<<"Enter the temperature in Fahrenheit.\n";
cin>>f;
c=(5*(f-32))/9;
cout<<"Temperature in Celsius = "<<c<<endl;
return 0;
}
START
int main()
float f, c;
INPUT: f;
c=(5*(f-32))/9;
END
2. Write a C++ program using “Switch” that will take an integer value from
the user and print the corresponding day (i.e., Monday if user provides 1,
Tuesday if user provides 2, …, so on)
#include<iostream>
using namespace std;
int main()
{
int d;
cout<<"Enter the Day Number.\n";
cin>>d;
switch(d)
{
case 1:
{
cout<<"Monday\n";
break;
}
case 2:
{
cout<<"Tuesday\n";
break;
}
case 3:
{
cout<<"Wednesday\n";
break;
}
case 4:
{
cout<<"Thursday\n";
break;
}
case 5:
{
cout<<"Friday\n";
break;
}
case 6:
{
cout<<"Saturday\n";
break;
}
case 7:
{
cout<<"Sunday\n";
break;
}
default:
{
cout<<"Enter a Valid Number from 1 to 7!\n";
break;
}
}
return 0;
}
START
int main()
int d;
INPUT: d;
switch(d)
END
3. Write a C++ program to convert an upper case string to lower case string.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string S;
cout<<"Enter Upper Case String.\n";
getline(cin, S);
for(int i = 0; i<S.length(); i++)
{
S[i] = tolower(S[i]);
}
cout<<"Character in Lower Case : "<<S<<endl;
return 0;
}
START
int main()
string S;
INPUT: S;
END
4. Write a C++ program to swap two number without using third variables.
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout<<"Enter The Numbers.\n";
cin>>a>>b;
cout<<"Original-\na="<<a<<"\nb="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"Swaped-\na="<<a<<"\nb="<<b<<endl;
return 0;
}
START
int main()
int a, b;
INPUT: a, b;
a=a+b;
b=a-b;
a=a-b;
cout<<“Swapped Numbers-
\na=”<<a<<“\nb=”<<b<<endl;
cout<<“Original Numbers-
\na=”<<a<<“\nb=”<<b<<endl;
END
5. Write a C++ program to print Fibonacci Series without using recursion. The
Fibonacci Series is the series of numbers: 0, 1, 1, 2, 3, 5, 8, …
#include<iostream>
using namespace std;
int main()
{
int n, t1=0, t2=1, s=0;
cout<<"Enter the Number of Terms!\n";
cin>>n;
cout<<"\n\nFibonacci Series-\n0, 1";
for(int i=3; i<=n; i++)
{
s=t1+t2;
t1=t2;
t2=s;
cout<<", "<<s;
}
return 0;
}
START
int main()
INPUT: n;
s=t1+t2;
t1=t2; False
t2=s;
OUTPUT: “, " s
END
6. Write a C++ program to check whether a number is Armstrong or not. An
Armstrong number is a number that equals to the sum of cubes of its digit.
For example, 407 is an Armstrong number.
#include<iostream>
using namespace std;
int main()
{
int n, a=0;
cout<<"Enter the Numbers.\n";
cin>>n;
int t=n;
while(t!=0)
{
int x=t%10;
t=t/10;
a=a+(x*x*x);
}
if(n==a)
cout<<"Armstrong Number\n";
else
cout<<"Not Armstrong Number\n";
return 0;
}
START
int main()
int n, a=0;
INPUT: n;
int t=n;
while(t!=0)
True False
int x=t%10;
t=t/10;
a=a+(x*x*x);
if(n==a)
True False
END
7. Write a C++ program to check whether a number is prime or not.
#include<iostream>
using namespace std;
int main()
{
int a, t=0;
cout<<"Enter the Number.\n";
cin>>a;
for (int i = 1; i<=a; i++)
{
if(a%i==0)
t++;
}
if(t>2)
cout<<"Not Prime Number.\n";
else
cout<<"Prime Number.\n";
return 0;
}
START
int main()
int a, t=0;
INPUT: a;
False
if(a%i==0)
True False
t++;
if(t>2)
True False
END
Experiment – 3
1. Write a C++ program using function to check whether a number is positive
or not.
#include <iostream>
using namespace std;
class positive_fn
{
public:
int n;
void input()
{
cout<<"Enter the Number.\n";
cin>>n;
}
int check(int a)
{
if(a>=0)
return 1;
else
return 0;
}
void display()
{
int t=check(n);
if(t==1)
cout<<"Positive Number.\n";
else
cout<<"Negative Number.\n";
}
};
int main()
{
positive_fn P;
P.input();
P.display();
return 0;
}
START
class positive_fn
int n;
void input()
INPUT: n;
int check(int a)
if(a>=0)
True False
return 1; return 0;
void display()
int t=check(n);
if(t==1)
True False
OUTPUT: “ Positive Number”
positive_fn P;
P.input();
P.display();
END
2. Write a C++ program using function to check whether a number is
Armstrong or not. An Armstrong number is a number that equals to the
sum of cubes of its digit. For example, 407 is an Armstrong number.
#include <iostream>
return 0;
}
START
class positive_fn
int n, s=0, t;
void input()
INPUT: n;
t=n;
void check()
if(t!=1)
True
int m=arm(t);
s=s+(m*m*m);
t=t/10;
check();
int arm(int a)
return a%10
void display()
if(n==s)
True False
int main()
armstrong_recursion A;
A.input();
A.check();
A.display();
END
3. Write a program using function to check whether a number is even or
odd.
#include<iostream>
using namespace std;
class odd_even_fn
{
public:
int n;
void input()
{
cout<<"Enter the Number.\n";
cin>>n;
}
int check(int a)
{
return a%2;
}
void display()
{
int t=check(n);
if(t==0)
cout<<"Even\n";
else
cout<<"Odd\n";
}
};
int main()
{
odd_even_fn A;
A.input();
A.display();
return 0;
}
START
class odd_even_fn
int n;
void input()
INPUT: n;
int check(int a)
return a%2;
void display()
int t=check(n);
if(n%2==0)
True False
int main()
odd_even_fn A;
A.input();
A.display();
END
4. Write a simple C++ program using function to swap the values of two
variables.
#include<iostream>
using namespace std;
class swap_fn
{
public:
int a, b;
void input()
{
cout<<"Enter the Numbers.\n";
cin>>a>>b;
}
void swap()
{
int t=a;
a=b;
b=t;
}
void display()
{
cout<<"Original-\na="<<a<<"\nb="<<b<<endl;
swap();
cout<<"Swaped-\na="<<a<<"\nb="<<b<<endl;
}
};
int main()
{
swap_fn A;
A.input();
A.display();
return 0;
}
START
class swap_fn
int a, b;
void input()
INPUT: a, b;
void swap()
int t=a;
a=b;
b=t;
void display()
cout<<“Original Numbers-
\na=”<<a<<“\nb=”<<b<<endl;
swap();
cout<<“Swaped Numbers-
\na=”<<a<<“\nb=”<<b<<endl;
int main()
swap_fn A;
A.input();
A.display();
END
5. Write a C++ program using function to print first n number of a Fibonaaci
Series.
#include<iostream>
using namespace std;
class fibonaccirec
{
public:
int n, t1=0, t2=1;
fibonaccirec()
{
cout<<"Enter Number of terms.\n";
cin>>n;
n=n-2;
}
void fibonacci()
{
int t=t1+t2;
t1=t2;
t2=t;
n=n-1;
display2();
}
void display1()
{
cout<<"Fibonacci Series:-\n0";
}
void display2()
{
cout<<", "<<t2;
if(n!=0)
fibonacci();
}
};
int main()
{
fibonaccirec A;
A.display1();
A.display2();
return 0;
}
START
class fibonaccirec
INPUT: n;
n=n-2;
void fibonacci()
int t=t1+t2;
t1=t2;
t2=t;
n=n-1;
display2();
void display1()
void display2()
OUTPUT: “, ” t2
if(n!=0)
True
fibonacci();
int main()
fibonaccirec A;
A.display1();
A.display2();
END
Experiment – 4
1. WAP with a class date and time with data members such as day, month,
year and hour, min and second with the Methods set () and get () to input
and display the state of the object.
#include<iostream>
using namespace std;
class datentime
{
public:
int day, month, year, hour, min, second;
void set()
{
cout<<"Enter Day, Month, Year, Hour, Minute and Second respectively.\n";
cin>>day>>month>>year>>hour>>min>>second;
}
void get()
{
cout<<"Date - "<<day<<"/"<<month<<"/"<<year<<endl;
cout<<"Time - "<<hour<<":"<<min<<":"<<second<<endl;
}
};
int main()
{
datentime dt;
dt.set();
dt.get();
return 0;
}
START
class datentime
void set()
void get()
int main()
datentime dt;
dt.set();
dt.get();
END
2. Write a main program to test the program
Data members:
1. Name of the depositor.
2. Account number.
3. Type of account.
4. Balance amount in the account.
Member functions:
1. To assign initial values.
2. To deposit an amount.
3. To withdraw an amount after checking the balance.
4. To display the name and balance.
#include<iostream>
#include<string>
using namespace std;
class bank
{
public:
string n, t;
int no, b, x;
void input()
{
cout<<"Input Account Holder's Name.\n";
getline(cin, n);
cout<<"Input Account Number.\n";
cin>>no;
cout<<"Input Account Type.\n";
cin>>t;
cout<<"Input Account Balance.\n";
cin>>b;
}
void operations()
{
int x;
cout<<"Enter The Operation Number:-\n1. Deposit\n2. Withdraw\n";
cin>>x;
if(x==1)
deposit();
else if(x==2)
withdraw();
else
cout<<"Enter a Valid Value.\n";
}
void deposit()
{
cout<<"Enter the amount to deposit.\n";
cin>>x;
b=b+x;
}
void withdraw()
{
cout<<"Enter the amount to withdraw.\n";
cin>>x;
b=b-x;
}
void display()
{
cout<<"Account Holder's Name ->"<<n<<"\nAccount Number ->"<<no<<"\nAccount Type -
>"<<t<<"\nAccount Balance ->"<<b;
}
};
int main(void)
{
bank B;
B.input();
B.operations();
B.display();
return 0;
}
START
class bank
string n, t;
int no, b, x;
void input()
INPUT: n;
INPUT: no;
INPUT: t;
INPUT: b;
void operations()
int x;
INPUT: x;
if(x==1)
True False
deposit()
if(x==2)
True
withdraw() False
void deposit()
INPUT: x;
b=b+x;
void withdraw()
INPUT: x;
b=b-x;
void display()
bank B;
B.input();
B.operations();
B.display();
END
3. Given that an EMPLOYEE class contains following members: data
members: Employee number, Employee name, Basic, DA, IT, Net Salary
and print data members. Write a C++ program to read the data of N
employee and compute Net salary of each employee (DA=52% of Basic
and Income Tax (IT) =30% of the gross salary)
#include<iostream>
#include<string>
using namespace std;
class employee
{
public:
string name;
float no, basic, DA=0, IT=0, GS=0, NS=0;
void input()
{
cout<<"Enter the Employee Name.\n";
getline(cin, name);
cout<<"Enter the Employee Number.\n";
cin>>no;
cout<<"Enter the Employee Basic.\n";
cin>>basic;
}
void calculations()
{
DA=basic*0.52;
GS=basic+DA;
IT=GS*0.30;
NS=GS-IT;
}
void display()
{
cout<<"Employee Name=> "<<name<<"\nEmployee Number=> "<<no<<"\nEmployee Gross
Salary=> Rs. "<<GS<<"\nEmployee Income Tax=> Rs. "<<IT<<"\nEmployee Net Salary=> Rs.
"<<NS<<endl;
}
};
int main()
{
employee e;
e.input();
e.calculations();
e.display();
return 0;
}
START
class employee
string name;
void input()
INPUT: name;
INPUT: no;
INPUT: basic;
void calculations()
DA=basic*0.52;
GS=basic+DA;
IT=GS*0.30;
NS=GS-IT;
void display()
int main()
employee e;
e.input();
e.calculations();
e.display();
END
Experiment – 5
1. Write a default constructor and destructor.
#include<iostream>
using namespace std;
class consdes
{
public:
consdes()
{
cout<<"Constructor.\n";
}
~consdes()
{
cout<<"Destructor.\n";
}
};
int main()
{
consdes a1; //constructor called
} //destructor called
START
class consdes
consdes()
OUTPUT: “Constructor.”
~consdes()
OUTPUT: “Destructor.”
int main()
consdes a1
END
2. Exercise the concept of constructor overloading with parametric
constructor and copy constructor.
#include<iostream>
using namespace std;
class consoverload
{
public:
int n;
consoverload(int a)
{
n=a;
cout<<"Parameterized Constructor.\n";
}
consoverload(consoverload &p)
{
n=p.n;
cout<<"Copy Constructor. "<<n<<endl;
}
};
int main()
{
consoverload a1(1);
consoverload a2(a1);
}
START
class consoverload
n=a;
consoverload(int a)
consoverload(consoverload &p)
n=p.n;
consoverload a1(1)
consoverload a2(a1)
END
Experiment – Array
1. Write a C++ program to find the largest element of a given array of
integers.
2. Write a C++ program to find the largest three elements in an array.
3. Write a C++ program to find second largest element in a given array of
integers.
4. Write a C++ program to find k largest elements in a given array of integers.
5. Write a C++ program to find the second smallest elements in a given array
of integers.
6. Write a C++ program to find all elements in array of integers which have
at-least two greater elements.
7. Write a C++ program to find the most occurring element in an array of
integers.
#include<iostream>
using namespace std;
class arrayfn
{
public:
int c;
int n;
int a[];
void code()
{
cout<<"Enter the code:\n\n1. To Find the Largest Element in Array.\n2. To Find
Largest 3 Elements in Array.\n3. To Find the Second Largest Element in Array.\n4. To Find
Largest k Elements in Array.\n5. To Find the Second Smallest Element in Array.\n6. To Find
the Elements which have atleast 2 Greater Elements in Array.\n7. To Find the Most Occuring
Element in Array.\n";
cin>>c;
if(c<=7&&c>=1)
input();
switch(c)
{
case 1:
{
largest();
break;
}
case 2:
{
large3();
break;
}
case 3:
{
secondlargest();
break;
}
case 4:
{
largek();
break;
}
case 5:
{
secondsmallest();
break;
}
case 6:
{
largethan2e();
break;
}
case 7:
{
mostoccur();
break;
}
default:
{
cout<<"Invalid Value.\n";
break;
}
}
}
void input()
{
cout<<"Enter Array length.\n";
cin>>n;
cout<<"Enter Array.\n";
for(int i=0; i<n; i++)
cin>>a[i];
}
void asort()
{
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
void dsort()
{
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(a[i]<a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int occurcount(int t)
{
int x=0;
for(int i=0; i<n; i++)
{
if(a[i]==t)
x++;
}
return x;
}
void largest()
{
dsort();
cout<<"Largest Element of Array => "<<a[0]<<endl;
}
void large3()
{
dsort();
int t=1;
cout<<"Largest 3 Elements of Array:-\n"<<a[0];
for(int i=1; i<n; i++)
{
if(a[i-1]==a[i])
continue;
else
{
cout<<", "<<a[i];
t++;
}
if(t==3)
break;
}
cout<<"\n";
if(t<3)
cout<<"Either Number of Elements in the Array is not Sufficient.\nOr Numbers in
array are repeated.\n";
}
void secondlargest()
{
dsort();
for(int i=1; i<n; i++)
{
if(a[i-1]==a[i])
continue;
else
{
cout<<"Second Largest Element of Array => "<<a[1]<<endl;
break;
}
}
}
void largek()
{
int k;
cout<<"Enter k.\n";
cin>>k;
if(k<=n&&k>0)
{
dsort();
int t=1;
cout<<"Largest k Elements of Array:-\n"<<a[0];
for(int i=1; i<n; i++)
{
if(a[i-1]==a[i])
continue;
else
{
cout<<", "<<a[i];
t++;
}
if(t==k)
break;
}
cout<<"\n";
if(t<k)
cout<<"Either Number of Elements in the Array is not Sufficient.\nOr Numbers
in array are repeated.\n";
}
else
cout<<"Invalid Value of k.\n";
}
void secondsmallest()
{
asort();
for(int i=1; i<n; i++)
{
if(a[i-1]==a[i])
continue;
else
{
cout<<"Second Smallest Element of Array => "<<a[1]<<endl;
break;
}
}
}
void largethan2e()
{
asort();
cout<<"Elements which have atleast 2 Greater Elements in Array:-\n"<<a[0];
for(int i=1; i<n-2; i++)
{
if(a[i]==a[n-2])
break;
else
cout<<", "<<a[i];
}
cout<<"\n";
}
void mostoccur()
{
asort();
int x=occurcount(a[0]);
int t=a[0];
int y;
for(int i=1; i<n; i++)
{
y=occurcount(a[i]);
if(y>x)
{
x=y;
t=a[i];
}
}
cout<<"Most Occuring Element in Array => "<<t<<", it occurs "<<x<<" times.\n";
}
};
int main()
{
arrayfn A;
A.code();
}