Lab Report 4
Lab Report 4
Lab Report 04
1st SEMESTER
Theory:
• Class Function:
A class in C++ is the building block that leads to Object-Oriented
Programming. It is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance in class.
• setw() Function:
setw() Function is a C++ Manipulator which stands for set
width. The manipulator sets the ios library field width or specifies the minimum number
of character positions a variable can consume. In most simplest terms it can be stated that
the setw()C++ function helps in adjusting the width used for output operation(For the
purpose of giving aesthetic look to the program.
• setfill() Function:
The setfill() method of iomanip library in C++ is used to set the ios
library fill character based on the character specified as the parameter to this
method. Parameters: This method accepts c as a parameter which is the character
argument corresponding to which the fill is to be set.
Using setfill() Function, we can use any symbol in order to make the output look
aesthetic and in other words, eye-catching.
• Dot(.) Operator:
Dot(.) operator is a operator associated with C/C++ language
which is used for direct member selection through an object name. Using a dot
operator, all available functions can be recalled from standard library related to
relevant function with whom it is used.
Lab Tasks
Q. Make use of user- defined function to simply print credentials on screen.
Code:
#include<iostream>
using namespace std;
int monsieur()
{
string ans;
cout << "\n Bonjour! Wie geht's?";
cout << "\n What do You Like?";
int a;
cout << "\n PLease Enter Your Age=";
cin >> a;
}
int main()
{
monsieur();
Output:
Q.2 Write a program using flags and mathematical functions to calculate the Area
of Circle.
Input:
#include<iostream>
#include<cmath>
using namespace std;
double PI = acos(-1.0);
int main()
{
double area;
double radius;
cout.setf(ios::fixed);
cout.precision(4);
cout << "\n The Area of Circle=" << area << "square centimeters";
return 0;
}
Output:
Q.3 Use setw() amd setfill() function in order to beautify the program taking in
sinple detail (Age).
Input:
#include<iostream>
#include<iomanip>
cout << setw(60) << setfill('$') << "\n Congragulations!Its your lucky Day!";
cout << "\n Auf Wiedersehen! ";
cout << setw(50) << setfill('^') << "\n Have a Good Day!!";
return 0;
Output:
Lab Assignment
Q.1 Write a C++ program taking an input in kilometres and converting to miles.
Input:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double km;
double mile;
cout << "\n Please Enter Length in Kilometres=";
cin >> km;
mile = 1.6093440*km;
cout.setf(ios::fixed);
cout.precision(5);
Output:
Q.2 Write a C++ Program taking in length in metres and converting to miles.
Input:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double metre;
double mile;
cout << setw(50) << setfill('^') << "\n Please Enter length in Metres=";
cin >> metre;
cout.setf(ios::scientific);
cout.precision(2);
mile = (1609.344)*metre;
cout << "\n The Length in Miles=" << mile;
return 0;
}
Output:
Q.3 Write a Program that converts pounds to kilograms
Input:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double pound;
double kilogram;
Output:
Q.4 Write a C++ Program that converts newtons to pounds.
Input:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double newton;
double lb;
cout << setw(40) << setfill('*') << "\nHello User! Welcome to the Program!";
cout << "\nEnter the Force in Newtons = ";
cin >> newton;
lb = (0.224) *newton;
cout.setf(ios::left);
cout.precision(2);
cout << "\nThe Force in Pounds = " << lb;
return 0;
Output:
Input:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double K;
double F;
cout << setw(13) << setfill('!') << "\n We are going to study
Thermodynamics!";
cout << "\n Enter Temperature in Kelvins=";
cin >> K;
Output:
Q.6 Write a C++ Program taking in Temperature in Fahrenheit and converting To Rankin.
Input:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout << setw(90) << setfill('*') << "\n Hello! Lets study Thermodynamics!!";
double F;
double R;
cout << setw(90) << setfill('-') << "\n PLease enter temperature in
Fahrenheit=";
cin >> F;
R = F + 459.67;
cout << setw(90) << setfill('^') << "\n The Temperature in Rankin=" << R;
return 0;
}
Output:
Input:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double C;
double F;
double R;
cout << setw(100) << setfill('+') << "\n There we are! Back to Conversion
Factors " << endl;
cout << setw(100) << setfill('|') << "\n Please Enter Temperature in
Celsius=" << endl;
cin >> C;
F = (1.8) * C + 32;
cout.setf(ios::fixed);
cout.precision(4);
cout << setw(100) << setfill('|') << "\n The Temperature in Fahrenheit= " <<
F << endl;
}
Output: