You are on page 1of 5

OOP LAB 1

Q #1: Write a program that read two integers from the keyboard and print their sum and
average.

#include <iostream>
using namespace std;

int main() {
double a,b,sum;
double average;

cout << "Enter first number: ";


cin >> a;
cout << "Enter second number: ";
cin >> b;

sum = a+b;
average = sum*0.5;

cout << "Sum is " << sum << endl;


cout << "Average is " << average << endl;

return 0;
}

OUTPUT:

Q #2: Write a program that prompts for a person’s height in inches. Convert this height
measurement into feet by using the conversion factor of foot2Inch= 12 inch. Now, the
value obtained can easily, be translated into feet and inches which are then output by
the program.

#include <iostream>
#include <math.h>
using namespace std;

int main() {
double inch, feet, f1, f2, inch1;

cout << "Enter height in inches: ";


cin >> inch;

feet = (inch / 12);


f1 = floor(feet);
f2 = feet - f1;
inch1 = f2 * 12;

cout << "This is equivalent to " << f1 <<"' "<< inch1 << "''";
return 0;
}

OUTPUT:

Q #3: Write a program that prompts for time in seconds and output that time in hours,
minutes,
and seconds. Here student will learn the usage of divide and modulus arithmetic
operators in integers.

int main() {

double seconds, secondsToHours, hours, h1, secondsToMinutes,minutes,s1 ;

cout << "Enter time in seconds: ";


cin >> seconds;

// find the number of whole hours


secondsToHours = (seconds / 3600);
hours = floor(secondsToHours);

// find the number of whole minutes


h1 = secondsToHours - hours;
secondsToMinutes = h1 * 60;
minutes = floor(secondsToMinutes);

//find the remaining seconds


s1 = secondsToMinutes - minutes;
seconds = s1 * 60;

cout << "Hours in time is: " << hours << endl;
cout << "Minutes in time is: " << minutes << endl;
cout << "Seconds in time is: " << seconds;
return 0;
}

OUTPUT:

Q #4: Write a program that prompts for amount in rupees and show how many 1000’s,
500’s,
100’s, 50’s, 10’s, 5’s, 2’s and 1’s in it.

#include <iostream>
#include <math.h>
using namespace std;

int main() {

int amount, Rs1000, Rs500, Rs100, Rs50, Rs10, Rs5, Rs2;


cout << "Enter amount: ";
cin >> amount;

Rs1000 = amount/1000;
amount = amount % 1000;

Rs500 = amount / 500;


amount = amount % 500;

Rs100 = amount / 100;


amount = amount % 100;

Rs50 = amount / 50;


amount = amount % 50;

Rs10 = amount / 10;


amount = amount % 10;

Rs5 = amount / 5;
amount = amount % 5;

Rs2 = amount;

cout << "1000's int the given amount is: " << Rs1000 << endl;
cout << "500's int the given amount is: " << Rs500 << endl;
cout << "100's int the given amount is: " << Rs100 << endl;
cout << "50's int the given amount is: " << Rs50 << endl;
cout << "10's int the given amount is: " << Rs10 << endl;
cout << "5's int the given amount is: " << Rs5 << endl;
cout << "2's int the given amount is: " << Rs2;

return 0;
}
OUTPUT:

Q #5: Write a program that calculates the temperature in Fahrenheit. For that it prompts for
temperature in Celsius degrees.Once if the task done do the vice versa

int main() {
double fahrenheit, celsius, fahrenheitToCelsius, CelsiusToFahrenheit;
cout << "Enter temperature in fahrenheit: ";
cin >> fahrenheit;

fahrenheitToCelsius = (fahrenheit -32 ) / 1.8;


cout << "Celsius temperature: " << fahrenheitToCelsius << endl;

cout << "Enter temperature in Celsius: ";


cin >> celsius;

CelsiusToFahrenheit = (celsius * 1.8 ) + 32;


cout << "Fahrenheit temperature: " << CelsiusToFahrenheit;
return 0;
}

OUTPUT:

Q #6: Write a program that inputs a two digit integer value, and output its reverse order.

int main() {

int number,modOfNumber, remainder,reverse,b;


cout << "Enter a two digit integer value: ";
cin >> number;

modOfNumber = number % 10;


remainder = floor(number / 10);

b = modOfNumber * 10;
reverse = b + remainder;

cout << "The reverse of this value is: " << reverse;

return 0;
}

OUTPUT:

Q #7: Write a program that reads the two digit nmber as two characters chTen and chUnit
and
convert that two digit number into an integer value. In order to compute the
corresponding integer value, each character must be converted to the digit in the range
0 to 9. this is done by subtracting 48(‘0’) from the ASCII value of the character.
To create integer value fro m, the positional value of each digit must be used. In this case
multiply ValueTen by 10.M=ValueTen*10+ValueUnit;//m=8*10+2=82
#include <iostream>
using namespace std;

int main(){
char chTen, chUnit;
int integerValue,valueTen,valueUnit;

cout << "Enter a two digit number: ";


cin >> chTen >> chUnit;

valueTen = chTen - '0';


valueUnit = chUnit - '0';

integerValue = valueTen*10+valueUnit;
cout << "The integer value of the two-digit number is: " << integerValue;

return 0;
}

OUTPUT:

You might also like