CSE222: Object Oriented Programming Lab 2
Lab Objectives
To gain experience with
- the activity of programming
- Write some simple C++ program
- using cin object
- simple expressions
Type in the following program and run it.
1 #include <iostream.h>
2 using namespace std;
3 int main()
4 {
5 float number1,number2,sum,average;
6 cout << "Enter two numbers: "; //prompt
7 cin >> number1;
8 cin >> number2;
9 sum= number1+number2;
10 //average= sum/2;
11 cout << "Sum=" << sum<<"\n";
12 cout << "Average= " << (sum/2) <<"\n";
13 return 0;
14 }
2 using namespace std;
3 int main()
4 {
5 float number1,number2,sum,average;
6 cout << "Enter two numbers: "; //prompt
7 cin >> number1;
8 cin >> number2;
9 sum= number1+number2;
10 //average= sum/2;
11 cout << "Sum=" << sum<<"\n";
12 cout << "Average= " << (sum/2) <<"\n";
13 return 0;
14 }
In line number 7 and 8 we use two cin statements to get two integer numbers from key board. We can use both statement in one statement like cin >> number1 >>number2.
This statement will do the same thing. On line number 10 we make the statement into comments. On line number 12 we calculating and displaying average so we didn’t use the line number 10. Here we see how to print numeric data.
Now using the experience of this program do the following programs exercise? Your output should be attractive. This all program will use simple expressions only.
1. Write a program that converts Celsius to Fahrenheit. The formula is
F = 9/5C + 32.
2. Write a program to calculate the volume of a sphere,
Volume =4/3
r3
3. Write a program to print out the perimeter of a rectangle given its height and width.
Perimeter = 2 · (width + height)
4. Write a program that takes hours and minutes as input and outputs the total number of minutes (e.g., 1 hour 30 minutes = 90 minutes).
5. Write a program that takes an integer as the number of seconds and outputs the total hours and minutes and seconds (e.g., 3665 seconds = 1 hour 1 minute 5 seconds).
No comments:
Post a Comment