C++ Practice
C++ Practice
Fall 2018
Software Engineering Department, NED University of Engineering and Technology
Week #2
Named constants
Expressions
Programming Exercise:
Use setw(width) manipulator for formatting output where required.
Q #1: Write a program that read two integers from the keyboard and print their sum and
average.
Sample run:
Enter first number: 25
Enter second number: 25
Sum is 50
Average is 25
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 f oot2Inch= 12 inch.
Now, the value obtained can easily, be translated into feet and inches which are
then output by the program.
Sample run:
Enter the height in inches: 20”
This is equivalent to 1’ 8”
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.
Sample run:
Enter the time in seconds:3713
Hours in time is: 1
Minutes in time: 1
Seconds in time is: 53
Object-Oriented Programming (Lab Exercise)
Fall 2018
Software Engineering Department, NED University of Engineering and Technology
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.
Sample run:
Enter amount in rupees: 5788
1000’s in the given amount is: 5
500’s in the given amount is: 1
100’s in the given amount is: 2
50’s in the given amount is: 1
10’s in the given amount is: 3
5’s in the given amount is: 1
2’s in the given amount is: 2
1’s in the given amount is: 1
Q #5: Write a program that calculates the temperature in Fahrenheit. For that it prompts
for temperature in Celsius degrees. Formula to calculate Fahrenheit temperature
is Fahrenheit=Celsius (9/5+32). Once if the task done do the vice versa i.e.
Celsius=5/9(Fahrenheit -32)
Sample run:
Enter the Fahrenheit temperature: 98.6
Celsius Temperature: 37.0
Q #6: Write a program that inputs a two digit integer value, and output its reverse order.
Sample run:
Enter a 2 digit integer value: 45
Reverse of this value is: 54
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.
ValueTen=chTen-‘0’; // ’8’-‘0’ is 8
ValueUnit=chUnit-‘0’; // ’2’-‘0’ is 2
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
Sample run:
Enter a two digit number: 82
Numeric Value is: 82
-------------------------------------------------------------------------------------------------------------------
Object-Oriented Programming (Lab Exercise)
Fall 2018
Software Engineering Department, NED University of Engineering and Technology